title: How to redirect when server-sent-event is finished in Flask on server side?
body: <h2>Question</h2>
  
  <p>How can one redirect to another page when server-sent-event is finished in Flask and on server side?</p>
  
  <h3>Problem description</h3>
  
  <p>An EventSource was implemented on client side code (JavaScript) and a Response was returned on server side code (Flask, Python). The EventSource will be closed if the last item from server was sent. I can't see a clear solution to redirect to another site via server side code. Maybe the answer is simple, but I'm not getting it.</p>
  
  <h3>Similar problem</h3>
  
  <p>A similar question is <a href="https://stackoverflow.com/questions/30645664/how-to-stop-server-sent-events">How to stop Server-Sent Events</a> and one possible answer in the comment <a href="https://stackoverflow.com/questions/7636165/how-do-server-sent-events-actually-work/11998868#11998868">How do server-sent events actually work?</a>. I'm not sure, if it's the same for php and python, so I've started a new question for that. I also didn't get it.</p>
  
  <p><strong>EDIT</strong> <a href="https://stackoverflow.com/questions/11597367/how-do-i-close-a-server-send-events-connection-in-flask">How do I close a Server-Send Events connection in Flask?</a> is strongly related to this question. The question there is mainly to see how you can stop SSE from Flask.</p>
  
  <h2>Working solution on client side</h2>
  
  <p>So I came up with a solution that worked (a progress bar), but only on the client side. How do I have to change the code to get a working example of reconnecting via Flask functions?</p>
  
  <h3>Relevant code snippets</h3>
  
  <p>HTML/Jinja</p>
  
  <pre><code>{% block additional_stylesheets %}
      &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"&gt;
      &lt;link rel="stylesheet" href="{{ url_for('static', filename='data_import.css') }}" /&gt;
  {%  endblock %}
  {% block additional_javascripts %}
      &lt;script src="{{ url_for('static', filename='js/data_import.js') }}"&gt;&lt;/script&gt;
  {% endblock %}
  {% block content %}
      &lt;div class="progress"&gt;
          &lt;div class="progress-bar progress-bar-striped active"
               role="progressbar" aria-valuenow="0" aria-valuemin="0"
               aria-valuemax="100"&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  {% endblock %}
  </code></pre>
  
  <p>JavaScript</p>
  
  <pre><code>$(document).ready(function()
      {
          var source = new EventSource("/progress");
  
          source.addEventListener('import-progress', function(event)
              {
                  $('.progress-bar')
                      .css('width', event.data + '%')
                      .attr('aria-valuenow', event.data);
              }, false
          );
  
          source.addEventListener('last-item', function()
              {
                  source.close();
                  redirect();
              }, false
          );
      }
  );
  
  # This works! But how to do the same thing without calling redirect()
  # on client site?
  function redirect()
  {
      window.document.location.href = window.location.protocol + "//" +
              window.location.host + "/products.html";
  }
  </code></pre>
  
  <p>Flask</p>
  
  <pre><code>from foo import app
  from flask import render_template, Response
  
  @app.route('/data_import.html')
  def data_import():
      return render_template(
          'data_import.html')
  
  
  @app.route('/progress')
  def progress():
      return Response(import_progress(), mimetype='text/event-stream')
  
  def import_progress():
      """
      Just a small example
      """
      for number in range(1, 101):
          sse_id = str(number)
          sse_progress = str(number)
          sse_event = 'import-progress'
  
          if number == 100:
              sse_event = 'last-item'
  
          yield "id:{_id}\nevent:{event}\ndata:{progress}\n\n".format(
              _id=sse_id, event=sse_event, progress=sse_progress)
  </code></pre>
  
  <p>I tried a lot to get a redirect working. But I don't know exactly how to do it. Every attempt so far has failed.</p>