Hi everyone.
Thank you @Marc and @riziles for your examples. They work perfectly fine. However I’m trying to do pretty much the same with some Bokeh plot where I have controls on one window and the actual figure on another one.
import uuid
from typing import Optional
import numpy as np
import panel as pn
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
def get_object_id() -> Optional[str]:
_id = pn.state.session_args.get("id")
return None if _id is None else _id[0].decode(encoding='utf-8')
def serve_controls():
source = ColumnDataSource(data={
'x': np.random.randint(0, 10, 10),
'y': np.random.randint(0, 10, 10),
})
fig = figure()
fig.circle(x='x', y='y', source=source)
object_id = str(uuid.uuid4())
pn.state.cache[object_id] = fig
randomize_btn = pn.widgets.Button(name='Randomize', button_type="success")
@pn.depends(randomize_btn, watch=True)
def randomize(_) -> None:
source.data = {
'x': np.random.randint(0, 10, 10),
'y': np.random.randint(0, 10, 10),
}
new_window_btn = pn.widgets.Button(name='Open in new window', button_type="primary")
new_window_btn.jscallback(clicks=f"window.open('?id={object_id}', '_blank', 'left=500,height=400,width=400')")
pn.Column(randomize_btn, new_window_btn,).servable("Controls")
def serve_object(object_id: str) -> None:
obj = pn.state.cache.get(object_id, pn.pane.Markdown("Not found"))
pn.Column(obj).servable("Figure")
panel_object_id: Optional[str] = get_object_id()
if panel_object_id:
serve_object(panel_object_id)
else:
serve_controls()
Unfortunately now I receive some error.
...
File "example.py", line 31, in randomize
source.data = {
...
File ".venv/lib/python3.9/site-packages/bokeh/document/events.py", line 400, in dispatch
super().dispatch(receiver)
File ".venv/lib/python3.9/site-packages/bokeh/document/events.py", line 223, in dispatch
cast(DocumentPatchedMixin, receiver)._document_patched(self)
File ".venv/lib/python3.9/site-packages/bokeh/server/session.py", line 247, in _document_patched
raise RuntimeError("_pending_writes should be non-None when we have a document lock, and we should have the lock when the document changes")
RuntimeError: _pending_writes should be non-None when we have a document lock, and we should have the lock when the document changes
Is there a possibility to get the proposed logic working with ColumnDataSources or is this actually a good approach for solving this specific issue?
I’m using:
- Python version: 3.9
- Bokeh version: 2.4.3
- Panel version: 0.13.1