Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To replace existing content on the page you might need javascript i.e., you could send it or make it to make requests for you, use long polling, websockets, etc. There are many ways to do it, here's one that uses <a href="http://dev.w3.org/html5/eventsource/" rel="noreferrer">server send events</a>:</p> <pre class="lang-py prettyprint-override"><code>#!/usr/bin/env python import itertools import time from flask import Flask, Response, redirect, request, url_for app = Flask(__name__) @app.route('/') def index(): if request.headers.get('accept') == 'text/event-stream': def events(): for i, c in enumerate(itertools.cycle('\|/-')): yield "data: %s %d\n\n" % (c, i) time.sleep(.1) # an artificial delay return Response(events(), content_type='text/event-stream') return redirect(url_for('static', filename='index.html')) if __name__ == "__main__": app.run(host='localhost', port=23423) </code></pre> <p>Where <code>static/index.html</code>:</p> <pre class="lang-html prettyprint-override"><code>&lt;!doctype html&gt; &lt;title&gt;Server Send Events Demo&lt;/title&gt; &lt;style&gt; #data { text-align: center; } &lt;/style&gt; &lt;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt; &lt;script&gt; if (!!window.EventSource) { var source = new EventSource('/'); source.onmessage = function(e) { $("#data").text(e.data); } } &lt;/script&gt; &lt;div id="data"&gt;nothing received yet&lt;/div&gt; </code></pre> <p>The browser reconnects by default in 3 seconds if the connection is lost. if there is nothing more to send the server could return 404 or just send some other than <code>'text/event-stream'</code> content type in response to the next request. To stop on the client side even if the server has more data you could call <code>source.close()</code>. </p> <p>Note: if the stream is not meant to be infinite then use other techniques (not SSE) e.g., send javascript snippets to replace the text (infinite <code>&lt;iframe&gt;</code> technique):</p> <pre class="lang-py prettyprint-override"><code>#!/usr/bin/env python import time from flask import Flask, Response app = Flask(__name__) @app.route('/') def index(): def g(): yield """&lt;!doctype html&gt; &lt;title&gt;Send javascript snippets demo&lt;/title&gt; &lt;style&gt; #data { text-align: center; } &lt;/style&gt; &lt;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt; &lt;div id="data"&gt;nothing received yet&lt;/div&gt; """ for i, c in enumerate("hello"): yield """ &lt;script&gt; $("#data").text("{i} {c}") &lt;/script&gt; """.format(i=i, c=c) time.sleep(1) # an artificial delay return Response(g()) if __name__ == "__main__": app.run(host='localhost', port=23423) </code></pre> <p>I've inlined the html here to show that there is nothing more to it (no magic). Here's the same as above but using templates:</p> <pre class="lang-py prettyprint-override"><code>#!/usr/bin/env python import time from flask import Flask, Response app = Flask(__name__) def stream_template(template_name, **context): # http://flask.pocoo.org/docs/patterns/streaming/#streaming-from-templates app.update_template_context(context) t = app.jinja_env.get_template(template_name) rv = t.stream(context) # uncomment if you don't need immediate reaction ##rv.enable_buffering(5) return rv @app.route('/') def index(): def g(): for i, c in enumerate("hello"*10): time.sleep(.1) # an artificial delay yield i, c return Response(stream_template('index.html', data=g())) if __name__ == "__main__": app.run(host='localhost', port=23423) </code></pre> <p>Where <code>templates/index.html</code>:</p> <pre class="lang-html prettyprint-override"><code>&lt;!doctype html&gt; &lt;title&gt;Send javascript with template demo&lt;/title&gt; &lt;style&gt; #data { text-align: center; } &lt;/style&gt; &lt;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt; &lt;div id="data"&gt;nothing received yet&lt;/div&gt; {% for i, c in data: %} &lt;script&gt; $("#data").text("{{ i }} {{ c }}") &lt;/script&gt; {% endfor %} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload