How to create BMI calculator web app using Python and PyWebio?

PyWebIO is a Python library that enables you to build web applications with simple user interfaces without requiring HTML and JavaScript knowledge. This tutorial demonstrates how to create a BMI (Body Mass Index) calculator web app using two different approaches.

BMI measures body fat based on weight and height, commonly used to determine if a person is underweight, normal weight, overweight, or obese.

Method 1: Object-Oriented Approach

This method uses a class-based structure to organize the BMI calculation logic ?

from pywebio.input import input, FLOAT
from pywebio.output import put_text

class BMICalculator:
    def __init__(self):
        self.height = None
        self.weight = None
        self.bmi = None
        self.classification = None

    def calculate_bmi(self):
        # Get user's height and weight
        self.height = input("Please enter your height in meters (m):", type=FLOAT)
        self.weight = input("Please enter your weight in kilograms (kg):", type=FLOAT)

        # Calculate BMI
        self.bmi = self.weight / (self.height ** 2)

        # Determine BMI classification
        if self.bmi < 16:
            self.classification = "Severely underweight"
        elif self.bmi < 18.5:
            self.classification = "Underweight"
        elif self.bmi < 25:
            self.classification = "Normal (healthy weight)"
        elif self.bmi < 30:
            self.classification = "Overweight"
        elif self.bmi < 35:
            self.classification = "Moderately obese"
        else:
            self.classification = "Severely obese"

        # Display results to the user
        put_text("Based on your height of {}m and weight of {}kg, your BMI is {:.1f}. This means you are classified as {}.".format(
            self.height, self.weight, self.bmi, self.classification))

# Create BMICalculator object and run
bmi_calculator = BMICalculator()
bmi_calculator.calculate_bmi()

Output

The web app will prompt for height first, then weight, and display the result ?

Based on your height of 1.7m and weight of 65kg, your BMI is 22.5. This means you are classified as Normal (healthy weight).

Method 2: Function-Based with Server

This simpler approach uses a single function and starts a web server ?

from pywebio.input import input, FLOAT
from pywebio.output import put_text
from pywebio import start_server

def calculate_bmi():
    height = input("Enter your height (in cm)", type=FLOAT)
    weight = input("Enter your weight (in kg)", type=FLOAT)
    
    bmi = weight / ((height/100) ** 2)
    bmi = round(bmi, 2)
    
    # Determine weight category
    if bmi < 18.5:
        weight_category = "underweight"
    elif 18.5 <= bmi <= 24.9:
        weight_category = "normal weight"
    elif 25 <= bmi <= 29.9:
        weight_category = "overweight"
    else:
        weight_category = "obese"
    
    put_text("Your BMI is: %s" % bmi)
    put_text("You have a %s" % weight_category)

if __name__ == '__main__':
    start_server(calculate_bmi, port=8080, debug=True, 
                title="BMI Calculator", button_text="Calculate BMI")

Output

This version runs on a local server and displays results in a similar format ?

Your BMI is: 21.22
You have a normal weight

Comparison

Method Structure Input Units Best For
Object-Oriented Class-based Height in meters Complex applications
Function-Based Single function Height in centimeters Simple web apps

Key Features of PyWebIO

PyWebIO offers several advantages for web development:

  • No HTML/JavaScript required Build web UIs using pure Python
  • Built-in widgets Text boxes, buttons, and dropdown menus
  • Framework compatibility Works with Flask, Django, and Tornado
  • Easy deployment Simple server setup with start_server()

Conclusion

PyWebIO makes it easy to create web applications without web development expertise. Use the object-oriented approach for complex applications or the function-based method for simple calculators like BMI tools.

Updated on: 2026-03-27T00:58:42+05:30

708 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements