2

テーブルに画像(プロット)があるページを返すcherryPyプログラムがあります。また、プロットを説明する変数をテーブルに入れたいと思います。本当にシンプルにしようとしているだけのテンプレートは使用していません。以下の例では、必要な場所に変数numberofapplicantsがありますが、テーブルに変数の値が出力されません。私はこれを行う方法の例を見つけることができませんでした。助けてくれてありがとう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>
    '''
4

2 に答える 2

3

Python 2.x を使用していると仮定すると、通常の文字列フォーマットを使用するだけです。

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}
于 2009-10-23T01:53:50.930 に答える
2

変数 'numberofapplicants' は、Python にとって残りの文字列のように見えます。'numberofapplicants' をその場所に入れたい場合は、% 構文を使用します。

 return '''big long string of stuff...
           and on...
           <td>%i</td>
           and done.''' % numberofapplicants
于 2009-10-23T01:54:52.457 に答える