-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
callbacks.py
490 lines (371 loc) · 16.3 KB
/
callbacks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#-----------------------------------------------------------------------------
# Copyright (c) 2012 - 2024, Anaconda, Inc., and Bokeh Contributors.
# All rights reserved.
#
# The full license is in the file LICENSE.txt, distributed with this software.
#-----------------------------------------------------------------------------
''' Encapulate the management of Document callbacks with a
DocumentCallbackManager class.
'''
#-----------------------------------------------------------------------------
# Boilerplate
#-----------------------------------------------------------------------------
from __future__ import annotations
import logging # isort:skip
log = logging.getLogger(__name__)
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# Standard library imports
import weakref
from collections import defaultdict
from functools import wraps
from typing import TYPE_CHECKING, Any, Callable
# Bokeh imports
from ..core.enums import HoldPolicy, HoldPolicyType
from ..events import (
_CONCRETE_EVENT_CLASSES,
DocumentEvent,
Event,
ModelEvent,
)
from ..model import Model
from ..models.callbacks import Callback as JSEventCallback
from ..util.callback_manager import _check_callback
from .events import (
DocumentPatchedEvent,
ModelChangedEvent,
RootAddedEvent,
RootRemovedEvent,
SessionCallbackAdded,
SessionCallbackRemoved,
TitleChangedEvent,
)
from .locking import UnlockedDocumentProxy
if TYPE_CHECKING:
from ..application.application import SessionDestroyedCallback
from ..core.has_props import Setter
from ..server.callbacks import SessionCallback
from .document import Document
from .events import DocumentChangeCallback, DocumentChangedEvent, Invoker
#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------
__all__ = (
'DocumentCallbackManager',
'DocumentPatchedEvent',
'invoke_with_curdoc',
'ModelChangedEvent',
'RootAddedEvent',
'RootRemovedEvent',
'SessionCallbackAdded',
'SessionCallbackRemoved',
'TitleChangedEvent',
)
Callback = Callable[[], None]
Originator = Callable[..., Any]
MessageCallback = Callable[[Any], None]
EventCallback = Callable[[Event], None]
#-----------------------------------------------------------------------------
# General API
#-----------------------------------------------------------------------------
class DocumentCallbackManager:
''' Manage and provide access to all of the models that belong to a Bokeh
Document.
The set of "all models" means specifically all the models reachable from
references form a Document's roots.
'''
_document: weakref.ReferenceType[Document]
_change_callbacks: dict[Any, DocumentChangeCallback]
_event_callbacks: dict[str, list[EventCallback]]
_js_event_callbacks: dict[str, list[JSEventCallback]]
_message_callbacks: dict[str, list[MessageCallback]]
_session_destroyed_callbacks: set[SessionDestroyedCallback]
_session_callbacks: set[SessionCallback]
_subscribed_models: dict[str, set[weakref.ReferenceType[Model]]]
_hold: HoldPolicyType | None = None
_held_events: list[DocumentChangedEvent]
def __init__(self, document: Document):
'''
Args:
document (Document): A Document to manage models for
A weak reference to the Document will be retained
'''
self._document = weakref.ref(document)
self._change_callbacks = {}
self._event_callbacks = defaultdict(list)
self._js_event_callbacks = defaultdict(list)
self._message_callbacks = defaultdict(list)
self._session_destroyed_callbacks = set()
self._session_callbacks = set()
self._subscribed_models = defaultdict(set)
self._hold = None
self._held_events = []
self.on_message("bokeh_event", self.trigger_event)
@property
def session_callbacks(self) -> list[SessionCallback]:
''' A list of all the session callbacks for this document.
'''
return list(self._session_callbacks)
@property
def session_destroyed_callbacks(self) -> set[SessionDestroyedCallback]:
''' A list of all the on_session_destroyed callbacks for this document.
'''
return self._session_destroyed_callbacks
@session_destroyed_callbacks.setter
def session_destroyed_callbacks(self, callbacks: set[SessionDestroyedCallback]) -> None:
self._session_destroyed_callbacks = callbacks
def add_session_callback(self, callback_obj: SessionCallback, callback: Callback, one_shot: bool) -> SessionCallback:
''' Internal implementation for adding session callbacks.
Args:
callback_obj (SessionCallback) :
A session callback object that wraps a callable and is
passed to ``trigger_on_change``.
callback (callable) :
A callable to execute when session events happen.
one_shot (bool) :
Whether the callback should immediately auto-remove itself
after one execution.
Returns:
SessionCallback : passed in as ``callback_obj``.
Raises:
ValueError, if the callback has been previously added
'''
doc = self._document()
if doc is None:
raise RuntimeError("Attempting to add session callback to already-destroyed Document")
if one_shot:
@wraps(callback)
def remove_then_invoke() -> None:
if callback_obj in self._session_callbacks:
self.remove_session_callback(callback_obj)
return callback()
actual_callback = remove_then_invoke
else:
actual_callback = callback
callback_obj._callback = _wrap_with_curdoc(doc, actual_callback)
self._session_callbacks.add(callback_obj)
# emit event so the session is notified of the new callback
self.trigger_on_change(SessionCallbackAdded(doc, callback_obj))
return callback_obj
def destroy(self) -> None:
''' Clean up references to the Documents models
'''
self._change_callbacks.clear()
del self._change_callbacks
self._event_callbacks.clear()
del self._event_callbacks
self._js_event_callbacks.clear()
del self._js_event_callbacks
self._message_callbacks.clear()
del self._message_callbacks
def hold(self, policy: HoldPolicyType = "combine") -> None:
if self._hold is not None and self._hold != policy:
log.warning(f"hold already active with '{self._hold}', ignoring '{policy}'")
return
if policy not in HoldPolicy:
raise ValueError(f"Unknown hold policy {policy}")
self._hold = policy
@property
def hold_value(self) -> HoldPolicyType | None:
return self._hold
def notify_change(self, model: Model, attr: str, old: Any, new: Any,
hint: DocumentPatchedEvent | None = None, setter: Setter | None = None, callback_invoker: Invoker | None = None) -> None:
''' Called by Model when it changes
'''
doc = self._document()
if doc is None:
return
# if name changes, need to update by-name index
if attr == 'name':
doc.models.update_name(model, old, new)
event: DocumentPatchedEvent
if hint is None:
new = model.lookup(attr).get_value(model)
event = ModelChangedEvent(doc, model, attr, new, setter, callback_invoker)
else:
assert hint.callback_invoker is None
hint.callback_invoker = callback_invoker
if hint.setter is None:
hint.setter = setter
event = hint
self.trigger_on_change(event)
def notify_event(self, model: Model, event: ModelEvent, callback_invoker: Invoker) -> None:
'''
'''
doc = self._document()
if doc is None:
return
# TODO (bev): use internal event here to dispatch, rather than hard-coding invocation here
invoke_with_curdoc(doc, callback_invoker)
def on_change(self, *callbacks: DocumentChangeCallback) -> None:
''' Provide callbacks to invoke if the document or any Model reachable
from its roots changes.
'''
for callback in callbacks:
if callback in self._change_callbacks:
continue
_check_callback(callback, ('event',))
self._change_callbacks[callback] = callback
def on_change_dispatch_to(self, receiver: Any) -> None:
if receiver not in self._change_callbacks:
self._change_callbacks[receiver] = lambda event: event.dispatch(receiver)
def on_event(self, event: str | type[Event], *callbacks: EventCallback) -> None:
''' Provide callbacks to invoke if a bokeh event is received.
'''
self._on_event(event, *callbacks)
def js_on_event(self, event: str | type[Event], *callbacks: JSEventCallback) -> None:
''' Provide JS callbacks to invoke if a bokeh event is received.
'''
self._on_event(event, *callbacks)
def _on_event(self, event: str | type[Event], *callbacks: EventCallback | JSEventCallback) -> None:
if not isinstance(event, str) and issubclass(event, Event):
event = event.event_name
if event not in _CONCRETE_EVENT_CLASSES:
raise ValueError(f"Unknown event {event}")
if not issubclass(_CONCRETE_EVENT_CLASSES[event], DocumentEvent):
raise ValueError("Document.on_event may only be used to subscribe "
"to events of type DocumentEvent. To subscribe "
"to a ModelEvent use the Model.on_event method.")
for callback in callbacks:
if isinstance(callback, JSEventCallback):
self._js_event_callbacks[event].append(callback)
else:
_check_callback(callback, ('event',), what='Event callback')
self._event_callbacks[event].append(callback)
def on_message(self, msg_type: str, *callbacks: MessageCallback) -> None:
self._message_callbacks[msg_type].extend(callbacks)
def on_session_destroyed(self, *callbacks: SessionDestroyedCallback) -> None:
''' Provide callbacks to invoke when the session serving the Document
is destroyed
'''
for callback in callbacks:
_check_callback(callback, ('session_context',))
self._session_destroyed_callbacks.add(callback)
def remove_on_change(self, *callbacks: Any) -> None:
''' Remove a callback added earlier with ``on_change``.
Raises:
KeyError, if the callback was never added
'''
for callback in callbacks:
del self._change_callbacks[callback]
def remove_on_message(self, msg_type: str, callback: MessageCallback) -> None:
'''
'''
message_callbacks = self._message_callbacks.get(msg_type, None)
if message_callbacks is not None and callback in message_callbacks:
message_callbacks.remove(callback)
def remove_session_callback(self, callback_obj: SessionCallback) -> None:
''' Remove a callback added earlier with ``add_periodic_callback``,
``add_timeout_callback``, or ``add_next_tick_callback``.
Returns:
None
Raises:
KeyError, if the callback was never added
'''
try:
callback_objs = [callback_obj]
self._session_callbacks.remove(callback_obj)
except KeyError:
raise ValueError("callback already ran or was already removed, cannot be removed again")
doc = self._document()
if doc is None:
return
# emit event so the session is notified and can remove the callback
for callback_obj in callback_objs:
self.trigger_on_change(SessionCallbackRemoved(doc, callback_obj))
def subscribe(self, key: str, model: Model) -> None:
self._subscribed_models[key].add(weakref.ref(model))
def event_callbacks_for_event_name(self, event_name: str) -> tuple[EventCallback, ...]:
''' Return a tuple containing all current event callbacks for the given
event name.
Args:
event_name (str) : the event name to look up callbacks for
'''
return tuple(self._event_callbacks.get(event_name, []))
def change_callbacks(self) -> tuple[DocumentChangeCallback, ...]:
''' Return a tuple containing all current change callbacks.
'''
return tuple(self._change_callbacks.values())
def trigger_event(self, event: Event) -> None:
# This is fairly gorpy, we are not being careful with model vs doc events, etc.
if isinstance(event, ModelEvent):
subscribed = self._subscribed_models[event.event_name].copy()
for model_ref in subscribed:
model = model_ref()
if model:
model._trigger_event(event)
for cb in self.event_callbacks_for_event_name(event.event_name):
cb(event)
def trigger_on_change(self, event: DocumentChangedEvent) -> None:
doc = self._document()
if doc is None:
return
if self._hold == "collect":
self._held_events.append(event)
return
elif self._hold == "combine":
_combine_document_events(event, self._held_events)
return
if event.callback_invoker is not None:
invoke_with_curdoc(doc, event.callback_invoker)
def invoke_callbacks() -> None:
for cb in self.change_callbacks():
cb(event)
invoke_with_curdoc(doc, invoke_callbacks)
def unhold(self) -> None:
''' Turn off any active document hold and apply any collected events.
Returns:
None
'''
# no-op if we are already no holding
if self._hold is None:
return
self._hold = None
events = list(self._held_events)
self._held_events = []
for event in events:
self.trigger_on_change(event)
#-----------------------------------------------------------------------------
# Dev API
#-----------------------------------------------------------------------------
def invoke_with_curdoc(doc: Document, f: Callable[[], None]) -> None:
from ..io.doc import patch_curdoc
curdoc: Document|UnlockedDocumentProxy = UnlockedDocumentProxy(doc) if getattr(f, "nolock", False) else doc
with patch_curdoc(curdoc):
return f()
#-----------------------------------------------------------------------------
# Private API
#-----------------------------------------------------------------------------
def _combine_document_events(new_event: DocumentChangedEvent, old_events: list[DocumentChangedEvent]) -> None:
''' Attempt to combine a new event with a list of previous events.
The ``old_event`` will be scanned in reverse, and ``.combine(new_event)``
will be called on each. If a combination can be made, the function
will return immediately. Otherwise, ``new_event`` will be appended to
``old_events``.
Args:
new_event (DocumentChangedEvent) :
The new event to attempt to combine
old_events (list[DocumentChangedEvent])
A list of previous events to attempt to combine new_event with
**This is an "out" parameter**. The values it contains will be
modified in-place.
Returns:
None
'''
for event in reversed(old_events):
if event.combine(new_event):
return
# no combination was possible
old_events.append(new_event)
def _wrap_with_curdoc(doc: Document, f: Callable[..., Any]) -> Callable[..., Any]:
@wraps(f)
def wrapper(*args: Any, **kwargs: Any) -> None:
@wraps(f)
def invoke() -> Any:
return f(*args, **kwargs)
return invoke_with_curdoc(doc, invoke)
return wrapper
#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------