-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Open
Labels
Description
Software versions
Python version : 3.11.10 | packaged by conda-forge | (main, Sep 22 2024, 14:10:38) [GCC 13.3.0]
IPython version : 9.3.0
Tornado version : 6.5.1
NumPy version : 1.26.4
Bokeh version : 3.7.3
BokehJS static path : /.../lib/python3.11/site-packages/bokeh/server/static
node.js version : (not installed)
npm version : (not installed)
jupyter_bokeh version : (not installed)
Operating system : Linux-6.6.87.2-microsoft-standard-WSL2-x86_64-with-glibc2.31
Browser name and version
Chrome 137
Jupyter notebook / Jupyter Lab version
N/A
Expected behavior
Discussed at: https://discourse.bokeh.org/t/on-session-destroyed-cannot-access-module-level-imports-variables/12482
For a on_session_destroyed
callback is added via doc.on_session_destroyed()
, it should have access to module level imports / variables like typical functions.
Observed behavior
The callback appears to run in some isolated context, and does not have access to to module level imports / variables.
Example code
Run the following codes with `bokeh serve` (either as a 1-file app or a directory app). Bokeh server would report an error in running the callback, complaining that the module level import `np` is not defined.
DocumentLifeCycleHandler on_session_destroyed callback <function my_on_session_destroyed at 0x714ea77614e0> failed with following error: name 'np' is not defined
The example 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())
Stack traceback or browser console output
No response
Screenshots
No response