Calmcode - ipywidgets: more buttons

More Buttons

1 2 3 4 5 6 7 8

Let's look at the code below.

import numpy as np
import matplotlib.pylab as plt
import ipywidgets as widgets

button = widgets.Button(description="Click Me!")
output = widgets.Output()

def on_button_clicked(b):
    r = np.random.normal(0, 1, 1)
    with output:
        output.clear_output()
        x1 = np.random.normal(r, 1.0, 1_000_000)
        plt.hist(x1, bins=30, label="standard", alpha=0.6)
        plt.title(f"r={r[0]}")
        plt.show()

button.on_click(on_button_clicked)

widgets.HBox([button, output])

Given that the button is defined, you're totally free to re-use it elsewhere!