traitlets in python
iPython traitlets is a multiuse library that can be used for enforcing specific trait attributes. They also can be used to facilitate quite a bit of communication between different ipywidgets, which is something I learned today.
Specifically, let’s say you have a plot that takes a dataframe in a matplotlib manner, such as bqplot:
import numpy as np import bqplot.pyplot as plt import pandas as pd x = pd.Series(np.linspace(-10, 10, 100) y = np.sin(x) fig = plt.figure() line = plt.plot(x=x, y=y) fig
Now let’s say you want to have an UI that:
- displays the entire dataframe as a filterable datagrid
- gives you options on what to plot against
- automatically updates the plot based on the filters in the datagrid
You can do this in shockingly few lines of Python using the ipydatagrid, bqplot, traitlets, and Jupyterwidgets.
Setup the jupyter dropdowns
Setup the datagrid
Setup the hook on the “transform” in the datagrid
Set the hook to take in the datagrid
Update the plot as needed