How to use curdoc in Bokeh

curdoc() is a function in Bokeh that returns the current document for a Bokeh application. You can use curdoc() to access the current document and add, remove, or modify elements in it.

Here’s an example of how you can use curdoc() to add a plot to a Bokeh document:

from bokeh.plotting import figure, curdoc
from bokeh.io import show
from bokeh.models import ColumnDataSource
from bokeh.layouts import row
from bokeh.sampledata.stocks import AAPL

# Prepare the data
df = AAPL
source = ColumnDataSource(df)

# Create a line plot
p = figure(x_axis_type="datetime", title="AAPL Stock Price")
p.line(x="date", y="close", line_width=2, source=source, legend_label="Close")

# Add the plot to the current document
curdoc().add_root(row(p))

# Show the plot
show(p)

In this example, we first import the required modules from Bokeh, including the figure, show, output_notebook, ColumnDataSource, row, Spectral4, and AAPL sample data.
Then we prepare the data by converting the AAPL sample data into a ColumnDataSource and create a line plot using the figure method.
Finally, we use the add_root method of the curdoc() object to add the plot to the current document, and we show the plot using the show function.