Python flask应用程序事件处理程序

Python flask应用程序事件处理程序,python,flask,Python,Flask,我目前正在运行一些python脚本来处理一些数据,然后通过Flask框架将这些数据转发到html文件 数据是从我的项目中的特定文件夹收集的。我想添加一个侦听器,如果特定数据文件夹中的文件发生更改,它将重新运行我的脚本。我想我可以管理侦听器,但我真的很困惑如何通过flask应用程序更新传递到html的信息 例如: @app.route("/") def chart(): return render_template("chart.html", data1=data1, someOt

我目前正在运行一些python脚本来处理一些数据,然后通过Flask框架将这些数据转发到html文件

数据是从我的项目中的特定文件夹收集的。我想添加一个侦听器,如果特定数据文件夹中的文件发生更改,它将重新运行我的脚本。我想我可以管理侦听器,但我真的很困惑如何通过flask应用程序更新传递到html的信息

例如:

@app.route("/")
def chart():
    return render_template("chart.html",
    data1=data1, someOtherData=someOtherData)

如果侦听器有反应,我想更新传递的信息(
data1
someOtherData
)。我试过四处看看,但我真的不知道该怎么做。

您需要检查文件是否已更改。您可以使用循环来完成此操作,循环每x秒检查一次文件时间戳

或者在更改文件夹中数据的函数中触发它

或者在Javascript中创建一个运行函数的循环

编辑:

以下是我当前项目中的一个示例,说明如何使用新数据呈现模板:

prog.py

@app.route('/show_all_persons', methods=['GET'])
@login_required
def show_all_persons():
    all_persons_query = '''
    SELECT 
        *
    FROM 
        persons
    '''

    persons = sql_conn.query(all_persons_query)
    persons = [dict(p) for p in persons]

    return render_template('childs/show_all_persons.html', persons=persons)
person.html

<table class="table table-light table-hover sort_list">
<thead>
<tr>
    <th style="width: 10em;" scope="col">ID</th>
    <th style="width: 10em;" scope="col">Vorname</th>
    <th id="lastname" scope="col">Nachname</th>
</tr>
</thead>
<tbody class="table-hover">

{% for person in persons %}
<tr class="click" data-href="/show_person/{{ person.person_id }}">
    <td scope="row"><b>#{{ person.person_id }}</b></td>
    <td>{{ person.person_firstname }}</td>
    <td>{{ person.person_lastname }}</td>
</tr>

{% endfor %}

</tbody>

身份证件
沃名称
姓氏
{人中人的百分比%}
#{{person.person_id}
{{person.person_firstname}
{{person.person_lastname}
{%endfor%}

我知道如何添加侦听器来检查文件本身是否已更改。但是我不知道如何将通过Flask框架传递的信息更新到我的html代码中。我不知道您的数据1和其他一些数据中有什么。如果data1不是数组或列表或。。你可以把它放在你的chart.html中你想要的数据:{%data1%}请阅读,如果你想亲自更新你的信息,html会用你的例子自我更新吗?目前,数据由两个列表和一些浮点值组成。数据将在重新加载页面时更新。如果您只想更新值,您可以编写一个javascript文件,将请求发送到一个返回json.Okey的链接,但是如果我只有本地信息,并且没有可能重新加载页面,因为脚本将在电视上运行。我可以写一个javascript文件自动重新加载页面吗?可能作为文件侦听器函数的最后一部分?谢谢你的帮助!我第一次使用flash,甚至写html!