RWTH Widgets

There are many ways of interactively using IPython’s widgets.

[1]:
import ipywidgets as widgets
import rwth_nb.plots.mpl_decorations as rwth_plots
/usr/local/lib/python3.8/site-packages/traitlets/traitlets.py:3030: FutureWarning: --rc={'figure.dpi': 96} for dict-traits is deprecated in traitlets 5.0. You can pass --rc <key=value> ... multiple times to add items to a dict.
  warn(

Interact

The easiest way to get started using IPython’s widgets is Interact.

It offers simple widgets from top to bottom, without any design.

Also, it will show up on call.

[2]:
# Define widgets
slider = widgets.FloatSlider(min=-4, max=4, value=-2, step=.1, description='Slider', style=rwth_plots.wdgtl_style)
checkbox = widgets.Checkbox(value=True, description='Checkbox', style=rwth_plots.wdgtl_style)

# interact
@widgets.interact(s=slider, c=checkbox)
def update(s, c):
    # do stuff with s and c here
    pass

Interactive

With interactive more complex designs are possible. (see: Styling)

Interactive returns a widget and does not display automatically.

[3]:
# define widget on change function
def update_signals(choose_A, opt1_A, opt2_A, choose_B, opt1_B, opt2_B):
    # do stuff with parameters and call other functions possibly
    pass

# Widgets
w_choose_A = widgets.Dropdown(options=['Option 1', 'Option 2', 'Option 3'], description=r'Choose A:', style=rwth_plots.wdgtl_style)
w_opt1_A = widgets.FloatSlider(min=0.5, max=4, value=1, step=.1, description=r'A float slider 1', style=rwth_plots.wdgtl_style)
w_opt2_A = s_t0=widgets.FloatSlider(min=-2, max=2, value=0, step=.1, description=r'A float slider 2', style=rwth_plots.wdgtl_style)
w_choose_B = widgets.Dropdown(options=['Option 1', 'Option 2', 'Option 3'], description=r'Choose B:', style=rwth_plots.wdgtl_style)
w_opt1_B = widgets.FloatSlider(min=0.5, max=4, value=1, step=.1, description=r'B float slider 1', style=rwth_plots.wdgtl_style)
w_opt2_B = s_t0=widgets.FloatSlider(min=-2, max=2, value=0, step=.1, description=r'B float slider 2', style=rwth_plots.wdgtl_style)

# interactive
# on change: update_signals is called using widgets' values
w = widgets.interactive(update_signals, choose_A=w_choose_A, opt1_A=w_opt1_A, opt2_A=w_opt2_A, choose_B=w_choose_B, opt1_B=w_opt1_B, opt2_B=w_opt2_B)

# custom display using 2 vertical boxes and 1 horizontal box
VBox_A = widgets.VBox((w_choose_A, w_opt1_A, w_opt2_A))
VBox_B = widgets.VBox((w_choose_B, w_opt1_B, w_opt2_B), layout=widgets.Layout(margin='0 0 0 100px'))

HBox_widget= widgets.HBox((VBox_A, VBox_B))

# display
display(HBox_widget);

Styling

RWTH widgets should always use style=mpl_decorations.wdgtl_style flag. This sets the widget label style to full width.

[4]:
w = widgets.FloatSlider(min=0, max=3, value=1.5, step=.5, description='full description is readable', style=rwth_plots.wdgtl_style)
display(w)

VBox/HBox

[5]:
words = ['A', 'B', 'C', 'D']
items = [widgets.Button(description=w) for w in words]
left_vbox = widgets.VBox([items[0], items[1]])
right_vbox = widgets.VBox([items[2], items[3]])
hbox = widgets.HBox([left_vbox, right_vbox])
display(hbox)

For more styling options, see Layout and Styling of Jupyter widgets.


This code is licensed under the MIT license.