In the previous article, we have told you how to add rich media output components ( audio, video, image, etc) in jupyter notebook. But all those widgets are not interactive. This article will show you an example of how to add a slide bar in jupyter notebook. When you slide the bar, it will change the python function input parameter value to the slide bar value accordingly, and then the python function result will be displayed on the jupyter notebook web page.
1. Install ipywidgets In Python Library.
- Check whether ipywidgets has been installed in your python environment. If you see the below output, that means ipywidgets has been installed.
(python_example) C:\Users\zhaosong>pip show ipywidgets Name: ipywidgets Version: 7.5.1 Summary: IPython HTML widgets for Jupyter Home-page: http://ipython.org Author: IPython Development Team Author-email: ipython-dev@scipy.org License: BSD Location: c:\users\zhaosong\anaconda3\envs\python_example\lib\site-packages Requires: ipython, nbformat, widgetsnbextension, ipykernel, traitlets Required-by:
- If ipywidgets has not been installed, run the below code to install it.
> pip install ipywidgets
2. Use Interact Decorator To Add Slide Bar To Python Function.
- To add a slide bar to a special python function in jupyter notebook, you just need to import
ipywidget.interact
decorator, then use it to decorate the python function, then the slide bar current value will be passed to the python function by the x argument. - You can add one slide bar to one python function defined in the jupyter notebook by decorating each function with
@interact
decorator. If your ipython version is before 4.0, then you should useIPython.html.widgets.interact
to define the interactive slide bar.
3. Add Slide Bar To Python Function By @interact Decorator Example.
- This example defines two python function, and add
@interact
decorator to each of the function. Input below python source code in jupyter notebook line cell.# import ipywidgets.interact class, this class will represent a slide bar. from ipywidgets import interact # use interact decorator to decorate the function, so the function can receive the slide bar's value with parameter x. @interact(x=(0, 100)) def double_number(x): print("The double of %d is %d." % (x, x*2)) # add another slide bar to this function with the interact decorator. @interact(y=(0, 10)) def square_number(y): print("The square of %d is %d." % (y, y**2))
- When you run the above source code, there will display two slide bars under the above code, and you can slide each bar to see the python function output on the web page.
4. Question & Answer.
4.1 The Interactive Slider Disappear.
- I create a jupyter notebook, and there is a table and a slider in the notebook. When I slide the slider, it will display different date range data in the table. But recently when I run the notebook again, I can not find the slider anymore, it does not show any error message, I do not change anything to my source code and environment, can anybody tell me why and how to fix it? Thanks.
- First, make sure you have installed the ipywidgets with the command pip show ipywidgets in a terminal.
- If the ipywidgets python library is not installed on your virtual python environment ( maybe it has been uninstalled that you do not find ), you need to run the command pip install ipywidgets to install it.
- If you have installed it, you can run the command jupyter nbextension enable –py –sys-prefix widgetsnbextension to enable it.