Skip to content

clear_output does not clear the previous widgets #1861

@suprithams

Description

@suprithams

I have a function that checks the value of a dropdown widget. When the value changes from one option of dropdown to other option, I have a function that observes the change and display slider according to the change. But in doing so, it does not clear the previous option's slider widget. I have tried using clear_output() but in vain. Can someone help me resolve the issue? Thanks.

Like in the case below, when I can the value of element from 1 to 2, it should delete slider1 and then display slider2.

element = widgets.Dropdown(
    options={'A': 1, 'B': 2, 'C': 3},
    value=1,
    description='Choose element:',
    )
  display(element)
  
  def on_ensemble_change(change):
    clear_output()
    changed_value =  change['new']
    if(changed_value == 1):
        print('inside if 1')
        s1_widget = widgets.FloatSlider(
                                            value=10,
                                            min=0,
                                            max=30,
                                            step=0.5,
                                            description="slider1",
                                            disabled=False,
                                            continuous_update=False,
                                            orientation='horizontal',
                                            readout=True,
                                            readout_format='.1f',
                                            )
    display(s1_widget)
    if(changed_value == 2):
        print('inside if 2')
        s2_widget = widgets.FloatSlider(
                                            value=10,
                                            min=0,
                                            max=50,
                                            step=0.5,
                                            description="slider2",
                                            disabled=False,
                                            continuous_update=False,
                                            orientation='horizontal',
                                            readout=True,
                                            readout_format='.1f',
                                            )
    display(s2_widget)

    if(changed_value == 3):
        print('inside if 3')
        s3_widget = widgets.FloatSlider(
                                            value=10,
                                            min=0,
                                            max=90,
                                            step=0.5,
                                            description="slider3",
                                            disabled=False,
                                            continuous_update=False,
                                            orientation='horizontal',
                                            readout=True,
                                            readout_format='.1f',
                                            )
    display(s3_widget)
   
 element.observe(on_element_change, names='value')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions