bokeh.document

Provide the Document class, which is a container for Bokeh Models to be reflected to the client side BokehJS library.

As a concrete example, consider a column layout with Slider and Select widgets, and a plot with some tools, an axis and grid, and a glyph renderer for circles. A simplified representation oh this document might look like the figure below:

../../_images/document.svg

A Bokeh Document is a collection of Bokeh Models (e.g. plots, tools, glyphs, etc.) that can be serialized as a single collection.

class Document(**kwargs)[source]

The basic unit of serialization for Bokeh.

Document instances collect Bokeh models (e.g. plots, layouts, widgets, etc.) so that they may be reflected into the BokehJS client runtime. Because models may refer to other models (e.g., a plot has a list of renderers), it is not generally useful or meaningful to convert individual models to JSON. Accordingly, the Document is thus the smallest unit of serialization for Bokeh.

add(*objects)[source]

Call add_root() on each object.

Warning

This function should only be called on top level objects such as Plot, and Layout containers.

Parameters:*objects (Model) – objects to add to the Document
Returns:None
add_next_tick_callback(callback)[source]

Add callback to be invoked once on the next tick of the event loop.

Parameters:callback (callable) – A callback function to execute on the next tick.
Returns:NextTickCallback

Note

Next tick callbacks only work within the context of a Bokeh server session. This function will no effect when Bokeh outputs to standalone HTML or Jupyter notebook cells.

add_periodic_callback(callback, period_milliseconds)[source]

Add a callback to be invoked on a session periodically.

Parameters:
  • callback (callable) – A callback function to execute periodically
  • period_milliseconds (int) – Number of milliseconds between each callback execution.
Returns:

PeriodicCallback

Note

Periodic callbacks only work within the context of a Bokeh server session. This function will no effect when Bokeh outputs to standalone HTML or Jupyter notebook cells.

add_root(model, setter=None)[source]

Add a model as a root of this Document.

Any changes to this model (including to other models referred to by it) will trigger on_change callbacks registered on this document.

Parameters:
  • model (Model) – The model to add as a root of this document.
  • setter (ClientSession or ServerSession or None, optional) –

    This is used to prevent “boomerang” updates to Bokeh apps. (default: None)

    In the context of a Bokeh server application, incoming updates to properties will be annotated with the session that is doing the updating. This value is propagated through any subsequent change notifications that the update triggers. The session can compare the event setter to itself, and suppress any updates that originate from itself.

add_timeout_callback(callback, timeout_milliseconds)[source]

Add callback to be invoked once, after a specified timeout passes.

Parameters:
  • callback (callable) – A callback function to execute after timeout
  • timeout_milliseconds (int) – Number of milliseconds before callback execution.
Returns:

TimeoutCallback

Note

Timeout callbacks only work within the context of a Bokeh server session. This function will no effect when Bokeh outputs to standalone HTML or Jupyter notebook cells.

apply_json_patch(patch, setter=None)[source]

Apply a JSON patch object and process any resulting events.

Parameters:
  • patch (JSON-data) – The JSON-object containing the patch to apply.
  • setter (ClientSession or ServerSession or None, optional) –

    This is used to prevent “boomerang” updates to Bokeh apps. (default: None)

    In the context of a Bokeh server application, incoming updates to properties will be annotated with the session that is doing the updating. This value is propagated through any subsequent change notifications that the update triggers. The session can compare the event setter to itself, and suppress any updates that originate from itself.

Returns:

None

apply_json_patch_string(patch)[source]

Apply a JSON patch provided as a string.

Parameters:patch (str) –
Returns:None
clear()[source]

Remove all content from the document (including roots, vars, stores) but do not reset title.

Returns:None
create_json_patch_string(events)[source]

Create a JSON string describing a patch to be applied.

Parameters:events – list of events to be translated into patches
Returns:JSON string which can be applied to make the given updates to obj
Return type:str
classmethod from_json(json)[source]

Load a document from JSON.

json (JSON-data) :
A JSON-encoded document to create a new Document from.
Returns:Document
classmethod from_json_string(json)[source]

Load a document from JSON.

json (str) :
A string with a JSON-encoded document to create a new Document from.
Returns:Document
get_model_by_id(model_id)[source]

Find the model for the given ID in this document, or None if it is not found.

Parameters:model_id (str) – The ID of the model to search for
Returns:Model or None
get_model_by_name(name)[source]

Find the model for the given name in this document, or None if it is not found.

Parameters:name (str) – The name of the model to search for
Returns:Model or None
on_change(*callbacks)[source]

Provide callbacks to invovke if the document or any Model reachable from its roots changes.

remove_next_tick_callback(callback)[source]

Remove a callback added earlier with add_next_tick_callback.

Returns:None
Raises:KeyError, if the callback was never added
remove_on_change(*callbacks)[source]

Remove a callback added earlier with on_change.

Raises:KeyError, if the callback was never added
remove_periodic_callback(callback)[source]

Remove a callback added earlier with add_periodic_callback

Returns:None
Raises:KeyError, if the callback was never added
remove_root(model, setter=None)[source]

Remove a model as root model from this Document.

Changes to this model may still trigger on_change callbacks on this document, if the model is still referred to by other root models.

Parameters:
  • model (Model) – The model to add as a root of this document.
  • setter (ClientSession or ServerSession or None, optional) –

    This is used to prevent “boomerang” updates to Bokeh apps. (default: None)

    In the context of a Bokeh server application, incoming updates to properties will be annotated with the session that is doing the updating. This value is propagated through any subsequent change notifications that the update triggers. The session can compare the event setter to itself, and suppress any updates that originate from itself.

remove_timeout_callback(callback)[source]

Remove a callback added earlier with add_timeout_callback.

Returns:None
Raises:KeyError, if the callback was never added
replace_with_json(json)[source]

Overwrite everything in this document with the JSON-encoded document.

json (JSON-data) :
A JSON-encoded document to overwrite this one.
Returns:None
select(selector)[source]

Query this document for objects that match the given selector.

Parameters:selector (JSON-like query dictionary) – you can query by type or by name, e.g. {"type": HoverTool}, {"name": "mycircle"}
Returns:seq[Model]
select_one(selector)[source]

Query this document for objects that match the given selector. Raises an error if more than one object is found. Returns single matching object, or None if nothing is found

Parameters:selector (JSON-like query dictionary) – you can query by type or by name, e.g. {"type": HoverTool}, {"name": "mycircle"}
Returns:Model or None
set_select(selector, updates)[source]

Update objects that match a given selector with the specified attribute/value updates.

Parameters:selector (JSON-like query dictionary) – you can query by type or by name,i e.g. {"type": HoverTool}, {"name": "mycircle"} updates (dict) :
Returns:None
to_json()[source]

Convert this document to a JSON object.

Returns:JSON-data
to_json_string(indent=None)[source]

Convert the document to a JSON string.

Parameters:indent (int or None, optional) – number of spaces to indent, or None to suppress all newlines and indentation (default: None)
Returns:str
validate()[source]

Perform integrity checks on the modes in this document.

Returns:None
roots

A list of all the root models in this Document.

session_callbacks

A list of all the session callbacks on this document.

session_context

The SessionContext for this document.

template

A Jinja2 template to use for rendering this document.

template_variables

A dictionary of template variables to pass when rendering self.template.

theme

The current Theme instance affecting models in this Document.

Setting this to None sets the default theme. (i.e this property never returns None.)

Changing theme may trigger model change events on the models in the document if the theme modifies any model properties.

title

A title for this document.

This title will be set on standalone HTML documents, but not e.g. when autoload_server is used.

class UnlockedDocumentProxy(doc)[source]

Wrap a Document object so that only methods that can safely be used from unlocked callbacks or threads are exposed. Attempts to otherwise access or change the Document results in an exception.

without_document_lock(f)[source]

Mark a callback function to execute without first obtaining the document lock.

Warning

The value of curdoc() inside the callback will be None. Any attempt to modify the document inside the callback can result in data or protocol corruption.