-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
flask_server.py
62 lines (47 loc) · 1.48 KB
/
flask_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'''This example demonstrates embedding an autoloaded Bokeh server
into a simple Flask application, and passing arguments to Bokeh.
To view the example, run:
python flask_server.py
in this directory, and navigate to:
http://localhost:5000
'''
import atexit
import subprocess
from flask import Flask, render_template_string
from bokeh.embed import server_document
home_html = """
<!DOCTYPE html>
<html lang="en">
<body>
<h1><a href="/batch/1"> Batch 1 (cos)</a></h1>
<h1><a href="/batch/2"> Batch 2 (sin)</a></h1>
<h1><a href="/batch/3"> Batch 3 (tan)</a></h1>
</body>
</html>
"""
app_html = """
<!DOCTYPE html>
<html lang="en">
<body>
<div>
<h2><a href="/batch/1">Batch 1 (cos)</a> - <a href="/batch/2">Batch 2 (sin)</a> - <a href="/batch/3">Batch 3 (tan)</a></h2>
</div>
{{ bokeh_script|safe }}
</body>
</html>
"""
app = Flask(__name__)
bokeh_process = subprocess.Popen(
['python', '-m', 'bokeh', 'serve', '--allow-websocket-origin=localhost:5000', 'bokeh_server.py'], stdout=subprocess.PIPE)
@atexit.register
def kill_server():
bokeh_process.kill()
@app.route('/')
def home():
return render_template_string(home_html)
@app.route('/batch/<int:batchid>')
def visualization(batchid):
bokeh_script = server_document(url='http://localhost:5006/bokeh_server', arguments=dict(batchid=batchid))
return render_template_string(app_html, bokeh_script=bokeh_script)
if __name__ == '__main__':
app.run(debug=True) # lgtm [py/flask-debug]