LostinSpatialAnalysis
LostinSpatialAnalysis

Reputation: 641

how to build multiple dropdown prompt function using IPyWidget?

I am trying to build a simple tool in python here that simply asks the user to select a fruit type and then a color type from two drop down options and then, based on the user input, print a string that reads: "I would love to try a color fruit!". I am using IPyWidget for the dropdown functionality. I am trying to accomplish this in a Jupyter Notebook.

Here is what I have so far:

import ipywidgets as widgets

country_list=['Banana', 'Apple','Lemon','Orange']
color_list=['Blue', 'Red', 'Yellow']

def update_dropdown(change):

    print(change.new)

drop1 = widgets.Dropdown(options=country_list, value='Banana', description='Fruit:')
drop1.observe(update_dropdown,names='value')
display(drop1)

drop2 = widgets.Dropdown(options=color_list, value='Blue', description='Color:')
drop2.observe(update_dropdown,names='value')
display(drop2)

This directly produces these two dropdown, which is what I want to see: enter image description here

However, I am a bit unsure about the functionality. My dropdown options list the first two potential choices, and so below these dropdowns, I want to see the string "I would love to try a blue banana!" printed. And so I want each dropdown option to be stored in a variable, fruit and color, so that there is a dynamic/changing print function that responds directly to the dropdown options chosen. And so, once the user for example selects "Apple", the print statement would change to "I would love to try a blue apple!", and once the user then chooses "Yellow", the print statement would change to "I would love to try a yellow apple!", and so forth, only showing a single print statement at a time.

My confusion with accomplishing this, is I am not sure how to get those fruit and color variables set up, since they both are stored in 'change.new' in my code, within each dropdown function. When I try to run 'print(change.new)' after I just get an error reading: 'NameError: name 'change' is not defined', which tells me that outside of the dropdown code, "change" is not recognized.

How can I use my dropdown functionality to store both dropdown options as unique variables that can be used to to print unique strings in a subsequent print statement?

Upvotes: 0

Views: 1169

Answers (1)

medium-dimensional
medium-dimensional

Reputation: 2253

The following code might help:

import ipywidgets as widgets
from ipywidgets import interactive

fruits = ['Banana', 'Apple','Lemon','Orange']
colors = ['Blue', 'Red', 'Yellow']

drop1 = widgets.Dropdown(options=fruits, value='Banana', description='Fruit:', disabled=False)
drop2 = widgets.Dropdown(options=colors, value='Blue', description='Color:', disabled=False)

def update_dropdown(fruit, color):
    info = f"I would love to try a {color.lower()} {fruit.lower()}!"
    display(info)  
        
w = interactive(update_dropdown, fruit=drop1, color=drop2) 
display(w)

As soon as you run the cell with the above code, you should see:

enter image description here

which then gets updated as per the choices made by the user.

Upvotes: 2

Related Questions