-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserve.py
executable file
·91 lines (75 loc) · 2.92 KB
/
serve.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
import importlib
import logging
import os
from bokeh.application import Application
from bokeh.application.handlers import DirectoryHandler
from bokeh.model import Model
from bokeh.server.server import Server
from bokeh.server.views.static_handler import StaticHandler
from bokeh.util.compiler import bundle_models
from jinja2 import Environment, FileSystemLoader
from tornado.web import RequestHandler, StaticFileHandler
from app.config import (MENU_VARS, WS_ORIGIN, PREFIX, GA_TRACKING_ID,
CUSTOM_BOKEH_MODELS)
env = Environment(loader=FileSystemLoader('app/templates'))
def make_url(arg):
prefix = PREFIX.lstrip('/')
if not prefix:
return f'/{arg}'
elif arg:
return f'/{prefix}/{arg}'
else:
return f'/{prefix}'
class IndexHandler(RequestHandler):
def get(self):
template = env.get_template('app_index.html')
mv = [(a[0], make_url(a[1])) for a in MENU_VARS]
self.write(template.render(menu_vars=mv, prefix=PREFIX,
ga_tracking_id=GA_TRACKING_ID))
def compile_custom_models():
"""
Pre-compile the custom models so they don't need to be
recompiled on each page load
"""
models = []
for modspec, cm in CUSTOM_BOKEH_MODELS:
mod = importlib.import_module(modspec)
custom_model = getattr(mod, cm)
custom_model.__implementation__ = custom_model.__js_implementation__
models.append(custom_model)
js_code = bundle_models(models)
# must remove the model from the bokeh model map_fig
# since it has no __implementation__ and will be added
# in the app
model_map = Model.model_class_reverse_map
for _, cm in CUSTOM_BOKEH_MODELS:
if cm in model_map:
del model_map[cm]
return js_code
if __name__ == '__main__':
os.environ['BOKEH_RESOURCES'] = 'cdn'
loglevel = os.getenv('ADVI_LOGLEVEL', 'INFO')
logging.basicConfig(level=loglevel,
format='%(asctime)s %(message)s')
apps = {make_url(arg): Application(DirectoryHandler(filename='app',
argv=[arg]))
for _, arg in MENU_VARS}
extra_patterns = [(make_url('?'), IndexHandler),
(make_url('(favicon.ico)'),
StaticFileHandler,
{'path': "app/static"}),
(make_url('static/(.*)'),
StaticHandler)]
paths = [a[0] for a in extra_patterns]
paths.extend(list(apps.keys()))
logging.info('Running on localhost:5006 with paths:\n%s', '\n'.join(paths))
server = Server(apps,
allow_websocket_origin=[WS_ORIGIN],
use_xheaders=True,
extra_patterns=extra_patterns,
use_index=False,
redirect_root=False,
)
server.start()
server.io_loop.start()