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?
-
1it really is incredible how poorly documented these simple features are. it is easy to find out how to draw a widget. plenty of beautiful examples. want to get a value from it? welcome to hell. – eric Jan 14 '21 at 16:04
3 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
-
3Is there a typo in the example? Should it be: `output_text = widgets.Text()` Not: `output_variable = widgets.Text()` – hmelberg May 17 '17 at 00:31
-
@harringr, are both input and output text necessary? Given a single variable `text`, then `text.on_submit(lambda x:x)` `text.value` is available for further use or processing. Is there a limitation to this approach? – alancalvitti May 13 '19 at 20:40
-
how do I use more than 1 slider? (example, looks like I can only use output_slider_variable.value and that means a single var across multiple sliders) – thistleknot Nov 21 '20 at 23:13
Another solution that may be easier is to use interactive
. It acts a lot like interact
, but allows you access to the returned value in later cells while creating only a single widget.
A simple example is below, and more complete documentation is here
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)
You can now access the result value, and also the values of the widgets if desired.
my_result.result # current value of returned object (in this case a+b)
my_result.children[0].value # current value of a
my_result.children[1].value # current value of b
-
1This solution doesn't work for me - the my_result.result attribute never updates from the initial value. – ThomasNicholas Jan 17 '20 at 23:03
-
1I see now - it only updates when called from a later notebook cell, it doesn't update the value within this code block. – ThomasNicholas Jan 17 '20 at 23:45
-
1@ThomasNicholas , one of the most direct ways to get this code block to work within a single Jupyter cell is to make a global variable inside the interactive function. You need to add three lines, or just two if you don't want to display the value as you update it. So above the `return a + b` line, add three lines. From top to bottom those lines are: `global the_current_value`, `the_current_value = a + b`, finally `display(the_current_value)`. Leave off the last if you don't want the value to show as you adjust the sliders. You can use `the_current_value` later in the cells too. – Wayne Mar 30 '23 at 15:07
-
@ThomasNicholas I added the code as a complete block as answer on this page now. Also, I'll point out these days there may be an asyncio way to do this that wasn't addressed in this thread anywhere yet. – Wayne Mar 30 '23 at 15:39
This is to tag onto elz's answer to address ThomasNichols' comments. See my comment here. This code block puts together what I suggest, building on elz's answer:
from ipywidgets import interactive
from IPython.display import display
# Define any function
def f(a, b):
global the_current_value
the_current_value = a + b
display(the_current_value)
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)
Others reading this thread may find it useful, and so I'll note I've added an example based on the use of interactive()
to have the value selected by a widget get used in realtime, automatic updating in another widget update upon selection under 'Adapting the streamlined version to display the value in a second widget' here.
Asyncio may be the way to go if interactive()
not useable
As I pointed out at the bottom of here, this solution to Is it possible to get the current value of a widget slider from a function without using multithreading? might be of interest to those looking for access current values. In that example the use of asyncio there allows to keep getting updated information from a widget that selects a value while allowing use of time.sleep()
which seems to block interactive()
from working.

- 6,607
- 8
- 36
- 93