What is the smallest unit of serialization
A Document is the smallest meaningful unit of serialization. Although individual Bokeh models have some JSON representation, that representation may also contain references to other models, so it is not in general meaningful to “serialize” a single Bokeh model in isolation. When you add a model as a “root” of a Document you also need to traverse any and all other Bokeh models reachable from that root, and include them in the Document references. Here’s an example of adding a bare plot as a root to a Document:
In [16]: p = figure()
In [17]: doc = Document()
In [18]: doc.add_root(p)
In [19]: doc.to_json()
The resulting output is
{
"roots": {
"references": [
{
"attributes": {
"axis": {
"id": "1013"
},
"ticker": null
},
"id": "1016",
"type": "Grid"
},
{
"attributes": {
"renderers": []
},
"id": "1005",
"type": "DataRange1d"
},
{
"attributes": {},
"id": "1014",
"type": "BasicTicker"
},
{
"attributes": {
"renderers": []
},
"id": "1007",
"type": "DataRange1d"
},
{
"attributes": {
"formatter": {
"id": "1040"
},
"ticker": {
"id": "1018"
}
},
"id": "1017",
"type": "LinearAxis"
},
{
"attributes": {},
"id": "1040",
"type": "BasicTickFormatter"
},
{
"attributes": {
"active_drag": "auto",
"active_inspect": "auto",
"active_multi": null,
"active_scroll": "auto",
"active_tap": "auto",
"tools": [
{
"id": "1021"
},
{
"id": "1022"
},
{
"id": "1023"
},
{
"id": "1024"
},
{
"id": "1025"
},
{
"id": "1026"
}
]
},
"id": "1028",
"type": "Toolbar"
},
{
"attributes": {},
"id": "1022",
"type": "WheelZoomTool"
},
{
"attributes": {
"bottom_units": "screen",
"fill_alpha": 0.5,
"fill_color": "lightgrey",
"left_units": "screen",
"level": "overlay",
"line_alpha": 1,
"line_color": "black",
"line_dash": [
4,
4
],
"line_width": 2,
"right_units": "screen",
"top_units": "screen"
},
"id": "1027",
"type": "BoxAnnotation"
},
{
"attributes": {
"below": [
{
"id": "1013"
}
],
"center": [
{
"id": "1016"
},
{
"id": "1020"
}
],
"left": [
{
"id": "1017"
}
],
"title": {
"id": "1035"
},
"toolbar": {
"id": "1028"
},
"x_range": {
"id": "1005"
},
"x_scale": {
"id": "1009"
},
"y_range": {
"id": "1007"
},
"y_scale": {
"id": "1011"
}
},
"id": "1004",
"subtype": "Figure",
"type": "Plot"
},
{
"attributes": {
"overlay": {
"id": "1027"
}
},
"id": "1023",
"type": "BoxZoomTool"
},
{
"attributes": {},
"id": "1009",
"type": "LinearScale"
},
{
"attributes": {},
"id": "1025",
"type": "ResetTool"
},
{
"attributes": {
"formatter": {
"id": "1038"
},
"ticker": {
"id": "1014"
}
},
"id": "1013",
"type": "LinearAxis"
},
{
"attributes": {},
"id": "1024",
"type": "SaveTool"
},
{
"attributes": {},
"id": "1011",
"type": "LinearScale"
},
{
"attributes": {
"text": ""
},
"id": "1035",
"type": "Title"
},
{
"attributes": {},
"id": "1021",
"type": "PanTool"
},
{
"attributes": {},
"id": "1026",
"type": "HelpTool"
},
{
"attributes": {},
"id": "1018",
"type": "BasicTicker"
},
{
"attributes": {},
"id": "1038",
"type": "BasicTickFormatter"
},
{
"attributes": {
"axis": {
"id": "1017"
},
"dimension": 1,
"ticker": null
},
"id": "1020",
"type": "Grid"
}
],
"root_ids": [
"1004"
]
},
"title": "Bokeh Application",
"version": "2.3.0dev5-6-g8c193aa5b"
}
What is the top-level key structure of a Document
Referring to the above:
"roots": {
"references": [<all the serialized models>],
"root_ids": ["<some model id>"]
},
"title": "Some title for the document",
"version": "<bokeh version that created>"
}
The references contains a JSON representation of every Bokeh model reachable from the root, where each looks like:
{
"attributes": {
"axis": {
"id": "1017"
},
"dimension": 1,
"ticker": null
},
"id": "1020",
"type": "Grid"
}
The above is a JSON repr for an object of type Grid, with "1020". Every models needs an id value that is unique within the document. Bokeh increments a simple counter by default but it can be anything (e.g. UUIDs if you want). The attributes contains all property values that are different from the default value for the property. I.e. all values that have been explicitly set. In this case it has a dimension value of 1, a ticker value of null (None, from Python) and for the axis, it has a reference to another model with id “1017”. These values were modified by the figure function when it assembled all the pieces of the figure.