Created
October 11, 2018 03:33
-
-
Save kairyu/f66df625a02075f76ee0bf41e95efcfa 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 crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script> | |
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script> | |
""" | |
body = """ | |
<div id="root"></div> | |
<script> | |
class Data extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
data: [], | |
} | |
} | |
append(d) { | |
const data = this.state.data.slice() | |
data.push(d) | |
this.setState({data}) | |
} | |
render() { | |
return this.state.data.sort().map(d => | |
React.createElement('div', {key: d}, d) | |
) | |
} | |
} | |
const component = ReactDOM.render( | |
React.createElement(Data), | |
document.getElementById('root'), | |
) | |
</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>component.append({})</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