Plot Actions

Plots can be configured to run code or other cells when the user clicks on or types into them.

In [ ]:
from beakerx import *
from random import randint
In [ ]:
abc = 0 # test variable
In [ ]:
p = Plot(showLegend = True, useToolTip= False)

def on_click1(info):
    info.graphics.display_name = "new name"
    
def on_click2(info):
    info.graphics.y[0] = randint(0, 9)
    
p.add(Line(x = [1, 2, 3], y = [2, 3, 4], width = 10, displayName = "line 1").onClick(on_click1))
p.add(Line(x = [1, 2, 3], y = [5, 6, 7], width = 10, displayName = "line 2").onClick(on_click2))
In [ ]:
p = Plot(showLegend = True, useToolTip = False)

def on_click(info):
    global abc
    abc += 1
    beakerx.runByTag("on_click_any_action")
    

p.add(Line(x = [1, 2, 3], y = [2, 3, 4], width = 10, displayName = "line 1").onClick(on_click))
In [ ]:
print(abc)
In [ ]:
plot = Plot(useToolTip = False)
plot.add(Points(x = list(range(1, 6)), y = list(range(1, 6)), size = 12, color = Color.orange, outlineColor = Color.black, displayName = "orange").onClick("run_tag"))
plot.add(Points(x = list(range(1, 6)), y = list(range(3, 9)), size = 12, color = Color.green, outlineColor = Color.black, displayName = "green").onClick("run_tag"))
In [ ]:
details = plot.details
item = details.graphics
index = details.index
key = details.key
tag = details.tag
action = details.actionType
print("You clicked on {} {} (element with coordinates [{},{}])".format(item.display_name, type(item).__name__, item.x[index], item.y[index]))
print("Key pressed = {} Tag = {} Action = {}".format(key, tag, action))
In [ ]:
barsPlot = Plot(useToolTip = False);
bars = Bars(x = list(range(1,6)), y = [5, 2, 4, 3, 7], color = Color.green, outlineColor = Color.black, width = 0.3)


def on_space(info):
    info.graphics.y[info.index] += 1
    
def on_caps_lock(info):
    info.graphics.y[info.index] -= 1

#Also buttons like KeyboardCodes.UP_ARROW is handled by jupyter notebook
bars.onKey(KeyboardCodes.SPACE, on_space)

#Also buttons like KeyboardCodes.DOWN_ARROW is handled by jupyter notebook
bars.onKey(KeyboardCodes.CAPS_LOCK, on_caps_lock)
        
#Tag events working
bars.onKey("T", "run_tag2")

barsPlot.add(bars)
In [ ]:
details = barsPlot.details
item = details.graphics
index = details.index
key = details.key
tag = details.tag
action = details.actionType
print("Key action on {} (element with coordinates [{}, {}])".format(type(item).__name__, item.x[index], item.y[index]))
print("Key pressed  = {} Tag = {} Action = {}".format(key, tag, action))
In [ ]:
barsPlot = Plot(useToolTip = False);
bars = Bars(x = list(range(1,5)), y = [5, 2, 4, 3, 7], color = Color.green, outlineColor = Color.black, width = 0.3)
        
#Buttons like KeyboardCodes.UP_ARROW is handled by jupyter notebook
def on_space(info):
    global abc
    abc+=1
    beakerx.runByTag('run_tag3')
    
bars.onKey(KeyboardCodes.SPACE, on_space)
barsPlot.add(bars)
In [ ]:
print(abc)