logo

... ipywidgets: buttons



Notes

You can create buttons too. You can attach custom functions that run when you click these buttons too!

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])

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.