Hi,
It seems that on_session_destroyed()
is run in some isolated context such that it has no access to module level variables (e.g., logger) and imports
E.g., for the following sample on_session_destroyed
callback, it has no access to np
.
import numpy as np
def my_on_session_destroyed(session_context):
print("my_on_session_destroyed():", np.nan)
Bokeh server outputs the following error:
DocumentLifeCycleHandler on_session_destroyed callback
<function my_on_session_destroyed at 0x714ea77614e0>
failed with following error: name 'np' is not defined
Is that the expected behavior? I’m running it with bokeh 3.7.3 and tornado 6.5.1
Thanks.
Sam
Complete test codes:
from bokeh.io import curdoc
from bokeh.layouts import layout
from bokeh.plotting import figure, ColumnDataSource
import numpy as np
def my_on_session_destroyed(session_context):
# # Uncomment the following line to make the codes to work
# # (the module level import seems to have no effect here)
# import numpy as np
print("my_on_session_destroyed():", np.nan)
def create_interact_ui(doc):
fig = figure(
title="Test Bokeh server",
)
source = ColumnDataSource(data=dict(
x=np.array([0, 5, 10], dtype=float),
y=np.array([4, 6, 8], dtype=float),
))
fig.scatter(source=source, size=16, marker="square", fill_color="red")
doc.add_root(layout([
[fig, ]
]))
doc.on_session_destroyed(my_on_session_destroyed)
#
# Main
#
create_interact_ui(curdoc())