Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
flask show while calculating
#1
hello,

i am wondering if flask has the ability to render the template already halfway trough the calculation
following is example code

@app.route('/')
def my_form():

    return render_template("interface.html")



@app.route('/', methods=['POST'])
def my_form_post():

    time.sleep(5)
    result = "1"
    render_template("interface.html", result=result)
    time.sleep(5)
    result = "2"
    render_template("interface.html", result=result)
    time.sleep(5)
    result = "3"
    return render_template("interface.html", result=result)
i cannot find online how to make this happen
thanks for any help
Reply
#2
i would like to make a bump on this subject

does somebody has an idea if it is possible?

until now i wasnt watching into other platforms than flask
maybe django or others can help me out

havent got time to take a look into django, but would like to know if it is possible before I take the effort

thanks in advance guys
Reply
#3
Why do you want to do that? What are you actually trying to achieve? Remember that HTTP is request/response - the client always has to make a request, rather than have data pushed to it. If you want bidirectional communication, i.e. to be able to push data from the server to the client, then look into WebSockets.
Reply
#4
I agree with @ndc85430 that need more information about the task.
Usually you don't want template to be rendered each time result will be updated.
Can update result with eg AJAX,then do not need to reloading/render template each time.
Other way Flask-SocketIO with socket.io on the client side,if need a more advance bidirectional communication.

Look at this post.
So there i am using AJAX to get value from server that eg could be your result every 10-sec.
Reply
#5
i will take a look at the given info, thanks for your efforts guys...

i am scraping multiple webshops and give the data back to an page produced by flask

the scraping takes some time
the more webshops i scrape, the more the user has to wait

it would be nice to show the result of the first webshop already when finished scraping the first webshop,

once 2nd webshop is done, ... (adding the extra results to the page)

and 3th ....
Reply
#6
Here is an other post you can look at.
So here is doing scheduling on the Flask server with APScheduler.
Last in post also demo with DB flask_sqlalchemy(what you always should use DB connection in Flask).

Both of solution i posted here could work for what you doing.
Just write a result check eg if webshop2 is done: then ready to send to page.
The scheduling just work at interval you set,the if a webshop is finish on next scheduling time it will send to page,if not finish send nothing.
Reply
#7
hello all,

i found this evening the time to check tis problem out ---- finally ....

in the example there is added a <script> to the HTML page
if i am correct, this script takes care of refressing every 15seconds

within the python code there is an scheduler job who reruns the calculation every 15 seconds

the problem is that 1 run of my code takes about 80-100 seconds
and i want to update my data every 10 seconds

if I split my runs in different methods, for each of my 4 webshops one method, then ....

thats where i get lost, i havent found the clue how to send the data 4 times to the HTML file


i wish it made sense to share my code with you guys
but i cannot give you acces to all 4 of the webshops of my suppliers.
Reply
#8
You don't send data to HTML files. If the server is doing these calculations, the results need to be sent to the client (i.e. the browser). The browser can make an AJAX request to ask for that data (so will need to ask repeatedly for updates), or you can have the server push the data to the client with WebSockets or socket.io for example. As I said earlier, HTTP is request/response, so you'll have to poll the server unless you want to look at WebSockets/socket.io.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  show csv file in flask template.html rr28rizal 8 34,523 Apr-12-2021, 09:24 AM
Last Post: adamabusamra

Forum Jump:

User Panel Messages

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