Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to bokeh 2.4 and drop compatibility for older versions #2739

Merged
merged 3 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions panel/io/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from bokeh.models import CustomJS
from param.parameterized import Watcher

from ..util import doc_event_obj
from .model import add_to_doc, diff
from .state import state

Expand Down Expand Up @@ -302,8 +301,7 @@ def is_embeddable(object):
values.append((ws, w_models, vals, getter))

add_to_doc(model, doc, True)
event_obj = doc_event_obj(doc)
event_obj._held_events = []
doc.callbacks._held_events = []

if not widget_data:
return
Expand Down Expand Up @@ -338,13 +336,12 @@ def is_embeddable(object):
break
sub_dict = sub_dict[g(m[0])]
if skip:
event_obj._held_events = []
doc.callbacks._held_events = []
continue

# Drop events originating from widgets being varied
models = [m for v in values for m in v[1]]
event_obj = doc_event_obj(doc)
event_obj._held_events = [e for e in event_obj._held_events if e.model not in models]
doc.callbacks._held_events = [e for e in doc.callbacks._held_events if e.model not in models]
events = record_events(doc)
changes |= events['content'] != '{}'
if events:
Expand Down
16 changes: 6 additions & 10 deletions panel/io/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from bokeh.models import Box, ColumnDataSource, Model
from bokeh.protocol import Protocol

from ..util import doc_event_obj
from .state import state

#---------------------------------------------------------------------
Expand All @@ -21,9 +20,8 @@ def diff(doc, binary=True, events=None):
Returns a json diff required to update an existing plot with
the latest plot data.
"""
event_obj = doc_event_obj(doc)
if events is None:
events = list(event_obj._held_events)
events = list(doc.callbacks._held_events)
if not events or state._hold:
return None

Expand All @@ -33,7 +31,7 @@ def diff(doc, binary=True, events=None):
and e.hint.cols is not None):
e.hint.cols = None
msg = Protocol().create("PATCH-DOC", events, use_buffers=binary)
event_obj._held_events = [e for e in event_obj._held_events if e not in events]
doc.callbacks._held_events = [e for e in doc.callbacks._held_events if e not in events]
return msg


Expand All @@ -57,15 +55,13 @@ def add_to_doc(obj, doc, hold=False):
# Add new root
remove_root(obj)
doc.add_root(obj)
event_obj = doc_event_obj(doc)
doc_hold = event_obj._hold
if doc_hold is None and hold:
if doc.callbacks.hold_value is None and hold:
doc.hold()


@contextmanager
def hold(doc, policy='combine', comm=None):
event_obj = doc_event_obj(doc)
held = event_obj._hold
held = doc.callbacks.hold_value
try:
if policy is None:
doc.unhold()
Expand All @@ -74,7 +70,7 @@ def hold(doc, policy='combine', comm=None):
yield
finally:
if held:
event_obj._hold = held
doc.callbacks._hold = held
else:
if comm is not None:
from .notebook import push
Expand Down
43 changes: 12 additions & 31 deletions panel/io/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

# Bokeh imports
from bokeh.application import Application as BkApplication
from bokeh.application.handlers.code import CodeHandler
from bokeh.application.handlers.code import CodeHandler, _monkeypatch_io, patch_curdoc
from bokeh.application.handlers.function import FunctionHandler
from bokeh.command.util import build_single_handler_application
from bokeh.core.templates import AUTOLOAD_JS
Expand All @@ -47,9 +47,8 @@
from tornado.wsgi import WSGIContainer

# Internal imports
from ..util import bokeh_version, doc_event_obj, edit_readonly
from ..util import edit_readonly
from .logging import LOG_SESSION_CREATED, LOG_SESSION_DESTROYED, LOG_SESSION_LAUNCHING

from .profile import profile_ctx
from .reload import autoreload_watcher
from .resources import BASE_TEMPLATE, Resources, bundle_resources
Expand Down Expand Up @@ -300,18 +299,11 @@ def modify_document(self, doc):
# stored modules are used to provide correct paths to
# custom models resolver.
sys.modules[module.__name__] = module
if bokeh_version >= '2.4.0':
doc.modules._modules.append(module)

else:
doc._modules.append(module)
doc.modules._modules.append(module)

old_doc = curdoc()
bk_set_curdoc(doc)

if bokeh_version < '2.4.0':
old_io = self._monkeypatch_io()

if config.autoreload:
set_curdoc(doc)
state.onload(autoreload_watcher)
Expand All @@ -325,10 +317,7 @@ def post_check():
# otherwise it will erroneously complain that there is
# a memory leak
if config.autoreload:
if bokeh_version >= '2.4.0':
newdoc.modules._modules = []
else:
newdoc._modules = []
newdoc.modules._modules = []

# script is supposed to edit the doc not replace it
if newdoc is not doc:
Expand Down Expand Up @@ -358,15 +347,10 @@ def handle_exception(handler, e):
bokeh.application.handlers.code_runner.handle_exception = handle_exception

state._launching.append(doc)
if bokeh_version >= '2.4.0':
from bokeh.application.handlers.code import _monkeypatch_io, patch_curdoc
with _monkeypatch_io(self._loggers):
with patch_curdoc(doc):
with profile_ctx(config.profiler) as sessions:
self._runner.run(module, post_check)
else:
with profile_ctx(config.profiler) as sessions:
self._runner.run(module, post_check)
with _monkeypatch_io(self._loggers):
with patch_curdoc(doc):
with profile_ctx(config.profiler) as sessions:
self._runner.run(module, post_check)

def _log_session_destroyed(session_context):
logger.info(LOG_SESSION_DESTROYED, id(doc))
Expand All @@ -380,8 +364,6 @@ def _log_session_destroyed(session_context):
state.param.trigger('_profiles')
except Exception:
pass
if bokeh_version < '2.4.0':
self._unmonkeypatch_io(old_io)
bk_set_curdoc(old_doc)

CodeHandler.modify_document = modify_document
Expand Down Expand Up @@ -452,10 +434,9 @@ def unlocked():
return
connections = curdoc.session_context.session._subscribed_connections

event_obj = doc_event_obj(curdoc)
hold = event_obj._hold
hold = curdoc.callbacks.hold_value
if hold:
old_events = list(event_obj._held_events)
old_events = list(curdoc.callbacks._held_events)
else:
old_events = []
curdoc.hold()
Expand All @@ -467,7 +448,7 @@ def unlocked():
if hasattr(socket, 'write_lock') and socket.write_lock._block._value == 0:
state._locks.add(socket)
locked = socket in state._locks
for event in event_obj._held_events:
for event in curdoc.callbacks._held_events:
if (isinstance(event, ModelChangedEvent) and event not in old_events
and hasattr(socket, 'write_message') and not locked):
msg = conn.protocol.create('PATCH-DOC', [event])
Expand All @@ -479,7 +460,7 @@ def unlocked():
WebSocketHandler.write_message(socket, payload, binary=True)
elif event not in events:
events.append(event)
event_obj._held_events = events
curdoc.callbacks._held_events = events
finally:
if not hold:
curdoc.unhold()
Expand Down
Loading