Russell Burdt
Russell Burdt

Reputation: 2673

bokeh event to announce a button click on a plot RedoTool

Looking to run a callback in bokeh when there is a button click on a plot RedoTool. The following are available to import from bokeh.events and not clear if there is support for a button click on the RedoTool (and also the UndoTool).

    DocumentEvent           MouseWheel              RotateEnd
    DocumentReady           Pan                     RotateStart
    DoubleTap               PanEnd                  SelectionGeometry
    Event                   PanStart                Tap
    LODEnd                  Pinch                   _CONCRETE_EVENT_CLASSES
    LODStart                PinchEnd                __all__
    log                     PinchStart              __doc__
    logging                 PlotEvent               __file__
    MenuItemClick           PointEvent              __name__
    ModelEvent              Press                   __package__
    MouseEnter              PressUp
    MouseLeave              Reset```

Upvotes: 0

Views: 325

Answers (1)

Tony
Tony

Reputation: 8267

As far as I know there is nothing for this in bokeh.events, so you would need a workaround. In the past I was using the "Bokeh template" solution together with JQuery for this (still works for v2.1.1) You can access all Bokeh models by referring to its name attribute.

import os
from bokeh.io import save
from bokeh.plotting import figure
from bokeh.util.browser import view

template = """
{% block postamble %}
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <script>
        $(document).ready(function() {
            document.querySelectorAll('.bk-tool-icon-redo[title="Redo"]')
                .forEach(d => d.addEventListener('click', function(event) {
                    var p = Bokeh.documents[0].get_model_by_name('MyPlot')
                    var l = Bokeh.documents[0].get_model_by_name('MyLine')
                    console.log(l.data_source.data)
                    l.data_source.data["y"] = [3,2,1]
                    l.data_source.change.emit()
                    
                    alert('Redo button clicked for ' + p.title.text);
                  }));
                  
            document.querySelectorAll('.bk-tool-icon-undo[title="Undo"]')
                .forEach(d => d.addEventListener('click', function(event) {
                    var p = Bokeh.documents[0].get_model_by_name('MyPlot')
                    alert('Undo button clicked for ' + p.title.text);
                  }))
        });
    </script>
{% endblock %} """

p = figure(tools='pan, redo, undo, reset, save', title="Line Plot", name="MyPlot")
p.line([1, 2, 3], [1, 2, 3], name="MyLine")
save(p, template=template)
view(os.path.join(os.path.dirname(__file__), os.path.basename(__file__)).replace('.py', ".html"))

Upvotes: 1

Related Questions