0

Hi I have below dropdown:

from ipywidgets import interact, interactive, fixed, interact_manual 
import ipywidgets as widgets

def f(Books):
    return Books
interact(f, Books=['a','b','c','d']);

This populates a dropdown list allowing user to choose from a, b, c, d.

Let's say user chooses one of the four.

How do you reference the result?

I want to use the output of the dropdown as a variable in other formulas.

ex.

if Books = a:
x = 1+1
Aaron
  • 10,133
  • 1
  • 24
  • 40

1 Answers1

2

At the risk of recommending something that's generally terrible practice, I'd use a Global variable here. It's quick and easy, and given the interactive / prototyping nature of iPython, a lot of the usual arguments against global variables aren't super important.

Basically, every time you change a variable on the input of the interact widget, f() is called. So you simply must update this variable inside the function:

from ipywidgets import interact, interactive, fixed, interact_manual 
import ipywidgets as widgets

myvar = ''

def f(Books):
    global myvar
    myvar = Books
    return Books

interact(f, Books=['a','b','c','d']);
Aaron
  • 10,133
  • 1
  • 24
  • 40