Python动态生成html页面

Posted by Kerwen Blog on December 15, 2020

使用bottle的template生成 html页面,可以传入参数:

1
2
3
4
5
6
7
8
9
10
11
12
from bottle import template
def WriteHtmlReport(htmlPath):    
    template_demo="""
    <html>
        <body>
            Hi { {Author} }
        </body>
    </html>
    """
    htmlTemp = template(template_demo, Author="Test")
    with open(htmlPath, 'wb') as f:
        f.write(htmlTemp.encode('utf-8'))

支持for循环:

1
2
3
4
5
6
7
8
9
10
11
12
template_demo="""
<html>
    <body>
        % for file in commit.CommitFiles:
            { {file} }</br>
        %end
    </body>
</html>    
"""
htmlTemp = template(template_demo, commit=commit)
with open(htmlPath, 'wb') as f:
    f.write(htmlTemp.encode('utf-8'))

支持if语句:

1
2
3
4
5
6
7
8
9
10
template_demo="""
<html>
    <body>
        <font color={ {"green" if BuildResult=="SUCCESS" else "red"} }> { {BuildResult} }</font>
    </body>
</html>    
"""
htmlTemp = template(template_demo, BuildResult="SUCCESS")
with open(htmlPath, 'wb') as f:
    f.write(htmlTemp.encode('utf-8'))

Bottle API
Python利用bottle来动态生成本地html页面
https://www.cnblogs.com/fhkankan/p/12978622.html