Convert panel.widgets.tables.Tabulator to layoutDOM - bokeh

I'm new to Bokeh so apologies if I get the terminology wrong.
I have a simple dashboard and I'm trying to add a chart using tabulator to the page docs
The basic setup is as follows
from bokeh.models import Select, Panel
from bokeh.models.widgets import Tabs
import my_func from irrelevant_code
chart = my_func() # this is a tabulator object
tab1 = Panel(child = summary_layout, title="Summary")
tab2 = Panel(child = chart, title="Chart")
tabs = Tabs(tabs=[tab1, tab2])
document = curdoc()
document.add_root(tabs)
This runs into a problem since Panel expects a LayoutDOM object and chart is a panel.widgets.tables.Tabulator object.
How can I convert chart to a layoutDOM object?
The specific error I get is
*** ValueError: failed to validate Panel(id='1212', ...).child: expected an instance of type LayoutDOM, got Tabulator(formatters={'testDate': DateForm...}, groups={'testGroup': ['col1',...}, selectable='checkbox', selection=[0, 1, 2, 3, 4, ...], titles={'col1': 'Column 1', ...}, value= val1 val2 v...) of type Tabulator

So while in theory you could use the .get_root() or .get_model() methods on the Tabulator to turn the Panel object into a Bokeh object I would generally recommend just sticking with Panel, e.g. your example can be written as:
import panel as pn
import my_func from irrelevant_code
chart = my_func() # this is a tabulator object
tabs = pn.Tabs(('Summary', summary_layout), ('Chart', chart))
tabs.servable()

Related

pyqtgraph: How to link two PlotWidget windows to show the same plot?

I am developing an orbital analysis tool using PyQT5 and pyqtgraph!
See: https://github.com/3fon3fonov/trifon
My tool has a plotting area with ~15 plots shown in different tab windows, which show different aspects of the data analysis.
The tool it self is assembled with the Designer-qt5, while the QGraphicView widgets are promoted to pyqtgraphs's PlotWidgets
For example in the gui.py I initialize the plots like this:
def initialize_plots(self):
global p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,pe
p1 = self.graphicsView_timeseries_RV
p2 = self.graphicsView_timeseries_RV_o_c
p3 = self.graphicsView_timeseries_phot
p4 = self.graphicsView_timeseries_phot_o_c
p5 = self.graphicsView_timeseries_activity
p6 = self.graphicsView_timeseries_correlations
# ...
pe = self.graphicsView_extra_plot
# ...
so p1-p6 in this case are different PlotWidget objects on which I add Items/Plot data, i.e. p1.plot(x,y), p1.addItem(), etc.
What I want is to link pe to any of p1-p6!. pe is an extra plot so the user can choose from those already available/created.
Thus the user can select which plot he/she wants to see next to the main plot.
Lets imagine that the ComboBox dropdown menu selects between p1-p6 objects, so
pe = p1, or later: pe = p4
for example.
Is there any way this to be done with PyQtgraph?
I really tried all kind things in the last two weeks and nothing seems to work.
I am aware of the
pe.setXLink(p1)
pe.setYLink(p2)
but this only links the axes not the plot object itself. It doesn't work for me.
I implemented something like that using Docks and a DockArea. I simply added several docks stacked below each other.
They can be shown using either by clicking on the labels or by using the raiseDock() method of each dock.
You can simply add the PlotWidget (or any other Widget) to the dock using the addWidget() method of each dock.
The labels can be hidden or locked if you don't want the user to be able to move the docks at runtime.
import sys
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui
from pyqtgraph.dockarea import DockArea, Dock
class Accel_GUI():
def __init__(self, window, dock_area):
self.testing = 0
self.pen = pg.mkPen(color='y')
"""Setup the UI"""
self.window = window
self.dock_area = dock_area
self.window.setCentralWidget(self.dock_area)
self.spec_dock = []
self.spec_dock.append(Dock("Spectro 1",
size=(1200, 600),
autoOrientation=False))
self.spec_dock.append(Dock("Spectro 2",
size=(1200, 600),
autoOrientation=False))
self.dock_area.addDock(self.spec_dock[0], "top")
self.dock_area.addDock(self.spec_dock[1], "below", self.spec_dock[0])
if __name__ == "__main__":
app = QtGui.QApplication.instance()
if app is None:
app = QtGui.QApplication(argv)
win = QtGui.QMainWindow()
area = DockArea()
pyqtplot = Accel_GUI(win, area)
win.show()
app.exec_()
There is also an example in the pyqtgraph library that shows how to work with docks.

Spotfire - Is there a way to update a data function input parameters based on the slider property control values?

I have to change the axis columns for the custom plot(a plot which is added using RinR) based on the selection from the Slider property control.
I found this below code to be useful as it allows me to set the input parameter for my data function but I am not sure how to get the Slider value and to set the input as a column instead of a String as shown in this example.
from Spotfire.Dxp.Data import *
from Spotfire.Dxp.Data.DataFunctions import *
dataManager = Document.Data
dataFunction = None
for function in dataManager.DataFunctions:
if function.Name == 'NewDF1':
dataFunction = function
for inputs in dataFunction.DataFunctionDefinition.InputParameters:
if inputs.DisplayName=="inputName":
dataFunction.Inputs.SetInput(inputs,"String(\"HelloWorld\")")

ipywidgets dropdown widgets: what is the onchange event?

I can register a handler to button.on_click in ipython notebook widgets, but I don't know how to do the same for a dropdown widget
import ipywidgets as widgets
from IPython.display import display
def on_button_clicked(b):
print("Button clicked.")
button = widgets.Button(description="Click Me!")
display(button)
button.on_click(on_button_clicked)
But for
choose_task = widgets.Dropdown(
options=['Addition', 'Multiplication', 'Subtraction'],
value='Addition',
description='Task:',
)
there seems to be only
on_trait_change(...)
if I register a handler with this, can I use it to access the value of the widget?
I have seen examples with the handler and the widget belong to a subclass, and the handler can use self to introspect. But if I don't want to use a subclass, how does the handler know what widget was the target of the event.?
Between this link and the traitlet docs on github and just playing around, I finally figured this out:
w = widgets.Dropdown(
options=['Addition', 'Multiplication', 'Subtraction', 'Division'],
value='Addition',
description='Task:',
)
def on_change(change):
if change['type'] == 'change' and change['name'] == 'value':
print("changed to %s" % change['new'])
w.observe(on_change)
display(w)
Overall this looks a lot richer than the deprecated interface, but it could definitely use more examples.
You can specify the change name in observe. This makes for cleaner code, and the handler is not called for changes you don't need:
from IPython.display import display
from ipywidgets import Dropdown
def dropdown_eventhandler(change):
print(change.new)
option_list = (1, 2, 3)
dropdown = Dropdown(description="Choose one:", options=option_list)
dropdown.observe(dropdown_eventhandler, names='value')
display(dropdown)
Put it all together
Inspired on previous answers and lambda expressions I use this:
def function(option):
print(option)
w = widgets.Dropdown(
options=['None', 'Option 1', 'Option 2', 'Option 3'],
description='Option:',
disabled=False
)
w.observe(
lambda c: plot_content(c['new']) if (c['type'] == 'change' and c['name'] == 'value') else None
)
display(w)
I agree that event handling is not as thorough as would be desired: I have been filtering the events as you receive multiple events for a typical dropdown change as the index changes, the value changes, i.e., change['name'].
I am doing the following:
def on_dropdown_change(change):
if change['name'] == 'value' and (change['new'] != change['old']):
print('do something with the change')
dropdown = ipywidgets.Dropdown({options=['one','two','three'],
value='one'})
dropdown.observe(on_dropdown_change)
I believe the idea is to use trait name, e.g. value. For example:
from ipywidgets import Dropdown
def handle_change():
print type_sel.value
type_sel = Dropdown(description="Keypoint type", options=['surf', 'orb'])
type_sel.on_trait_change(handle_change, name="value")
display(type_sel)
SciPy 2015 Advanced Jupyter Video Tutorial
I had the same issue. This also begs the next question, how to interface button actions based on dropdown menu selections.
# Common Imports for Widgets
from __future__ import print_function
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
'''
Precusor:
<class 'traitlets.utils.bunch.Bunch'> It is a dictionary-like object containing:
{'name': 'value', 'old': 'what_ever_the_old_value_was', 'new': 'what_ever_the_new_value_is',
'owner': Dropdown(description='the_user_defined_label:', index=1, # I'm not sure what this is
options=()#list of options passed,
value='value_kwarg_value'), 'type': 'change'} # type: action_or_event type
For more information see:
https://traitlets.readthedocs.io/en/stable/using_traitlets.html#default-values-and-checking-type-and-value
or
https://github.com/jupyter-widgets/tutorial/blob/master/notebooks/08.00-Widget_Events.ipynb
or a long but well done SciPy talk on the use of widgets #
https://www.youtube.com/watch?v=HaSpqsKaRbo
'''
foo = ['a','b','c'] # List to use
# Function to apply to drop box object
def bar(x):
'''
I am intentionally passing what it is made of so you can see the output.
'''
print(x,'\n') # Whole object
print(x.new,'\n') # New value
# Function for the button to select user input and do work
def get_user_selection(a): # A default arg is needed here, I am guessing to pass self
# Displays the current value of dropbox1 and dropbox two
display(dropbox1.value,dropbox2.value)
# creation of a widget dropdown object called dropbox1
dropbox1 = widgets.Dropdown(
options=foo, # Object to iterate over
description='Letter:', # User defined
value=foo[1], # Default value selection
rows=len(foo), # The number of rows to display when showing the box
interactive=True, # This makes the box interactive, I believe this is true by default
);
# Drop box of k,v like pairs
dropbox2 = widgets.Dropdown(
options=[('One', 1), ('Two', 2), ('Three', 3)],
value=2,
description='Number:',
)
# Button to click
select_button = widgets.Button(
description='Click', # User defined
disabled=False
)
# Event Handlers
dropbox1.observe(bar,names='value')
dropbox2.observe(bar,names='value')
select_button.on_click(get_user_selection)
# I you need more help with commands try things like:
# interact_manual?
# display(arg.keys,arg.traits)
# print(widgets.widget_type_here.widget_function_or_attr.__doc__)
# Create a UI object to display things. There are other ways of organizing them.
ui = widgets.HBox([dropbox1,dropbox2,select_button]) # pass an array of widgets to the ui
# display the UI
display(ui)
This will display the following after a couple of clicks.

Using on_trait_change in ipython notebook widgets

I'm trying to use the IntSlider widget in IPython.html.widgets, and I want to call a function whenever the slider value changes. Additionally, I want to pass multiple arguments into the function. I was able to use on_trait_change() to call a function but have no idea how to pass arguments:
def somefn(parameter1, parameter2):
print (parameter1, parameter2)
slider = widgets.IntSlider(min=0,max=3,step=1)
slider.on_trait_change(somefn)
I would suggest using a partial.
from IPython.display import display
import ipywidgets as widgets
def somefn(parameter1,my_arg):
print ("parameter1: {}".format(parameter1))
print ("my_arg: {}".format(my_arg))
from functools import partial
somefn_arg_0 = partial(somefn, my_arg=0)
slider = widgets.IntSlider(min=0,max=3,step=1)
slider.observe(somefn_arg_0)
display(slider)
Note I have replaced the deprecated on_trait_change with observe.

Bokeh Interactively adding lines to figure

I want to plotting lines corresponding to the input user will give through InputText
the code snippet, example.py
region = pd.read_csv("region.csv", encoding="utf-8").set_index("plo_region")
TS_LENGTH=360
fig = figure(plot_width=700, plot_height=500, y_axis_type="log",y_range=(10**-2, 10**3))
# callback template
def callback_template(new, table, ts_length):
# whatever behavior
def callback_region(attr, old, new):
callback_template(new, region, TS_LENGTH)
# what to add line in callback function
curdoc().clear()
fig.line(x=[1,2,3], y=[1,2,3], legend="sdfds",color="black")
curdoc().add_root(hplot(inputs, fig))
textInput_region = TextInput(value="", title="region:")
textInput_region.on_change("value", callback_region)
controls = [textInput_region]
inputs = HBox(VBoxForm(controls), width=300)
# put the button and plot in a layout and add to the document
curdoc().add_root(hplot(inputs, fig))
# HOW TO SYNC THE CHANGE ON FIG TO BROWSER ??
so if the user gives "Berlin, Hamburg" in TextInput box, it will trigger the callback function and plotting two lines on the figure. Instead, if the user gives "Berlin, Hamburg, Hannover", it will plot three lines.

Resources