Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Ipython ipywidget to create a variable?

Tags:

This seems really simple but I have not been able to find a single example or to solve this myself. How do I use an ipywidget widget to create or return a python variable/object, such as a list or string, that can be used in a following cell?

like image 491
Little Bobby Tables Avatar asked Feb 12 '16 11:02

Little Bobby Tables


People also ask

What is ipywidgets in Python?

ipywidgets provides a list of functions which can let us create widgets UI for any of our existing function. It'll create widgets by itself by looking at parameters of functions and create widgets UI with all parameters represented as one widget. It's a very good way to start using ipywidgets.

How to display UI of widgets in IPython widgets?

ipywidgets provides another function called interactive () to create UI of widgets by passing a function to it. Unlike interact () function, interactive () returns objects which does not displays widgets automatically. We need to use IPython function display () to display widgets UI as well as the output of a function.

How to create interactive sliders in Python using ipywidgets?

from ipywidgets import interactive from IPython.display import display # Define any function def f (a, b): return a + b # Create sliders using interactive my_result = interactive (f, a= (1,5), b= (6,10)) # You can also view this in a notebook without using display. display (my_result)

Do I need to learn JavaScript to use ipywidgets?

This way you won't need to learn javascript and continue to use python by just learning ipywidgets. Every widget generated by ipywidgets consists of two components behind the scene: Python - It runs in jupyter notebook kernel. Javascript - It runs in browser.


1 Answers

There is a good introduction to ipywidgets at http://blog.dominodatalab.com/interactive-dashboards-in-jupyter/ which answers this question.

You need two widgets, one for input, and another to bind the value of that input. Here's an example for text input:

from ipywidgets import widgets    # Create text widget for output output_text = widgets.Text()  # Create text widget for input input_text = widgets.Text()  # Define function to bind value of the input to the output variable  def bind_input_to_output(sender):     output_text.value = input_text.value  # Tell the text input widget to call bind_input_to_output() on submit input_text.on_submit(bind_input_to_output)  # Display input text box widget for input input_text  # Display output text box widget (will populate when value submitted in input) output_text  # Display text value of string in output_text variable output_text.value  # Define new string variable with value of output_text, do something to it uppercase_string = output_text.value.upper() print uppercase_string 

You can then use the uppercase_string, or output_text.value string, for example, throughout your notebook.

A similar pattern can be followed for using other input values, e.g. the interact() slider:

from ipywidgets import widgets, interact  # Create text widget for output output_slider_variable = widgets.Text()  # Define function to bind value of the input to the output variable  def f(x):     output_slider_variable.value = str(x)  # Create input slider with default value = 10     interact(f, x=10)  # Display output variable in text box output_slider_variable  # Create and output new int variable with value of slider new_variable = int(output_slider_variable.value) print new_variable  # Do something with new variable, e.g. cube new_variable_cubed = pow(new_variable, 3) print new_variable_cubed 

Screenshot of iPython notebook to illustrate binding variables from ipywidgets Text() and interact() for use throughout notebook

like image 150
harringr Avatar answered Sep 20 '22 09:09

harringr