Created
October 11, 2018 05:15
-
-
Save kairyu/b16e3cf65dae44ceedbea650c5048633 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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