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

标签 python python-3.x oop bokeh

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

  • 使用大类并包含其他文件的属性和方法:

    class Bigclass(object):
    
        from bk_plots import p1, p2, p3
        from bk_data import d1, d2, d3
        from bk_layout import ly1, ly2
        from bk_events import ev1, ev2
        # unfortunately, "from classdefn import *" is an error or warning
    
        num = 42   # add more members here if you like
    

    注意:此解决方案复制自 here (partial classes)

  • 或者我可以使用继承。父级将具有共享属性。这个系统的好处是我需要将其余的对象引用发送到每个子类

    class Parent():
        shared = 'parent'
    
    class Plot(Parent):
        def __init__(self):
            Parent.shared = 'plots'   # update class variable from this class
                                    # I would need to have references to the objects of other classes
    
    class Data(Parent):
        def __init__(self):
            Parent.shared = 'second'
    
    # [...]
    

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

最佳答案

最后我创建了一个 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 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

在每个类中,我都会引用 BokehSharedData 类。我也从该类继承来访问类变量。

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()

完整的应用程序,您可以在其中看到所有类的工作情况是 here

关于python - 如何设计复杂的 Bokeh 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49150549/

相关文章:

python - 需要一种快速的方法来一次计算和求和一个迭代

python-3.x -\将字段内带双引号的 CSV 复制到表中

c++ - 尝试在 Vector 中查找内容时出错,不确定如何修复

scala - 特征混合与冲突成员 : Why my code can be compiled successfully?

c++ - 不了解如何为源/接收器类逻辑初始化指针

python - 在python中打印多个列表

python - 如何使用 Python 将此 XML 字符串转换为二进制形式?

python - 无法访问更新后的全局变量的值

python - 关于OpenCV resize's INTER_AREA working domain的问题(func != 0 && cn <= 4 in function 'cv::hal::resize' failure)

python - Django 服务器经常被杀死