如何设计复杂的 Bokeh 应用程序?

How to design a complex Bokeh Application?

我正在编写散景应用程序。我想将功能拆分到不同的文件中。但我希望每个 class 都可以访问一些属性,这些属性应该共享并始终更新。例如,存储所有绘图将要使用的数据框的属性。所以我认为我至少有两种可能的解决方案:

有更好的方法吗?哪个选项会给我带来更少的问题?

我终于创建了一个 my_bokeh_app 文件夹。我有一个 __init__.py 文件,其中包含用于初始化的内容:

from my_bokeh_app.bokeh_data import BokehData
from my_bokeh_app.bokeh_plots import BokehPlots
from my_bokeh_app.bokeh_table import BokehDataTable
from my_bokeh_app.bokeh_events import BokehEvents
from my_bokeh_app.bokeh_layout import BokehLayout

BokehData()
BokehPlots()
BokehDataTable()
BokehEvents()
BokehLayout()

我创建了一个 Class 来在所有对象之间共享数据。这是 class:

class BokehSharedData(object):
    # ------------------- CLASS VARIABLES ---------------------- #
    # This variables are shared. So all the children can access them

    data_source = None

    bk_layout = None
    bk_data = None
    bk_plot = None
    bk_table = None
    bk_events = None

在每个 class 中,我都引用了 BokehSharedData class。我还继承了 class 以访问 class 变量。

from my_bokeh_app.bokeh_shared_data import BokehSharedData

class BokehData(BokehSharedData):
    def __init__(self, **kwargs):
        self.env = BokehSharedData
        self.env.bk_data = self

        # If for example I want to access to the source attribute from the rest of objects
        # I could make this shortcut on the shared class
        self.env.data_source = ColumnDataSource(...)

    def update_data_source(self):

        # [...]

而且我可以从其他对象读取共享属性或执行方法:

from my_bokeh_app.bokeh_shared_data import BokehSharedData

class BokehPlots(BokehSharedData):
    def __init__(self, **kwargs):
        self.env = BokehSharedData
        self.env.bk_plots = self

        # I could use self.env.data_source here or run some method of BokehData class like this

        self.env.bk_data.update_data_source()