0

I want to constantly update my list stuff by appending more "stuff" in it. However, my list is not updating (preferably every second, but I don't know how to do a while loop inside flask).

Here is my routes.py :

from flask import render_template
from app import app
from win32gui import GetWindowText, GetForegroundWindow

@app.route('/')
@app.route('/index')
def index():
    user = {'username': 'Miguel'}
    stuff = []
    stuff.append(GetWindowText(GetForegroundWindow()))
    return render_template('index.html', title='Home', user=user, stuff = stuff)

if __name__ == "__main__":
    app.run(debug=True)

Here is my index.html :

<html>
    <head>
        {% if title %}
        <title>{{ title }} - Microblog</title>
        {% else %}
        <title>Welcome to Microblog!</title>
        {% endif %}
    </head>
    <body>
        <h1>Hello, {{ user.username }}!</h1>
        <h1>Here: </h1>
        {% for item in stuff %}
            <p> {{ item }} </p>
        {% endfor %}
    </body>
</html>

When I flask run, there is only ever one item in the list. How do I let the program know I want to continue adding more items? I would like to achieve this in the index() function.

Thank you for your help!

Rebecca Bibye
  • 190
  • 2
  • 18

1 Answers1

2

Every time your index method is called, the local variable stuff gets re-initialised to an empty list and then you append an element to it. That's why every time you refresh the page, you only see this one newly added element in stuff.

Consider making stuff global and then add items to it:

from flask import render_template
from app import app
from win32gui import GetWindowText, GetForegroundWindow

stuff = []

@app.route('/')
@app.route('/index')
def index():
    global stuff
    user = {'username': 'Miguel'}
    stuff.append(GetWindowText(GetForegroundWindow()))
    return render_template('index.html', title='Home', user=user, stuff = stuff)

if __name__ == "__main__":
    app.run(debug=True)

Or store global variables in a more better way.

Jarvis
  • 8,494
  • 3
  • 27
  • 58
  • 1
    Thank you so much this was very helpful!! – Rebecca Bibye Dec 22 '20 at 18:00
  • 1
    I've been thinking, and how do I have it append info every second, and not just every time I refresh the page? @Jarvis – Rebecca Bibye Dec 22 '20 at 18:06
  • 1
    You could simply put the append inside `while True`, then `time.sleep(1)` and then append the element into stuff there. But since you want to increment it every second, you should consider moving to some other function since it's no longer related to `index` anymore then. You could spawn a thread (daemon=True) in the function before which will run the infinite while loop, sleep for 1 second and then append the element into the list. – Jarvis Dec 22 '20 at 18:09
  • 1
    Thank you for replying! so should I have another function with the While loop, and then call that function inside index? Would that work since the while loop might go on forever and not return any values? – Rebecca Bibye Dec 22 '20 at 18:12
  • 1
    That won't, that's why this method should be called from a thread which would run as a daemon in background. This thread would call the method with the infinite while loop, you can spawn the thread in the main method itself. Have a look at Python threads documentation to get some idea about it. – Jarvis Dec 22 '20 at 18:14
  • 1
    That sounds very complicated. I will make sure to look at the documentation. Thanks for giving me an idea to start on! – Rebecca Bibye Dec 22 '20 at 18:16
  • 1
    [This](https://stackoverflow.com/questions/2223157/how-to-execute-a-function-asynchronously-every-60-seconds-in-python) can help. – Jarvis Dec 22 '20 at 18:17