Forums

Flask app dynamic updating value won't show

Hey all. I got a flask application with a form and if you submit the form it's doing some processing. So I want to show the user the status of it by passing the status from flask over JSON to my HTML-Template. It works on my local machine and the text in the Button at the end of the form will start changing once you clicked on it. But it does not work here. Link: https://vatrox.pythonanywhere.com/

The corresponding of my Flask app:

@app.route('/_stuff', methods=['GET'])
def stuff():
    return jsonify(result=status_list[-1])

Script in my index.html:

var intervalID = setInterval(update_values, 1000);
$SCRIPT_ROOT = {{ request.script_root|tojson|safe }};

function update_values() {
    $.getJSON($SCRIPT_ROOT + '/_stuff',

    function(data) {
        $('#status').text(data.result);
        if ($('#status').text() !== 'Make Magic') {
            $('#status').addClass('passive');
            console.log($('#status').text())
        }
    });
};

I can see all the requests in the access.log but the value isn't changing in my HTML. As I said, I tried this with the exact same code on my local machine and it worked. So I was curious if this is just a pythonanywhere-thing. Thanks in advance!

Is status_list a global variable? On production environments like PythonAnywhere, you will have more than one process running to handle requests to your site, and different requests -- even from the same browser -- will be allocated to them at random. So you can't use global variables to hold state; the database is the place for that.

It's a global variable, yes. So there is no other possibility to solve this issue than using a database? Because I'm really not a pro in these and most likely don't get them to work ^^

The database is definitely the right way to go -- it will take a while to work out how to do it, but it's worth it, I promise :-)

You might find our Flask and MySQL tutorial is a good starting point -- you can create another account, just a free one, and then go through it there, and that should demystify quite a lot of it.