'How to change the data send on Flask? [duplicate]
I run thin code.
And I want that after 20 seconds that the variable msg will get the value "hello msg2".
And if I refresh the page I see there "hello msg2" instead of "msg1".
from flask import Flask, render_template
import time
mag = "msg1"
app = Flask(__name__)
@app.route("/")
def home():
return render_template("home.html")
@app.route("/index.html")
def user():
return render_template("index.html", msg=msg)
if __name__ == "__main__":
app.run(port=7654, debug=True)
The index.html
<!doctype html>
<html>
<head>
<title>Home page</title>
</head>
<body>
<h1>Home Page!5 {{ msg }} </h1>
</body>
</html>
It is possible? Because I could not run any more commands in Python while the site was running.
Solution 1:[1]
You can use threading to spawn up a background thread to take care of updating value of msg. Use time.sleep() to add a delay to the execution.
from flask import Flask, render_template
import time
import threading
msg = "msg1"
# utility to change msg variable
def change_msg(delay=20):
time.sleep(delay)
global msg
msg = "msg2"
app = Flask(__name__)
@app.route("/")
def home():
return render_template("home.html")
@app.route("/index.html")
def user():
return render_template("index.html", msg=msg)
if __name__ == "__main__":
# spawn a thread that changes value of msg after a delay
t = threading.Thread(target=change_msg)
t.start()
app.run(port=7654, debug=True)
Solution 2:[2]
You'll need some Javascript. The process you're looking at is
Load the page which makes a call to your server (python)
Check for the presence of a cookie which tells you the page has been loaded before. If this cookie is not present, you return "msg1" and set a cookie letting you know you've returned "msg1". If the cookie is present, then you return "hello msg2" and you don't have to set the cookie again.
This part is where Javascript comes in - After 20 seconds, make another call to your server. You can do this asynchronously so that your page is not reloaded. The cookie will be sent along and step 2 above comes into play.
Solution 3:[3]
The easiest way to do what you asked for is a Timer. In your case the content of some_function() should be in your handler function user().
from threading import Timer
from time import sleep
msg = "msg1"
def setMessage():
global msg
msg = "msg2"
print("Message has changed")
def some_function():
global msg
timer = Timer(1.0, setMessage)
timer.start()
print(msg)
some_function()
sleep(3)
some_function()
Expected output:
msg1
Message has changed
msg2
Message has changed
Note:
setMessage()is called twice here, but you could check if the message is alreadymsg2before starting the timer to prevent this from happening.
If what you actually want is concurrency or parallelism you should have a look at Python Threading or for asynchronous programming at asyncio.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | NoCommandLine |
| Solution 3 |
