尝试使用Flask / Bokeh组合构建包含多个页面的仪表板。
我用于主文件(test.py
)的代码收集(导入)与仪表板页面对应的所有文件。
# test.py
from page1 import Page1
app = Flask(__name__)
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
class Dashboard(object):
def __init__(self, *pages):
self._pages = [page(self) for page in pages]
def __call__(self, doc):
tabs = []
for page in self._pages:
tabs.append(Panel(child=page.layout, title=page.title))
doc.add_root(Tabs(tabs=tabs))
bkapp = Application(FunctionHandler(Dashboard(Page1)))
目的是在自己的文件中定义仪表板的每个页面,最终将其导入主文件(test.py
)并由网络服务器(例如gunicorn)提供。
# page1.py from bokeh.models.widgets import Select
from bokeh.layouts import column, gridplot, widgetbox, layout, row
class Page(object):
title = "Override in subclass"
def __init__(self, dashboard):
self._dash = dashboard
self._layout = None
@property
def layout(self):
if self._layout is None:
self._layout = self._make_layout()
return self._layout
def _make_layout(self):
raise NotImplementedError("subclasses must define")
class Page1(Page):
title = "Page1"
def _make_layout(self):
self.sim_prod = Select(title="Selection:",
value="Yes",
options=["Yes", "No"]
)
self.x = row(self.sim_prod)
self.layout1 = layout([ [self.x] ], sizing_mode='scale_width')
return self.layout1
def some_callback(self, attr, old, new):
# to be defined later
pass
小部件(在本例中为Select
)构建并正确提供(参见图片),但在尝试重新加载页面时会产生错误:
错误:
raise RuntimeError("Models must be owned by only a single document, %r is already in a doc" % (self))
RuntimeError: Models must be owned by only a single document, WidgetBox(id='04f8b890-e3ca-48d9-954d-c33b96e80c78', ...) is already in a doc
ERROR:tornado.access:500 GET /bkapp/autoload.js?bokeh-autoload-element=8ca7bf9a-0c4f-462c-97f9-1f6b1a246628&bokeh-app-path=/bkapp&bokeh-absolute-url=http://127.0.0.1:60624/bkapp (127.0.0.1) 233.03ms
[2018-05-01 10:17:20 -0700] [94201] [DEBUG] GET /
任何建议如何解决它,非常感谢! 我使用的是Python 3.6和Bokeh 0.12.15。
干杯,
答案 0 :(得分:1)
如果其他人遇到同样的问题,更改layout
类的Page
方法,如下所示,每次重新加载页面时都会重新创建布局。
class Page(object):
title = "Override in subclass"
def __init__(self, dashboard):
self._dash = dashboard
self._layout = None
@property
def layout(self):
return self._make_layout()
def _make_layout(self):
raise NotImplementedError("subclasses must define")