I am using the following code to produce a tool in a Jupyter Notebook that allows the user to print a statement describing which coloured fruit they would like to try:
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)
The code comes from the answer to this Stack Overflow question.
When the user chooses a fruit and/or a color from the dropdown menus, an associated print statement should also be printed, reading "I would love to try a {color} {fruit}!". The following image, produced by the code above, show how the partial, expected output looks like in Jupyter Notebook:
However, I am trying to display "Choose a fruit!" right above the fruit dropdown menu and display "Choose a color right above the color dropdown menu, as so:
When I try to insert these print statements using the following code:
...
drop1 = ...
print("Hey")
drop2 = ...
print("Hello")
I see this, which is not what I want:
How can I modify the .interactive()
function line to insert the print statements I am trying to show?