Some cool features that you can use with jupyter notebooks

Interact


In [1]:
from ipywidgets import interact, fixed

@interact(x=True, y=1.0, z=fixed(20))
def g(x, y, z):
    return (x, y, z)


(True, 1.0, 20)

In [2]:
from ipywidgets import interact, fixed

@interact(x={'one': 10, 'two': 20}, y=(-1.0, 10.0, 2.5))
def g(x, y):
    return (x, y)


(20, 4.0)

Widget Non exhaustive List


In [3]:
import ipywidgets as widgets

widgets.FloatSlider(
    value=7.5,
    min=5.0,
    max=10.0,
    step=0.1,
    description='Test',
    orientation='vertical',
)

In [4]:
widgets.FloatProgress(
    value=7.5,
    min=5.0,
    max=10.0,
    step=0.1,
    description='Loading:',
)

In [5]:
widgets.ToggleButton(
    description='Click me',
    value=False,
)

In [6]:
widgets.Valid(
    value=True,
)

In [7]:
widgets.RadioButtons(
    description='Pizza topping:',
    options=['pepperoni', 'pineapple', 'anchovies'],
)

In [8]:
widgets.ToggleButtons(
    description='Speed:',
    options=['Slow', 'Regular', 'Fast'],
)
Keyword argumentWidget
`True` or `False`Checkbox
`'Hi there'`Text
`value` or `(min,max)` or `(min,max,step)` if integers are passedIntSlider
`value` or `(min,max)` or `(min,max,step)` if floats are passedFloatSlider
`('orange','apple')` or `{'one':1,'two':2}`Dropdown

Interactive : reuse interact widget


In [9]:
from ipywidgets import interactive

def f(x, y):
    return x, y


widget = interactive(f, x=(0, 100, 5), y=('gauche', 'droite', 'haut', 'bas'))

In [10]:
widget


(50, u'gauche')

In [11]:
widget.kwargs


Out[11]:
{'x': 50, 'y': u'gauche'}

In [12]:
widget.children[0].value = 50

In [22]:
from IPython.display import display

name = widgets.Text(description='Name:', padding=4)
color = widgets.Dropdown(description='Color:', padding=4, options=['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'])
page1 = widgets.Box(children=[name, color], padding=4)

age = widgets.IntSlider(description='Age:', padding=4, min=0, max=120, value=50)
gender = widgets.RadioButtons(description='Gender:', padding=4, options=['male', 'female'])
page2 = widgets.Box(children=[age, gender], padding=4)

tabs = widgets.Tab(children=[page1, page2])
display(tabs)

tabs.set_title(0, 'Name')
tabs.set_title(1, 'Details')

In [14]:
from IPython.display import display

form = widgets.VBox()
first = widgets.Text(description="First:")
last = widgets.Text(description="Last:")

student = widgets.Checkbox(description="Student:", value=False)
school_info = widgets.VBox(visible=False, children=[
    widgets.Text(description="School:"),
    widgets.IntText(description="Grade:", min=0, max=12)
    ])

pet = widgets.Text(description="Pet:")
form.children = [first, last, student, school_info, pet]
display(form)

def on_student_toggle(name, value):
    if value:
        school_info.visible = True
    else:
        school_info.visible = False
student.on_trait_change(on_student_toggle, 'value')

Callback


In [15]:
from IPython.display import display
button = widgets.Button(description="Click Me!")
display(button)

def on_button_clicked(b):
    print("Button clicked.")

button.on_click(on_button_clicked)

Use bootstrap alerts

As of Jupyter 4.0, the widgets in this notebook won't show up on http://nbviewer.ipython.org. To view the widgets and interact with them, you will need to download this notebook and run it with a Jupyter Notebook server.
As of Jupyter 4.0, the widgets in this notebook won't show up on http://nbviewer.ipython.org. To view the widgets and interact with them, you will need to download this notebook and run it with a Jupyter Notebook server.
As of Jupyter 4.0, the widgets in this notebook won't show up on http://nbviewer.ipython.org. To view the widgets and interact with them, you will need to download this notebook and run it with a Jupyter Notebook server.
As of Jupyter 4.0, the widgets in this notebook won't show up on http://nbviewer.ipython.org. To view the widgets and interact with them, you will need to download this notebook and run it with a Jupyter Notebook server.

In [ ]: