Calmcode - ipywidgets: interact

Interact

1 2 3 4 5 6 7 8

Let's make a first ipywidget by making a simple function with a slider attached. We can do this with a decorator from jupyter lab. Here's an example.

from ipywidgets import interact

import numpy as np
import matplotlib.pylab as plt

@interact(p=(0.001, 0.2, 0.01))
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()