Search code examples
pythoncherrypy

CherryPy variables in html


I have a cherryPy program that returns a page that has an image (plot) in a table. I would also like to have variables in the table that describe the plot. I am not using any templating just trying to keep it really simple. In the example below I have the variable numberofapplicants where I want it but it does not output the value of the variable in the table. I have not been able to find any examples of how to do this. Thanks for your help Vincent

 return '''
    <html>
    <body>
    <table width="400" border="1">
    <tr>
    <td>numberofapplicants</td>
    </tr>
    <tr>
    <td width="400" height="400"><img src="img/atest.png" width="400" height="400" /></td>
    </tr>
    </table>
    </body>
    </html>
    '''

Solution

  • Assuming you're using Python 2.x, just use regular string formatting.

    return '''
        <html>
        <body>
        <table width="400" border="1">
        <tr>
        <td>%(numberofapplicants)s</td>
        </tr>
        <tr>
        <td width="400" height="400"><img src="img/atest.png" width="400" height="400" /></td>
        </tr>
        </table>
        </body>
        </html>
        ''' % {"numberofapplicants": numberofapplicants}