title: Flask App: Update progress bar while function runs
body: <p>I'm building a fairly simple WebApp in Flask that performs functions via a website's API.  My users fill out a form with their account URL and API token; when they submit the form I have a python script that exports PDFs from their account via the API. This function can take a long time so I want to display a bootstrap progress bar on the form page indicating how far along in the process the script is.  My question is how to I update the progress bar as the function is running?  Here is a simplified version of what I'm talking about.</p>
  
  <p>views.py:</p>
  
  <pre><code>@app.route ('/export_pdf', methods = ['GET', 'POST'])
  def export_pdf():
      form = ExportPDF()
      if form.validate_on_submit():
        try:
          export_pdfs.main_program(form.account_url.data,
            form.api_token.data)
          flash ('PDFs exported')
          return redirect(url_for('export_pdf'))
        except TransportException as e:
          s = e.content
          result = re.search('&lt;error&gt;(.*)&lt;/error&gt;', s)
          flash('There was an authentication error: ' + result.group(1))
        except FailedRequest as e:
          flash('There was an error: ' + e.error)
      return render_template('export_pdf.html', title = 'Export PDFs', form = form)
  </code></pre>
  
  <p>export_pdf.html:</p>
  
  <pre><code>{% extends "base.html" %}
  
  {% block content %}
  {% include 'flash.html' %}
  &lt;div class="well well-sm"&gt;
    &lt;h3&gt;Export PDFs&lt;/h3&gt;
    &lt;form class="navbar-form navbar-left" action="" method ="post" name="receipt"&gt;
      {{form.hidden_tag()}}
      &lt;br&gt;
      &lt;div class="control-group{% if form.errors.account_url %} error{% endif %}"&gt;
        &lt;label class"control-label" for="account_url"&gt;Enter Account URL:&lt;/label&gt;
        &lt;div class="controls"&gt;
          {{ form.account_url(size = 50, class = "span4")}}
          {% for error in form.errors.account_url %}
            &lt;span class="help-inline"&gt;[{{error}}]&lt;/span&gt;&lt;br&gt;
          {% endfor %}
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;br&gt;
      &lt;div class="control-group{% if form.errors.api_token %} error{% endif %}"&gt;
        &lt;label class"control-label" for="api_token"&gt;Enter API Token:&lt;/label&gt;
        &lt;div class="controls"&gt;
          {{ form.api_token(size = 50, class = "span4")}}
          {% for error in form.errors.api_token %}
            &lt;span class="help-inline"&gt;[{{error}}]&lt;/span&gt;&lt;br&gt;
          {% endfor %}
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;br&gt;
      &lt;button type="submit" class="btn btn-primary btn-lg"&gt;Submit&lt;/button&gt;
    &lt;br&gt;
    &lt;br&gt;
    &lt;div class="progress progress-striped active"&gt;
    &lt;div class="progress-bar"  role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%"&gt;
      &lt;span class="sr-only"&gt;&lt;/span&gt;
    &lt;/div&gt;
  &lt;/form&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  {% endblock %}
  </code></pre>
  
  <p>and export_pdfs.py:</p>
  
  <pre><code>def main_program(url, token):
      api_caller = api.TokenClient(url, token)
      path = os.path.expanduser('~/Desktop/'+url+'_pdfs/')
      pdfs = list_all(api_caller.pdf.list, 'pdf')
      total = 0
      count = 1
      for pdf in pdfs:
          total = total + 1
      for pdf in pdfs:
          header, body = api_caller.getPDF(pdf_id=int(pdf.pdf_id))
          with open('%s.pdf' % (pdf.number), 'wb') as f:
            f.write(body)
          count = count + 1
          if count % 50 == 0:
            time.sleep(1)
  </code></pre>
  
  <p>In that last function I have total the number of PDFs I will export, and have an ongoing count while it is processing.  How can I send the current progress to my .html file to fit within the 'style=' tag of the progress bar?  Preferably in a way that I can reuse the same tool for progress bars on other pages.  Let me know if I haven't provided enough info.</p>