#-----------------------------------------------------------------------------# Copyright (c) 2012 - 2022, Anaconda, Inc., and Bokeh Contributors.# All rights reserved.## The full license is in the file LICENSE.txt, distributed with this software.#-----------------------------------------------------------------------------''' Provide a base class for all objects (called Bokeh Models) that can go ina Bokeh |Document|.'''#-----------------------------------------------------------------------------# Boilerplate#-----------------------------------------------------------------------------from__future__importannotationsimportlogging# isort:skiplog=logging.getLogger(__name__)#-----------------------------------------------------------------------------# Imports#-----------------------------------------------------------------------------# Standard library importsfrominspectimportisclassfromjsonimportloadsfromtypingimport(TYPE_CHECKING,Any,ClassVar,Dict,Iterable,List,Set,Type,)# Bokeh importsfrom..coreimportpropertiesaspfrom..core.has_propsimportHasProps,abstractfrom..core.json_encoderimportserialize_jsonfrom..core.typesimport(ID,Ref,ReferenceJson,Unknown,)from..eventsimportEventfrom..themesimportdefaultasdefault_themefrom..util.callback_managerimportEventCallbackManager,PropertyCallbackManagerfrom..util.serializationimportmake_idfrom.docsimporthtml_repr,process_examplefrom.utilimport(HasDocumentRef,collect_models,qualified_model,visit_value_and_its_immediate_references,)ifTYPE_CHECKING:from..core.has_propsimportSetterfrom..core.queryimportSelectorTypefrom..core.typesimportJSONfrom..documentimportDocumentfrom..document.eventsimportDocumentPatchedEventfrom..models.callbacksimportCallbackasJSEventCallbackfrom..util.callback_managerimportPropertyCallback#-----------------------------------------------------------------------------# Globals and constants#-----------------------------------------------------------------------------__all__=('Model',)#-----------------------------------------------------------------------------# General API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Dev API#-----------------------------------------------------------------------------
[docs]@abstractclassModel(HasProps,HasDocumentRef,PropertyCallbackManager,EventCallbackManager):''' Base class for all objects stored in Bokeh |Document| instances. '''model_class_reverse_map:ClassVar[Dict[str,Type[Model]]]={}@classmethoddef__init_subclass__(cls):super().__init_subclass__()# use an explicitly provided view model name if there is oneif"__view_model__"notincls.__dict__:cls.__view_model__=cls.__name__if"__view_module__"notincls.__dict__:cls.__view_module__=cls.__module__qualified=qualified_model(cls)cls.__qualified_model__=qualified# update the mapping of view model names to classes, checking for any duplicatesprevious=cls.model_class_reverse_map.get(qualified,None)ifpreviousisnotNoneandnothasattr(cls,"__implementation__"):raiseWarning(f"Duplicate qualified model declaration of '{qualified}'. Previous definition: {previous}")cls.model_class_reverse_map[qualified]=clsprocess_example(cls)_id:IDdef__new__(cls,*args,**kwargs)->Model:obj=super().__new__(cls)obj._id=kwargs.pop("id",make_id())returnobjdef__init__(self,**kwargs:Any)->None:# "id" is popped from **kw in __new__, so in an ideal world I don't# think it should be here too. But Python has subtle behavior here, so# it is necessarykwargs.pop("id",None)super().__init__(**kwargs)default_theme.apply_to_model(self)def__str__(self)->str:name=self.__class__.__name__returnf"{name}(id={self.id!r}, ...)"__repr__=__str__
[docs]defdestroy(self)->None:''' Clean up references to the document and property '''self._document=Noneself._temp_document=Noneself._property_values.clear()
@propertydefid(self)->ID:returnself._idname:str|None=p.Nullable(p.String,help=""" An arbitrary, user-supplied name for this model. This name can be useful when querying the document to retrieve specific Bokeh models. .. code:: python >>> plot.circle([1,2,3], [4,5,6], name="temp") >>> plot.select(name="temp") [GlyphRenderer(id='399d53f5-73e9-44d9-9527-544b761c7705', ...)] .. note:: No uniqueness guarantees or other conditions are enforced on any names that are provided, nor is the name used directly by Bokeh for any reason. """)tags:List[Any]=p.List(p.AnyRef,help=""" An optional list of arbitrary, user-supplied values to attach to this model. This data can be useful when querying the document to retrieve specific Bokeh models: .. code:: python >>> r = plot.circle([1,2,3], [4,5,6]) >>> r.tags = ["foo", 10] >>> plot.select(tags=['foo', 10]) [GlyphRenderer(id='1de4c3df-a83d-480a-899b-fb263d3d5dd9', ...)] Or simply a convenient way to attach any necessary metadata to a model that can be accessed by ``CustomJS`` callbacks, etc. .. note:: No uniqueness guarantees or other conditions are enforced on any tags that are provided, nor are the tags used directly by Bokeh for any reason. """)js_event_callbacks=p.Dict(p.String,p.List(p.Instance("bokeh.models.callbacks.CustomJS")),help=""" A mapping of event names to lists of ``CustomJS`` callbacks. Typically, rather then modifying this property directly, callbacks should be added using the ``Model.js_on_event`` method: .. code:: python callback = CustomJS(code="console.log('tap event occurred')") plot.js_on_event('tap', callback) """)subscribed_events=p.List(p.String,help=""" List of events that are subscribed to by Python callbacks. This is the set of events that will be communicated from BokehJS back to Python for this model. """)js_property_callbacks=p.Dict(p.String,p.List(p.Instance("bokeh.models.callbacks.CustomJS")),help=""" A mapping of attribute names to lists of ``CustomJS`` callbacks, to be set up on BokehJS side when the document is created. Typically, rather then modifying this property directly, callbacks should be added using the ``Model.js_on_change`` method: .. code:: python callback = CustomJS(code="console.log('stuff')") plot.x_range.js_on_change('start', callback) """)syncable:bool=p.Bool(default=True,help=""" Indicates whether this model should be synchronized back to a Bokeh server when updated in a web browser. Setting to ``False`` may be useful to reduce network traffic when dealing with frequently updated objects whose updated values we don't need. .. note:: Setting this property to ``False`` will prevent any ``on_change()`` callbacks on this object from triggering. However, any JS-side callbacks will still work. """)# Properties --------------------------------------------------------------@propertydefref(self)->Ref:returnRef(id=self._id)@propertydefstruct(self)->ReferenceJson:''' A Bokeh protocol "structure" of this model, i.e. a dict of the form: .. code-block:: python { 'type' : << view model name >> 'id' : << unique model id >> } Additionally there may be a `subtype` field if this model is a subtype. '''this=ReferenceJson(id=self.id,type=self.__qualified_model__,attributes={},)if"__subtype__"inself.__class__.__dict__:# XXX: remove __subtype__ and this garbage at 2.0parts=this["type"].split(".")parts[-1]=self.__view_model__this["type"]=".".join(parts)this["subtype"]=self.__subtype__returnthis# Public methods ----------------------------------------------------------defjs_on_event(self,event:str|Type[Event],*callbacks:JSEventCallback)->None:ifisinstance(event,str):passelifisinstance(event,type)andissubclass(event,Event):event=event.event_nameelse:raiseValueError(f"expected string event name or event class, got {event}")all_callbacks=list(self.js_event_callbacks.get(event,[]))forcallbackincallbacks:ifcallbacknotinall_callbacks:all_callbacks.append(callback)self.js_event_callbacks[event]=all_callbacks
[docs]defjs_link(self,attr:str,other:Model,other_attr:str,attr_selector:int|str|None=None)->None:''' Link two Bokeh model properties using JavaScript. This is a convenience method that simplifies adding a CustomJS callback to update one Bokeh model property whenever another changes value. Args: attr (str) : The name of a Bokeh property on this model other (Model): A Bokeh model to link to self.attr other_attr (str) : The property on ``other`` to link together attr_selector (Union[int, str]) : The index to link an item in a subscriptable ``attr`` Added in version 1.1 Raises: ValueError Examples: This code with ``js_link``: .. code :: python select.js_link('value', plot, 'sizing_mode') is equivalent to the following: .. code:: python from bokeh.models import CustomJS select.js_on_change('value', CustomJS(args=dict(other=plot), code="other.sizing_mode = this.value" ) ) Additionally, to use attr_selector to attach the left side of a range slider to a plot's x_range: .. code :: python range_slider.js_link('value', plot.x_range, 'start', attr_selector=0) which is equivalent to: .. code :: python from bokeh.models import CustomJS range_slider.js_on_change('value', CustomJS(args=dict(other=plot.x_range), code="other.start = this.value[0]" ) ) '''descriptor=self.lookup(attr,raises=False)ifdescriptorisNone:raiseValueError("%r is not a property of self (%r)"%(attr,self))ifnotisinstance(other,Model):raiseValueError("'other' is not a Bokeh model: %r"%other)other_descriptor=other.lookup(other_attr,raises=False)ifother_descriptorisNone:raiseValueError("%r is not a property of other (%r)"%(other_attr,other))frombokeh.modelsimportCustomJSselector=f"[{attr_selector!r}]"ifattr_selectorisnotNoneelse""cb=CustomJS(args=dict(other=other),code=f"other.{other_descriptor.name} = this.{descriptor.name}{selector}")self.js_on_change(attr,cb)
[docs]defjs_on_change(self,event:str,*callbacks:JSEventCallback)->None:''' Attach a ``CustomJS`` callback to an arbitrary BokehJS model event. On the BokehJS side, change events for model properties have the form ``"change:property_name"``. As a convenience, if the event name passed to this method is also the name of a property on the model, then it will be prefixed with ``"change:"`` automatically: .. code:: python # these two are equivalent source.js_on_change('data', callback) source.js_on_change('change:data', callback) However, there are other kinds of events that can be useful to respond to, in addition to property change events. For example to run a callback whenever data is streamed to a ``ColumnDataSource``, use the ``"stream"`` event on the source: .. code:: python source.js_on_change('streaming', callback) '''iflen(callbacks)==0:raiseValueError("js_on_change takes an event name and one or more callbacks, got only one parameter")# handle any CustomJS callbacks herefrombokeh.modelsimportCustomJSifnotall(isinstance(x,CustomJS)forxincallbacks):raiseValueError("not all callback values are CustomJS instances")descriptor=self.lookup(event,raises=False)ifdescriptorisnotNone:event=f"change:{descriptor.name}"old={k:[cbforcbincbs]fork,cbsinself.js_property_callbacks.items()}ifeventnotinself.js_property_callbacks:self.js_property_callbacks[event]=[]forcallbackincallbacks:ifcallbackinself.js_property_callbacks[event]:continueself.js_property_callbacks[event].append(callback)self.trigger('js_property_callbacks',old,self.js_property_callbacks)
[docs]defon_change(self,attr:str,*callbacks:PropertyCallback)->None:''' Add a callback on this object to trigger when ``attr`` changes. Args: attr (str) : an attribute name on this object *callbacks (callable) : callback functions to register Returns: None Example: .. code-block:: python widget.on_change('value', callback1, callback2, ..., callback_n) '''descriptor=self.lookup(attr)super().on_change(descriptor.name,*callbacks)
[docs]defreferences(self)->Set[Model]:''' Returns all ``Models`` that this object has references to. '''returnset(collect_models(self))
[docs]defselect(self,selector:SelectorType)->Iterable[Model]:''' Query this object and all of its references for objects that match the given selector. Args: selector (JSON-like) : Returns: seq[Model] '''from..core.queryimportfindreturnfind(self.references(),selector)
[docs]defselect_one(self,selector:SelectorType)->Model|None:''' Query this object and all of its references for objects that match the given selector. Raises an error if more than one object is found. Returns single matching object, or None if nothing is found Args: selector (JSON-like) : Returns: Model '''result=list(self.select(selector))iflen(result)>1:raiseValueError("Found more than one object matching %s: %r"%(selector,result))iflen(result)==0:returnNonereturnresult[0]
[docs]defset_select(self,selector:Type[Model]|SelectorType,updates:Dict[str,Unknown])->None:''' Update objects that match a given selector with the specified attribute/value updates. Args: selector (JSON-like) : updates (dict) : Returns: None '''ifisclass(selector)andissubclass(selector,Model):selector=dict(type=selector)forobjinself.select(selector):forkey,valinupdates.items():setattr(obj,key,val)
[docs]defto_json(self,include_defaults:bool)->JSON:''' Returns a dictionary of the attributes of this object, containing only "JSON types" (string, number, boolean, none, dict, list). References to other objects are serialized as "refs" (just the object ID and type info), so the deserializer will need to separately have the full attributes of those other objects. There's no corresponding ``from_json()`` because to deserialize an object is normally done in the context of a Document (since the Document can resolve references). For most purposes it's best to serialize and deserialize entire documents. Args: include_defaults (bool) : whether to include attributes that haven't been changed from the default '''returnloads(self.to_json_string(include_defaults=include_defaults))
[docs]defto_json_string(self,include_defaults:bool)->str:''' Returns a JSON string encoding the attributes of this object. References to other objects are serialized as references (just the object ID and type info), so the deserializer will need to separately have the full attributes of those other objects. There's no corresponding ``from_json_string()`` because to deserialize an object is normally done in the context of a Document (since the Document can resolve references). For most purposes it's best to serialize and deserialize entire documents. Args: include_defaults (bool) : whether to include attributes that haven't been changed from the default '''json_like=self._to_json_like(include_defaults=include_defaults)json_like['id']=self.id# serialize_json "fixes" the JSON from _to_json_like by converting# all types into plain JSON types # (it converts Model into refs,# for example).returnserialize_json(json_like)
[docs]deftrigger(self,attr:str,old:Unknown,new:Unknown,hint:DocumentPatchedEvent|None=None,setter:Setter|None=None)->None:''' '''# The explicit assumption here is that hinted events do not need to# go through all the same invalidation steps. Currently this is the# case for ColumnsStreamedEvent and ColumnsPatchedEvent. However,# this may need to be further refined in the future, if the# assumption does not hold for future hinted events (e.g. the hint# could specify explicitly whether to do normal invalidation or not)ifhintisNone:dirty_count=0defmark_dirty(_:HasProps):nonlocaldirty_countdirty_count+=1ifself._documentisnotNone:visit_value_and_its_immediate_references(new,mark_dirty)visit_value_and_its_immediate_references(old,mark_dirty)ifdirty_count>0:self.document.models.invalidate()# chain up to invoke callbacksdescriptor=self.lookup(attr)super().trigger(descriptor.name,old,new,hint=hint,setter=setter)
def_attach_document(self,doc:Document)->None:''' Attach a model to a Bokeh |Document|. This private interface should only ever called by the Document implementation to set the private ._document field properly '''ifself.documentisdoc:# nothing to doreturnifself.documentisnotNone:raiseRuntimeError(f"Models must be owned by only a single document, {self!r} is already in a doc")doc.theme.apply_to_model(self)self.document=docself._update_event_callbacks()@classmethoddef_clear_extensions(cls)->None:cls.model_class_reverse_map={k:vfork,vincls.model_class_reverse_map.items()ifgetattr(v,"__implementation__",None)isNoneandgetattr(v,"__javascript__",None)isNoneandgetattr(v,"__css__",None)isNone}def_detach_document(self)->None:''' Detach a model from a Bokeh |Document|. This private interface should only ever called by the Document implementation to unset the private ._document field properly '''self.document=Nonedefault_theme.apply_to_model(self)def_to_json_like(self,include_defaults:bool):''' Returns a dictionary of the attributes of this object, in a layout corresponding to what BokehJS expects at unmarshalling time. This method does not convert "Bokeh types" into "plain JSON types," for example each child Model will still be a Model, rather than turning into a reference, numpy isn't handled, etc. That's what "json like" means. This method should be considered "private" or "protected", for use internal to Bokeh; use ``to_json()`` instead because it gives you only plain JSON-compatible types. Args: include_defaults (bool) : whether to include attributes that haven't been changed from the default. '''all_attrs=self.properties_with_values(include_defaults=include_defaults)# If __subtype__ is defined, then this model may introduce properties# that don't exist on __view_model__ in bokehjs. Don't serialize such# properties.subtype=getattr(self.__class__,"__subtype__",None)ifsubtypeisnotNoneandsubtype!=self.__class__.__view_model__:attrs={}forattr,valueinall_attrs.items():ifattrinself.__class__.__dict__:continueelse:attrs[attr]=valueelse:attrs=all_attrsfor(k,v)inattrs.items():# we can't serialize Infinity, we send it as None and# the other side has to fix it up. This transformation# can't be in our json_encoder because the json# module checks for inf before it calls the custom# encoder.ifisinstance(v,float)andv==float('inf'):attrs[k]=Nonereturnattrsdef_repr_html_(self)->str:returnhtml_repr(self)def_sphinx_height_hint(self)->int|None:returnNone