Calmcode - ipywidgets: buttons

Buttons

1 2 3 4 5 6 7 8

You can create buttons too with ipywidgets. These can be used to trigger python functions when you click them.

An example of this is shown 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])