What is the purpose of curdoc ()?

I have been playing with bokeh for a bit of time, and now I am at the stage where I would like to create interactive stories and embed them online (for example, in WordPress posts).

However, although I spent some time reviewing and testing the code with examples from the bokeh website, it is difficult for me to understand what the purpose of curdoc () is. It seems to be necessary to create a bokeh application with widgets, but from what I found in the resources, I do not quite understand it.

+6
source share
1 answer

Some terminology is fine. When you create a Bokeh story, it consists of many small objects, such as glyphs, ranges, data sources, tools, etc. And the plot itself is a container for all these objects. All of these objects, including the plot, are called models. Other models, such as layouts (e.g. row and column ), as well as built-in widgets for Bokeh (e.g. Slider or Select ).

For each of these models, on the Python side, there is a corresponding JavaScript object that actually implements the model and does all the work in the browser for drawing or interaction. The way Bokeh works is that it can automatically turn a collection of Python models into a bunch of JSON, and then BokehJS can recreate all the relevant JS objects from that.

The document is a collection of Bokeh models. This is the smallest serialization unit. That is, it often does not make sense to turn one Python model into JSON (for example, Plot ), because an object can refer to many other objects (for example, axes or glyphs, etc.). Thus, a collection of models in a Bokeh document can be turned into JSON together in a meaningful way.

All of the above applies regardless of whether you are executing stand-alone documents (for example, using output_file ) or creating Bokeh applications on a Bokeh server. In the case of Bokeh applications, the application code itself is just a recipe for modifying a document. Therefore, whenever a Bokeh application session is created (i.e., whenever a user opens the bokeh application URL on the Bokeh server), a new empty Document is created for him, and the application code is launched where a new Document for this session is available curdoc() . Then the application code can add things to this document (for example, using curdoc().add_root(...) , and as soon as the application code is completed, the "completed" document will be transferred to JSON and sent to BokehJS to show the user.

The last mention is that the main purpose of the Bokeh server is to make sure that the Bokeh documents created in this way are synchronized with the user view in the browser automatically. If the user smooths the slider that updates the Slider model in the JS document in the browser, it automatically updates the Python Slider object in the Python document on the Bokeh server. An application can respond to this and make other changes (possibly updating the Python data source), which forces the JS data source to update as well (and thus changing the graph). This automatic two-way synchronization is what provides all the complex interactive features with the Bokeh server.

+5
source

Source: https://habr.com/ru/post/1012257/


All Articles