Last active
October 11, 2018 03:30
-
-
Save kairyu/3474de4f1d7a087ee5f0128337397439 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__) | |
script = """ | |
<script> | |
function insert(data) { | |
var item = document.createElement('div'); | |
item.setAttribute('class', 'data') | |
item.setAttribute('id', data) | |
item.textContent = data | |
var elements = document.getElementsByClassName('data'); | |
for (var i = 0; i < elements.length; i++) { | |
var id = elements[i].getAttribute('id'); | |
if (data <= id) { | |
document.body.insertBefore(item, elements[i]); | |
return; | |
} | |
} | |
document.body.appendChild(item); | |
} | |
</script> | |
""" | |
@app.route('/') | |
def index(): | |
data = list(range(10)) | |
random.shuffle(data) | |
def generate(): | |
yield '<html><head>{}</head><body>'.format(script) | |
for d in data: | |
time.sleep(1) | |
yield '<script>insert({})</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