ipywidgets:
components
Usually code runs in your notebook because a cell is being run. What if instead it is triggered by a user interface element? It might be a better discovery tool or a better method for rapid prototyping. This is what the ipywidgets library tries to enable.
Notes
Instead of making a single component for a single chart, we'll make seperate components instead in this video.
import numpy as np
import matplotlib.pylab as plt
import ipywidgets as widgets
p = widgets.FloatSlider(
value=0.02,
min=0.01,
max=0.20,
step=0.001,
description='$$p$$',
readout=True,
readout_format='.3f',
)
html = widgets.HTMLMath(
value=f"Let's play around with the following formula: <br> $$np(1-p)^2$$",
)
def f(p):
n = np.linspace(1, 100, 100)
plt.plot(n, n*0.02*(1-0.02)**n, label="p=0.02");
plt.plot(n, n*p*(1-p)**n, label=f"p={np.round(p, 3)}");
plt.legend()
out = widgets.interactive_output(f, {'p': p})
widgets.VBox([html, p, out])
Feedback? See an issue? Something unclear? Feel free to mention it here.
If you want to be kept up to date, consider signing up for the newsletter.