Skip to content

Instantly share code, notes, and snippets.

@kervel
kervel / example1.py
Created April 2, 2020 14:05
say my name
def say_my_name(name):
"""
Print the current widget value in short sentence
"""
print(f'My name is {name}')
widgets.interact(say_my_name, name=["Jim", "Emma", "Bond"]);
%matplotlib widget
import ipywidgets as widgets
import matplotlib.pyplot as plt
import numpy as np
def say_my_name(name):
"""
Print the current widget value in short sentence
"""
print(f'My name is {name}')
widgets.interact(say_my_name, name=["Jim", "Emma", "Bond"]);
def say_something(x):
"""
Print the current widget value in short sentence
"""
print(f'Widget says: {x}')
widgets.interact(say_something, x=[0, 1, 2, 3])
widgets.interact(say_something, x=(0, 10, 1))
widgets.interact(say_something, x=(0, 10, .5))
_ = widgets.interact(say_something, x=True)
def three(x, y, z):
return (x, y, z)
_ = widgets.interact(
three,
x=(0, 10, 1),
y=True,
z=['a', 'b', 'c']
)
_ = widgets.interact(
three,
x=(0, 10, 1),
y=True,
z=widgets.fixed('I am fixed')
)
@widgets.interact(x=(0, 10, 1))
def foo(x):
"""
Print the current widget value in short sentence
"""
print(f'Slider says: {x}')
# set up plot
fig, ax = plt.subplots(figsize=(6, 4))
ax.set_ylim([-4, 4])
ax.grid(True)
# generate x values
x = np.linspace(0, 2 * np.pi, 100)
def my_sine(x, w, amp, phi):
widgets.Textarea(
'\n'.join([w for w in dir(widgets) if not w.islower()]),
layout=widgets.Layout(height='200px')
)
label = widgets.Label(
value='A label')
html = widgets.HTML(
value='<b>Formatted</b> <font color="red">html</font>',
description=''
)
text = widgets.Text(
value='A text field',