是否可以将数据从 Python 传输到 HTML template/Website(使用 Flask)

Is it possible to transfer data from Python to HTML template/Website (Using Flask)

我对这个话题一窍不通,这是我的第一个问题。 我想问一下是否可以上传一个变化的变量 从python到HTMLtextbox/paragraph/forms? 我见过几种方法,但无法找出一种有效的方法。 我在我的 raspberry PI 2 上使用 Python 3.4,我还将从中托管一个 Web 服务器 using flask . 我也更喜欢使用烧瓶来完成这项任务,但无论如何,如果不可能的话,我可以尽快使用任何其他方法,因为它们更容易。

提前致谢!

虽然这个问题很混乱,但据我了解你可以这样做

在 python 文件中:

    from flask import Flask,render_template
    app = Flask(__name__)
    @app.route('/')
    def index():
        name='Whosebug'
        return render_template('index.html', name=name)

并在 index.html 文件中:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <script>
        var name = {{ name|safe }}
    </script>
    <h2> this is passed from python {{ name }} </h2>
    </html>

注意:当您使用 render_template() 时,您在里面提到的 HTML 文件应该在 ./templates/ 中,在我们的例子中它应该是 ./templates/index.html