使用来自HTML代码的flask运行python脚本

时间:2017-10-12 22:01:53

标签: python html flask

我已经为烧瓶运行了这个代码,打开了一个名为“Home”的页面

from flask import Flask, render_template
logging, request

app = Flask(__name__)

@app.route('/')
def Home():
    return render_template('Home.html')

if __name__ == "__main__":
    app.run(debug = True)

在页面上,当我按下按钮时,我试图运行Code.py文件。我尝试使用

<a href={{"File Location"}}">Press this Button</a> 

但它在网站上返回错误,如何让它运行文件并输出到html页面

1 个答案:

答案 0 :(得分:1)

尝试添加另一个视图,您可以在其中调用文件并返回一些输出。

from flask import Flask, render_template

app = Flask(__name__)


@app.route('/')
def Home():
    return render_template('Home.html')


@app.route('/myview')
def myview():
    # Call your methods here and return other template
    return render_template('Home.html')


if __name__ == "__main__":
    app.run(debug=True)

模板/ Home.html中

<html>
  <head>
  </head>
  <body>
    <a href={{ url_for('myview') }}>myview</a>
  </body>
</html>
相关问题