Python Forum
Flask run function in background and auto refresh page
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flask run function in background and auto refresh page
#1
I'm trying to get a function to run in the background when accessing my flask application then update the content of the page when the function is completed. Could anyone give me some advice of how i can do this.

Here is an example of what i want to do:
app.py
from flask import Flask, render_template
from time import sleep
from random import random

app = Flask(__name__)

def dummy_function():
    sleep(5)
    return random()

@app.route('/')
def index():
    x = dummy_function() # run this i background and when it is done update the page

    return render_template('index.html', x=x) # I want this to render even if the dummy function isn't done

if __name__ == '__main__':
    app.run(debug=True)
index.html
<html>
    <body>
        <p>x is {{ x }}</p>
    </body>
</html>
Reply


Messages In This Thread
Flask run function in background and auto refresh page - by raossabe - Aug-17-2022, 11:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: list index out of range" & "TypeError: The view function f: Flask Web App joelbeater992 5 3,578 Aug-31-2021, 08:08 PM
Last Post: joelbeater992
  Reload flask current page GrahamL 2 5,193 Jan-08-2021, 08:31 AM
Last Post: GrahamL
  API auto-refresh on HTML page using Flask toc 2 11,922 Dec-23-2020, 02:00 PM
Last Post: toc
  how to pass javascript variables to url_for function in a flask template experimental 5 6,460 Oct-29-2020, 03:29 AM
Last Post: universe
  [Flask]After login page is not redirecting me to dashboard shockwave 0 2,735 May-07-2020, 05:22 PM
Last Post: shockwave
  Flask Create global response function to be called from every where in the web app umen 2 2,343 Apr-14-2020, 09:54 PM
Last Post: umen
  Flask - adding new page affects all other pages CMR 15 5,726 Mar-28-2020, 04:13 PM
Last Post: CMR
  use Xpath in Python :: libxml2 for a page-to-page skip-setting apollo 2 3,670 Mar-19-2020, 06:13 PM
Last Post: apollo
  flask-SQLAlchemy query with keyword as function argument pascale 2 3,526 Mar-13-2019, 08:45 PM
Last Post: Ecniv
  How do i scrape website whose page changes using javsacript _dopostback function and Prince_Bhatia 1 7,280 Aug-06-2018, 09:45 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020