Skip to content

Instantly share code, notes, and snippets.

@kairyu
Created October 11, 2018 05:15
Show Gist options
  • Save kairyu/b16e3cf65dae44ceedbea650c5048633 to your computer and use it in GitHub Desktop.
Save kairyu/b16e3cf65dae44ceedbea650c5048633 to your computer and use it in GitHub Desktop.
import time
import random
from flask import Flask, Response, stream_with_context
app = Flask(__name__)
head = """
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
"""
body = """
<div id="app" v-cloak>
<div v-for="d in sorted_data">
{{ d }}
</div>
</div>
<script>
var app = new Vue({
el: '#app',
data: {
data: [],
},
computed: {
sorted_data: function () {
return this.data.sort()
},
},
})
</script>
"""
@app.route('/')
def index():
data = list(range(10))
random.shuffle(data)
def generate():
yield '<html><head>{}</head><body>'.format(head)
yield body
for d in data:
time.sleep(1)
yield '<script>app.data.push({})</script>'.format(d)
yield '</body></html>'
return Response(stream_with_context(generate()))
app.debug = True
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment