Python Flask-尝试在HTML文件中运行SQL查询

Python Flask-尝试在HTML文件中运行SQL查询,python,mysql,python-2.7,flask,Python,Mysql,Python 2.7,Flask,我正在尝试在头文件中运行SQL查询。 SQL查询是: cur.execute("select count(*) from registered_users") number_of_users = cur.fetchone()[0] 头文件名为“header.html” HTML代码: <div class="snowball"> Registered users: {{ "{:,}".format(number_of_users) }} </div> 我希望在所有页面

我正在尝试在头文件中运行SQL查询。 SQL查询是:

cur.execute("select count(*) from registered_users")
number_of_users = cur.fetchone()[0]
头文件名为“header.html”

HTML代码:

<div class="snowball">
Registered users: {{ "{:,}".format(number_of_users) }}
</div>

我希望在所有页面中都包含“header”文件,而不必在“website.com/header”上进行渲染。有没有更好的方法在烧瓶上进行此操作?实现这一点的正确方法是什么?

看看flask模板:这里有一个非常好的指南,您可以在所有html页面中包含头文件。然后您可以制作模板标记来检索header.htmldata@rajvi如果要在头html页面中运行代码(如SQL查询),该怎么办?您必须在应用程序python文件中写入app.route吗?@xiholac761您可以创建一个模板标记,然后当调用它时,会自动运行写入模板标记中的sql查询。
<div class="snowball">
Registered users: {{ "{:,}".format(number_of_users) }}
</div>
app.route("/"):
def index():
    render_template('index.html')