![]() |
VOOZH | about |
Bokeh is a powerful Python library for creating interactive visualizations for modern web browsers. It allows for the creation of a wide variety of plots and dashboards that can be easily embedded into web applications. One of the key features of Bokeh is its ability to create live, interactive plots that can be updated in real-time. To achieve this, Bokeh provides a function called curdoc(), which plays a crucial role in building interactive applications.
Table of Content
The curdoc() function in Bokeh is used to retrieve the current Document object. A Document in Bokeh is an essential component that holds all the models, plots, widgets, and layouts that make up the contents of a Bokeh application. Essentially, the Document is the container that encapsulates everything you want to display in your Bokeh server application.
When you create a Bokeh application, you interact with the Document to manage and update the elements that make up the user interface. The curdoc() function provides access to this current document, enabling you to modify it programmatically.
curdoc() in Bokeh ApplicationsWhen building Bokeh applications, curdoc() plays a pivotal role in managing the document lifecycle. Here's how it functions within the Bokeh ecosystem:
curdoc() provides access to this document, allowing developers to manipulate it by adding or removing models.curdoc() is central to this synchronization, ensuring that any changes made on the server side are reflected in the client-side view.curdoc() is used to access the document for the current session, allowing the application code to modify the document as neededcurdoc() in BokehTo understand the practical application of curdoc(), let's explore a typical workflow in a Bokeh server application:
Here are some of the primary uses of curdoc() in Bokeh:
In Bokeh, all visual elements (plots, widgets, layouts) must be added to the document to be displayed in a web application. You do this by appending them to the document's list of "roots."
For example:
Output:
Bokeh applications often involve interactive elements like sliders, buttons, or dropdown menus. These elements can trigger callbacks, which are functions that execute in response to user interactions. By using curdoc(), you can add these callbacks to the document, ensuring they are properly managed and executed within the application context.
Output:
Once the document is set up, the Bokeh server can be run to serve the application. The curdoc() function ensures that the document is correctly managed and synchronized during the application's lifecycle.
Bokeh's curdoc() is often used in server applications that require real-time updates to plots or data. For instance, you might have a streaming data source or a background process that periodically updates the plot. Beyond basic usage, curdoc() supports more advanced operations that enhance the functionality of Bokeh applications:
curdoc() to create dynamic, time-based updates.Output:
Unfortunately, periodic callbacks require a Bokeh server, which Colab does not support.
curdoc()When working with curdoc() in Bokeh, consider the following best practices:
The curdoc() function is a fundamental component of Bokeh's server architecture, enabling the creation of interactive and dynamic visualizations. By managing the current document, it facilitates synchronization between the server and client, ensuring that applications remain responsive and up-to-date. Understanding how to effectively use curdoc() is essential for anyone looking to leverage Bokeh's full potential in building sophisticated data visualization applications.