Answer the question
In order to leave comments, you need to log in
How to get progress data from flask for Progress Bar?
Hello.
I've searched all over the internet, but either I'm not formulating the question correctly, or I'm just too stupid to miss it.
In general, the essence of the issue is this. There is a flask application, I need to make an indication of the execution of requests on the pages. I have no question how to make it layout and make it move, blink, glow, etc. through js.
But I can’t understand how I can do this logic:
1) The user fills in the form data
2) A post request flies to the backend via JS
3) The backend receives the data and starts executing the my_func1 function, where it already starts processing the data received from the web, walking for different databases, collect new data, build a bunch of useful information from all this, which it will then display on the page.
4) While the back is busy executing the my_func1 function, a progress bar should appear on the page, which will display the progress of the function.
So now I have it implemented as follows:
After the user clicks on the "Send data" button, the entire page is darkened and I just show the GIF with a spinning strip until the response from the back comes.
But this process can be quite lengthy and sometimes the user thinks that something is broken.
And if you show a normal progress bar that fills up as the function executes, it will be better.
But I can’t understand how I can get the process of executing the my_func1 function from the back ...
I hope I explained it normally and intelligibly. If something is not clear - ask, I will clarify.
Answer the question
In order to leave comments, you need to log in
Web applications don't work that way. The handler should return a response as quickly as possible. To perform long-running operations, you need to start background processes using something like Celery. The progress of their execution could be given via websocket, but synchronous Flask is not suitable for this. We'll have to do with polling through AJAX.
You need to create a child process with an ID and return the ID, then make periodic requests with the ID and get the state of that process from it.
You can do this:
1. Connect libraries at the beginning of the page:
<script src="../static/js/bootstrap.js"></script>
<script src="../static/js/bootstrap.bundle.js"></script>
<script src="../static/js/bootstrap.bundle.min.js"></script>
<script src="../static/jquery/jquery-3.6.0.js"></script>
<div class="progress" style="height: 23px;">
<div id="r3c1" class="progress-bar progress-bar-striped progress-bar-animated bg-info" role="progressbar" style="width: 0%">0</div>
</div>
<script>
setInterval(
function()
{
$.getJSON('/get_form',{},function(data){
$("#r3c1").text(data.result[2][0]);
$("#r3c1").css({'width':data.result[2][0]+'%'});
});
},
500);
</script>
@app.route('/get_form')
def get_form():
pass
return jsonify(result=data_form)
def my_func(pause):
percent = 100 / pause
while pause != 0:
time.sleep(1)
pause -= 1
data_form[2][0] = int( 100 - (pause * percent))
return
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question