An alternative approach.
Since you have a combination of HTML and Python code, I would suggest to separate the logical part and the part of the presentation , switching to the usual template engine .
An example of using mako .
Create an HTML template file, leaving the appropriate placeholders:
<html> <head> <title>${title}</title> </head> <body> <p>${text}</p> </body> </html>
Then, in python code, render the template containing the values for placehodlers:
from mako.template import Template t = Template(filename='index.html') print t.render(title='My Title', text='My Text')
This will print:
<html> <head> <title>My Title</title> </head> <body> <p>My Text</p> </body> </html>
source share