#-----------------------------------------------------------------------------# 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.#-----------------------------------------------------------------------------''' Represent granular events that can be used to trigger callbacks.Bokeh documents and applications are capable of supporting various kinds ofinteractions. These are often associated with events, such as mouse or touchevents, interactive downsampling mode activation, widget or tool events, andothers. The classes in this module represent these different events, so thatcallbacks can be attached and executed when they occur.It is possible to respond to events with ``CustomJS`` callbacks, which willfunction with or without a Bokeh server. This can be accomplished by passingand event class, and a ``CustomJS`` model to the:func:`~bokeh.model.Model.js_on_event` method. When the ``CustomJS`` isexecuted in the browser, its ``cb_obj`` argument will contain the concreteevent object that triggered the callback... code-block:: python from bokeh.events import ButtonClick from bokeh.models import Button, CustomJS button = Button() button.js_on_event(ButtonClick, CustomJS(code='console.log("JS:Click")'))Alternatively it is possible to trigger Python code to run when eventshappen, in the context of a Bokeh application running on a Bokeh server.This can accomplished by passing an event class, and a callback functionto the the :func:`~bokeh.model.Model.on_event` method. The callback shouldaccept a single argument ``event``, which will be passed the concreteevent object that triggered the callback... code-block:: python from bokeh.events import ButtonClick from bokeh.models import Button button = Button() def callback(event): print('Python:Click') button.on_event(ButtonClick, callback).. note :: There is no throttling of events. Some events such as ``MouseMove`` may trigger at a very high rate.'''#-----------------------------------------------------------------------------# Boilerplate#-----------------------------------------------------------------------------from__future__importannotationsimportlogging# isort:skiplog=logging.getLogger(__name__)#-----------------------------------------------------------------------------# Imports#-----------------------------------------------------------------------------# Standard library importsfromtypingimport(TYPE_CHECKING,Any,ClassVar,Literal,Type,TypedDict,)# Bokeh importsfrom.core.serializationimportDeserializerifTYPE_CHECKING:from.core.typesimportGeometryDatafrom.modelimportModelfrom.models.plotsimportPlotfrom.models.widgets.buttonsimportAbstractButton#-----------------------------------------------------------------------------# Globals and constants#-----------------------------------------------------------------------------__all__=('ButtonClick','DocumentEvent','DocumentReady','DoubleTap','Event','LODStart','LODEnd','MenuItemClick','ModelEvent','MouseEnter','MouseLeave','MouseMove','MouseWheel','Pan','PanEnd','PanStart','Pinch','PinchEnd','PinchStart','RangesUpdate','Rotate','RotateEnd','RotateStart','PlotEvent','PointEvent','Press','PressUp','Reset','SelectionGeometry','Tap',)#-----------------------------------------------------------------------------# Private API#-----------------------------------------------------------------------------_CONCRETE_EVENT_CLASSES:dict[str,Type[Event]]={}#-----------------------------------------------------------------------------# General API#-----------------------------------------------------------------------------classEventRep(TypedDict):type:Literal["event"]name:strvalues:dict[str,Any]
[docs]classEvent:''' Base class for all Bokeh events. This base class is not typically useful to instantiate on its own. '''event_name:ClassVar[str]@classmethoddef__init_subclass__(cls):super().__init_subclass__()ifhasattr(cls,"event_name"):_CONCRETE_EVENT_CLASSES[cls.event_name]=cls@classmethoddeffrom_serializable(cls,rep:EventRep,decoder:Deserializer)->Event:ifnot("name"inrepand"values"inrep):decoder.error("invalid representation")name=rep.get("name")ifnameisNone:decoder.error("'name' field is missing")values=rep.get("values")ifvaluesisNone:decoder.error("'values' field is missing")cls=_CONCRETE_EVENT_CLASSES.get(name)ifclsisNone:decoder.error(f"can't resolve event '{name}'")decoded_values=decoder.decode(values)event=cls(**decoded_values)returnevent
[docs]classDocumentEvent(Event):''' Base class for all Bokeh Document events. This base class is not typically useful to instantiate on its own. '''
[docs]classDocumentReady(DocumentEvent):''' Announce when a Document is fully idle. '''event_name='document_ready'
[docs]classModelEvent(Event):''' Base class for all Bokeh Model events. This base class is not typically useful to instantiate on its own. '''model:Model|None
[docs]def__init__(self,model:Model|None)->None:''' Create a new base event. Args: model (Model) : a Bokeh model to register event callbacks on '''self.model=model
[docs]classButtonClick(ModelEvent):''' Announce a button click event on a Bokeh button widget. '''event_name='button_click'def__init__(self,model:AbstractButton|None)->None:from.models.widgetsimportAbstractButton,ToggleButtonGroupifmodelisnotNoneandnotisinstance(model,(AbstractButton,ToggleButtonGroup)):clsname=self.__class__.__name__raiseValueError(f"{clsname} event only applies to button and button group models")super().__init__(model=model)
[docs]classMenuItemClick(ModelEvent):''' Announce a button click event on a Bokeh menu item. '''event_name='menu_item_click'def__init__(self,model:Model,item:str|None=None)->None:self.item=itemsuper().__init__(model=model)
[docs]classPlotEvent(ModelEvent):''' The base class for all events applicable to Plot models. '''def__init__(self,model:Plot|None)->None:from.modelsimportPlotifmodelisnotNoneandnotisinstance(model,Plot):raiseValueError(f"{self.__class__.__name__} event only applies to Plot models")super().__init__(model)
[docs]classLODStart(PlotEvent):''' Announce the start of "interactive level-of-detail" mode on a plot. During interactive actions such as panning or zooming, Bokeh can optionally, temporarily draw a reduced set of the data, in order to maintain high interactive rates. This is referred to as interactive Level-of-Detail (LOD) mode. This event fires whenever a LOD mode has just begun. '''event_name='lodstart'
[docs]classLODEnd(PlotEvent):''' Announce the end of "interactive level-of-detail" mode on a plot. During interactive actions such as panning or zooming, Bokeh can optionally, temporarily draw a reduced set of the data, in order to maintain high interactive rates. This is referred to as interactive Level-of-Detail (LOD) mode. This event fires whenever a LOD mode has just ended. '''event_name='lodend'
[docs]classRangesUpdate(PlotEvent):''' Announce combined range updates in a single event. Attributes: x0 (float) : start x-coordinate for the default x-range x1 (float) : end x-coordinate for the default x-range y0 (float) : start x-coordinate for the default y-range y1 (float) : end y-coordinate for the default x-range Callbacks may be added to range ``start`` and ``end`` properties to respond to range changes, but this can result in multiple callbacks being invoked for a single logical operation (e.g. a pan or zoom). This event is emitted by supported tools when the entire range update is complete, in order to afford a *single* event that can be responded to. '''event_name='rangesupdate'def__init__(self,model:Plot|None,*,x0:float|None=None,x1:float|None=None,y0:float|None=None,y1:float|None=None):self.x0=x0self.x1=x1self.y0=y0self.y1=y1super().__init__(model=model)
[docs]classSelectionGeometry(PlotEvent):''' Announce the coordinates of a selection event on a plot. Attributes: geometry (dict) : a dictionary containing the coordinates of the selection event. final (bool) : whether the selection event is the last selection event in the case of selections on every mousemove. '''event_name="selectiongeometry"def__init__(self,model:Plot|None,geometry:GeometryData|None=None,final:bool=True)->None:self.geometry=geometryself.final=finalsuper().__init__(model=model)
[docs]classReset(PlotEvent):''' Announce a button click event on a plot ``ResetTool``. '''event_name="reset"def__init__(self,model:Plot|None)->None:super().__init__(model=model)
[docs]classPointEvent(PlotEvent):''' Base class for UI events associated with a specific (x,y) point. Attributes: sx (float) : x-coordinate of the event in *screen* space sy (float) : y-coordinate of the event in *screen* space x (float) : x-coordinate of the event in *data* space y (float) : y-coordinate of the event in *data* space Note that data space coordinates are relative to the default range, not any extra ranges, and the the screen space origin is at the top left of the HTML canvas. '''def__init__(self,model:Plot|None,sx:float|None=None,sy:float|None=None,x:float|None=None,y:float|None=None):self.sx=sxself.sy=syself.x=xself.y=ysuper().__init__(model=model)
[docs]classTap(PointEvent):''' Announce a tap or click event on a Bokeh plot. Attributes: sx (float) : x-coordinate of the event in *screen* space sy (float) : y-coordinate of the event in *screen* space x (float) : x-coordinate of the event in *data* space y (float) : y-coordinate of the event in *data* space '''event_name='tap'
[docs]classDoubleTap(PointEvent):''' Announce a double-tap or double-click event on a Bokeh plot. Attributes: sx (float) : x-coordinate of the event in *screen* space sy (float) : y-coordinate of the event in *screen* space x (float) : x-coordinate of the event in *data* space y (float) : y-coordinate of the event in *data* space '''event_name='doubletap'
[docs]classPress(PointEvent):''' Announce a press event on a Bokeh plot. Attributes: sx (float) : x-coordinate of the event in *screen* space sy (float) : y-coordinate of the event in *screen* space x (float) : x-coordinate of the event in *data* space y (float) : y-coordinate of the event in *data* space '''event_name='press'
[docs]classPressUp(PointEvent):''' Announce a pressup event on a Bokeh plot. Attributes: sx (float) : x-coordinate of the event in *screen* space sy (float) : y-coordinate of the event in *screen* space x (float) : x-coordinate of the event in *data* space y (float) : y-coordinate of the event in *data* space '''event_name='pressup'
[docs]classMouseEnter(PointEvent):''' Announce a mouse enter event onto a Bokeh plot. Attributes: sx (float) : x-coordinate of the event in *screen* space sy (float) : y-coordinate of the event in *screen* space x (float) : x-coordinate of the event in *data* space y (float) : y-coordinate of the event in *data* space .. note:: The enter event is generated when the mouse leaves the entire Plot canvas, including any border padding and space for axes or legends. '''event_name='mouseenter'
[docs]classMouseLeave(PointEvent):''' Announce a mouse leave event from a Bokeh plot. Attributes: sx (float) : x-coordinate of the event in *screen* space sy (float) : y-coordinate of the event in *screen* space x (float) : x-coordinate of the event in *data* space y (float) : y-coordinate of the event in *data* space .. note:: The leave event is generated when the mouse leaves the entire Plot canvas, including any border padding and space for axes or legends. '''event_name='mouseleave'
[docs]classMouseMove(PointEvent):''' Announce a mouse movement event over a Bokeh plot. Attributes: sx (float) : x-coordinate of the event in *screen* space sy (float) : y-coordinate of the event in *screen* space x (float) : x-coordinate of the event in *data* space y (float) : y-coordinate of the event in *data* space .. note:: This event can fire at a very high rate, potentially increasing network traffic or CPU load. '''event_name='mousemove'
[docs]classMouseWheel(PointEvent):''' Announce a mouse wheel event on a Bokeh plot. Attributes: delta (float) : the (signed) scroll speed sx (float) : x-coordinate of the event in *screen* space sy (float) : y-coordinate of the event in *screen* space x (float) : x-coordinate of the event in *data* space y (float) : y-coordinate of the event in *data* space .. note:: By default, Bokeh plots do not prevent default scroll events unless a ``WheelZoomTool`` or ``WheelPanTool`` is active. This may change in future releases. '''event_name='wheel'def__init__(self,model:Plot|None,*,delta:float|None=None,sx:float|None=None,sy:float|None=None,x:float|None=None,y:float|None=None):self.delta=deltasuper().__init__(model,sx=sx,sy=sy,x=x,y=y)
[docs]classPan(PointEvent):''' Announce a pan event on a Bokeh plot. Attributes: delta_x (float) : the amount of scroll in the x direction delta_y (float) : the amount of scroll in the y direction direction (float) : the direction of scroll (1 or -1) sx (float) : x-coordinate of the event in *screen* space sy (float) : y-coordinate of the event in *screen* space x (float) : x-coordinate of the event in *data* space y (float) : y-coordinate of the event in *data* space '''event_name='pan'def__init__(self,model:Plot|None,*,delta_x:float|None=None,delta_y:float|None=None,direction:Literal[-1,-1]|None=None,sx:float|None=None,sy:float|None=None,x:float|None=None,y:float|None=None):self.delta_x=delta_xself.delta_y=delta_yself.direction=directionsuper().__init__(model,sx=sx,sy=sy,x=x,y=y)
[docs]classPanEnd(PointEvent):''' Announce the end of a pan event on a Bokeh plot. Attributes: sx (float) : x-coordinate of the event in *screen* space sy (float) : y-coordinate of the event in *screen* space x (float) : x-coordinate of the event in *data* space y (float) : y-coordinate of the event in *data* space '''event_name='panend'
[docs]classPanStart(PointEvent):''' Announce the start of a pan event on a Bokeh plot. Attributes: sx (float) : x-coordinate of the event in *screen* space sy (float) : y-coordinate of the event in *screen* space x (float) : x-coordinate of the event in *data* space y (float) : y-coordinate of the event in *data* space '''event_name='panstart'
[docs]classPinch(PointEvent):''' Announce a pinch event on a Bokeh plot. Attributes: scale (float) : the (signed) amount of scaling sx (float) : x-coordinate of the event in *screen* space sy (float) : y-coordinate of the event in *screen* space x (float) : x-coordinate of the event in *data* space y (float) : y-coordinate of the event in *data* space .. note:: This event is only applicable for touch-enabled devices. '''event_name='pinch'def__init__(self,model:Plot|None,*,scale:float|None=None,sx:float|None=None,sy:float|None=None,x:float|None=None,y:float|None=None):self.scale=scalesuper().__init__(model,sx=sx,sy=sy,x=x,y=y)
[docs]classPinchEnd(PointEvent):''' Announce the end of a pinch event on a Bokeh plot. Attributes: sx (float) : x-coordinate of the event in *screen* space sy (float) : y-coordinate of the event in *screen* space x (float) : x-coordinate of the event in *data* space y (float) : y-coordinate of the event in *data* space .. note:: This event is only applicable for touch-enabled devices. '''event_name='pinchend'
[docs]classPinchStart(PointEvent):''' Announce the start of a pinch event on a Bokeh plot. Attributes: sx (float) : x-coordinate of the event in *screen* space sy (float) : y-coordinate of the event in *screen* space x (float) : x-coordinate of the event in *data* space y (float) : y-coordinate of the event in *data* space .. note:: This event is only applicable for touch-enabled devices. '''event_name='pinchstart'
[docs]classRotate(PointEvent):''' Announce a rotate event on a Bokeh plot. Attributes: rotation (float) : the rotation that has been done (in deg) sx (float) : x-coordinate of the event in *screen* space sy (float) : y-coordinate of the event in *screen* space x (float) : x-coordinate of the event in *data* space y (float) : y-coordinate of the event in *data* space .. note:: This event is only applicable for touch-enabled devices. '''event_name='rotate'def__init__(self,model:Plot|None,*,rotation:float|None=None,sx:float|None=None,sy:float|None=None,x:float|None=None,y:float|None=None):self.rotation=rotationsuper().__init__(model,sx=sx,sy=sy,x=x,y=y)
[docs]classRotateEnd(PointEvent):''' Announce the end of a rotate event on a Bokeh plot. Attributes: sx (float) : x-coordinate of the event in *screen* space sy (float) : y-coordinate of the event in *screen* space x (float) : x-coordinate of the event in *data* space y (float) : y-coordinate of the event in *data* space .. note:: This event is only applicable for touch-enabled devices. '''event_name='rotateend'
[docs]classRotateStart(PointEvent):''' Announce the start of a rotate event on a Bokeh plot. Attributes: sx (float) : x-coordinate of the event in *screen* space sy (float) : y-coordinate of the event in *screen* space x (float) : x-coordinate of the event in *data* space y (float) : y-coordinate of the event in *data* space .. note:: This event is only applicable for touch-enabled devices. '''event_name='rotatestart'
#-----------------------------------------------------------------------------# Dev API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Code#-----------------------------------------------------------------------------Deserializer.register("event",Event.from_serializable)