Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
Flask - How to send a value to JavaScript function during Python script to update Progress Bar?

I have as an example, two scripts:

  • home.html
  • views.py

views.py has a function in it with various tasks e.g.:

def home()
    list1 = [1, 2, 3, 4, 5]
    total1 = 0
    for x in list1:
        total1 += x

    list2 = [10, 20, 30, 40, 50]
    total2 = 0
    for x in list2:
        total2 += x

return render_template("home.html")

home.html has a JavaScript function in it which updates a html/css progressbar depending on what the variable 'value' is:

<script>
    function updateProgressBar(progressBar, value) {
        value = Math.round(value);
        progressBar.querySelector(".progress__fill").style.width = `${value}%`;
        progressBar.querySelector(".progress__text").textContent = `${value}%`;
    }
</script>

The python function can vary in completion time. As a way of updating the user of the progress, after every task (i.e. total1 complete, total2 complete) is done in home(), I want to send a value to the JavaScript function so it can update the progress bar on home.html e.g. if there were 5 tasks in home(), it might send the value 20 after task 1, then 40 after task 2 etc.

I am new to 'web development' so struggling to do this and would appreciate any help.

Thank you in advance.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

One possible solution is to send the data from flask:

# ...
return render_template("home.html", **{
    'total1': total1,
    'total2': total2
})

Then add two hidden elements in the html you need to use the variables:

<p id="total1" style="display:none;">{{ total1 }}</p>
<p id="total2" style="display:none;">{{ total2 }}</p>

Then use JavaScript to load in those element's data:

const total1 = document.getElementById('total1');
const total2 = document.getElementById('total2');
// ...

Then you are free to use total1 and total2 in your JavaScript file.

A better solution would be if the JavaScript file you have would make an AJAX request towards your flask server, where the server could have a separate function to handle the request and send the data.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!