使用flask在html页面中编写脚本的输出

cha*_*hra 2 html python flask

我编写了一个脚本,使我能够运行脚本然后获得我想要的输出,我正在尝试将输出写入 html 页面,我该怎么做?这是我的脚本:

def execute(cmd):
    os.system(cmd)
    to_return =dict()
    for filename in files:
        with open(filename, 'r') as f:
            data = f.read()
            to_return[filename] = data
    return to_return

output = execute('./script')
print output
Run Code Online (Sandbox Code Playgroud)

关于如何生成 html 页面的任何想法,我可以在其中打印运行此脚本的结果?

小智 8

在你的views.py中,在相应的路由下,做

@app.route('/route_name')
def script_output():
    output = execute('./script')
    return render_template('template_name.html',output=output)
Run Code Online (Sandbox Code Playgroud)

在你的模板中,

<p>{{ output }}</p>
Run Code Online (Sandbox Code Playgroud)