0
votes

I'm currently running a few python script to handle some data which I then forward to a html file via the Flask framework.

The data is collected from a specific folder in my project. I would like to add a listener that re-run my scripts if the files are changed inside the specific data folder. I think I can manage the listener but I'm really confused with how I can update the information passed to the html through the flask app.

for example:

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

If the listener reacts, I want to update the passed information(data1, someOtherData). I have tried looking around but cant really get my head around how to make this work.

1

1 Answers

0
votes

You need to check if the file has changed. You can do it with a loop wich checks the file timestamp every x-seconds.

Or trigger it in the function who changes the data in your folder.

Or you make a loop in Javascript who runs the function.

EDIT:

Here is an example from my current project how to render template with new data:

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>