Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1

format the output of the Python program.


LISTING 22.1 Using HTML in the Python Program Output


Click here to view code image


#!/usr/bin/python3

import math
print('Content-Type: text/html')
print('')
print('<!DOCTYTPE html>')
print('<html>')
print('<head>')
print('<title>The Area of a Circle</title>')
print('</head>')
print('<body>')
print('<h2>Calculating the area of a circle:</h2>')
print('<table>')
print('<tr><th>Radius</th><th>Area</th></tr>')
for radius in range(1,11):
area = math.pi * radius * radius
print('<tr><td>', radius, '</td><td>', area, '</td></tr>')
print('</table>')
print('</body>')
print('</html>')

After you copy the script2203.cgi file to the /usr/lib/cgi-bin folder, you can view it in
your browser to see the results. Figure 22.3 shows what you should see.


FIGURE 22.3 The script2203.cgi program output.

It’s amazing what just a little bit of HTML code can do to help with the output of your Python

Free download pdf