Vincent
Vincent

Reputation: 1647

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>
    '''

Upvotes: 2

Views: 1833

Answers (2)

Nick T
Nick T

Reputation: 26717

Your variable 'numberofapplicants' just looks like the rest of the string to Python. If you want to put 'numberofapplicants' into that spot, use the % syntax, e.g.

 return '''big long string of stuff...
           and on...
           <td>%i</td>
           and done.''' % numberofapplicants

Upvotes: 2

pavpanchekha
pavpanchekha

Reputation: 2093

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}

Upvotes: 3

Related Questions