ChesuCR
ChesuCR

Reputation: 9630

How to design a complex Bokeh Application?

I am programming a bokeh application. I want to split the functionalities into different files. But I want to have some attributes accesible from every class, these attributes should be shared and always updated. For example an attribute which stores a dataframe that all the plots are going to use. So I think I have at least two possible solutions:

Is there a better way to do this? Which option will bring me less problems?

Upvotes: 1

Views: 941

Answers (1)

ChesuCR
ChesuCR

Reputation: 9630

Finally I have created an my_bokeh_app folder. There I have an __init__.py file with this content for the initialization:

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

I have created a Class to share data among all the objects. This is the 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

In every class I make a reference to the BokehSharedData class. I also inherit from that class to access to the class variables.

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

        # [...]

And I could read the shared attributes or execute methods from other object:

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

The complete app where you can see all the classes working is here

Upvotes: 1

Related Questions