CS1116/CS5018

Web Development 2

Dr Derek Bridge

School of Computer Science & Information Technology

University College Cork

Body Mass Index (BMI)

bmi.html: a page with a form

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>BMI</title>
    </head>
    <body>
        <form action="bmi.py" method="get">
            <label for="mass_kg">Mass (kg): </label>
            <input type="text" name="mass_kg" id="mass_kg" />
            <label for="height_m">Height (m): </label>
            <input type="text" name="height_m" id="height_m" />
            <input type="submit" value="Calculate BMI" />
        </form>
    </body>
</html>

bmi.py: a program with bugs

#!/usr/local/bin/python3
            
from cgi import FiledStorage

print('Content-Type: text/html')

form_data = FieldStorage()
mass_kg = form_data.getfirst('mass_kg')
height_m = form_data.getfirst('height_m')
bmi = mass_kg / heiight_m * height_m
category = ''
if bmi < 18.5
    category = 'underweight'
elif bmi > 25
    category ='overweight
else
category = 'normal'

print("""
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title>BMI</title>
        </head>
        <body>
            <p>
                Your mass in kg is %s. Your height in m is %s. 
                Your BMI is %s. You are %s.
            </p>
        </body>
    </html>""" % (mass_kg, height_m, bmi, category))

Bugs

The first ever bug was a moth.

Fixing the program

Form data always arrives as strings

bmi.py: fixed!

#!/usr/local/bin/python3

from cgitb import enable
enable()            

from cgi import FieldStorage

print('Content-Type: text/html')
print()

form_data = FieldStorage()
mass_kg = float(form_data.getfirst('mass_kg'))
height_m = float(form_data.getfirst('height_m'))
bmi = mass_kg / (height_m * height_m)
category = ''
if bmi < 18.5:
    category = 'underweight'
elif bmi > 25:
    category ='overweight'
else:
    category = 'normal'

print("""
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title>BMI</title>
        </head>
        <body>
            <p>
                Your mass in kg is %.1f. Your height in m is %.1f. 
                Your BMI is %.2f. You are %s.
            </p>
        </body>
    </html>""" % (mass_kg, height_m, bmi, category))

bmi.py: fixed!

#!/usr/local/bin/python3

from cgitb import enable
enable()

from cgi import FieldStorage

print('Content-Type: text/html')
print()

form_data = FieldStorage()
mass_kg = float(form_data.getfirst('mass_kg'))
height_m = float(form_data.getfirst('height_m'))
bmi = mass_kg / (height_m * height_m)
category = ''
if bmi < 18.5:
    category = 'underweight'
elif bmi > 25:
    category ='overweight'
else:
    category = 'normal'

print("""
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>BMI</title>
        </head>
        <body>
            <p>
                Your mass in kg is %.1f. Your height in m is %.1f. 
                Your BMI is %.2f. You are %s.
            </p>
        </body>
    </html>""" % (mass_kg, height_m, bmi, category))