0

I have been going in circles for hours. I am trying to get the dropdown output to loop, to ensure the result is correct.

I get the dropdown list, but the "output" is none. If I select 'DEV' or "DEV", it prints DEV. The output (w) is none and the loop exits at else not if??

The python code (jypter):

source = ["Select Source", "DEV", "TEMP", "PROD"]
source_ = widgets.Dropdown(
    options=source,
    value=source[0],
    description='Select variable:',
    disabled=False,
    button_style=''
)
def sourceURL(b):
    clear_output()
    print(source_.value)

### Drop Down
print("Drop Down")
display(source_)
w = source_.observe(sourceURL)

## print("output: ")
print(w)            ### output is None

#### LOOP
if w == 'DEV':
    print("This is Dev")    
elif w == "TEST":
    print("This is TEST")  
else:
    print("This is PROD")
  • Can you make your question clearer? There is no `else not if` part in the code you provided. – ac24 Feb 05 '20 at 15:46

1 Answers1

0

When you do source_.observe(sourceURL), there is no return value from this call. Hence this is equivalent to w = None.

To get the behaviour you want, I think you would need to move the code at the end of your script into your sourceURL function.

import ipywidgets as widgets
from IPython.display import clear_output

source = ["DEV", "TEMP", "PROD"]
source_ = widgets.Dropdown(
    options=source,
    value=source[0],
    description='Select variable:',
    disabled=False,
    button_style=''
)
def sourceURL(b):
#     clear_output()
    w = b['new']

    if w == 'DEV':
        print("This is Dev")    
    elif w == "TEMP":
        print("This is TEMP")  
    else:
        print("This is PROD")

### Drop Down
print("Drop Down")
display(source_)
w = source_.observe(sourceURL, names='value')

ac24
  • 5,325
  • 1
  • 16
  • 31