Interactive controls for Jupyter notebooks: Python Examples

Interactive controls for Jupyter notebooks: Python Examples

Last updated:
Table of Contents

WIP Alert This is a work in progress. Current information is correct but more content may be added in the future.

Jupyter notebook available here: interactive-controls-post.ipynb

Input box

name=input()

When the cell is run, a text input shows up:

input-text-jupyter-notebook This is useful when you need to share notebooks
but there's user-specific information you want people
to fill out

Password input box

With password input, whatever is typed doesn't get shown on the screen

Use getpass (it's part of the Python standard library):

import getpass

password = getpass.getpass()

text-input-password Use this when you need sensitive information (e.g. database username and passwords)
but you don't want to hardcode that
information in the notebook, for security purposes.

Int Slider

See the docs for IntSlider

from ipywidgets import interact, widgets

# this is a sample function
def myfunc(x):
    return x*2

interact(myfunc,x=widgets.IntSlider(min=-10,max=10,value=0))

int-slider You can wrap any function with a call to interact and add a
key-word argument with the same name


Troubleshooting

[IPKernelApp] WARNING | Widget Javascript not detected. It may not be installed or enabled properly.

This happens when you don't have javascript widgets enabled on jupyter.

To fix this run this:

$ jupyter nbextension enable --py --sys-prefix widgetsnbextension
Enabling notebook extension jupyter-js-widgets/extension...
      - Validating: OK