lundi 29 janvier 2018

Python Flask updating file in background

So I have 3 files, api.py, build.py and data.json

My problem is that when I update the json file via api.py and try to load a page it won't be adjusted to the new and updated data.

Example: Json contains a field which is 0 or 1, if I change one to the other nothing happens, however if I go in and manually update the build.py file and reload the page it works(etc change a string in the 'build_header()' function below).

Here is a short example of my code:

api.py:

@app.route('/')
def api_root():
    build.build_page('index')
    return render_template('index.html')

build.py:

def build_page(page):
    with open('data.json') as json_data:
        data = json.load(json_data)
    if page === 'index':
        header = build_header()
        body = build_body(data)

The json file is updated in a separate thread, which isn't relevant here afaik.

When I make a HTTP GET request I get the right codes 200 if changed and 304 if not changed for data.json.

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire