Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ipywidget interactive hiding visibility

I would like to make an interactive module with ipywidgets. So far so good but I'm stuck. I want to hide the visibility of a certain ipywidget object dependent on a certain situation, and I want my printed text to show up above the widget and stay there.

dropdown=widgets.Dropdown(
    options={'Coffee machine': 1, 'Washing machine': 2, 'Water Heater': 3, 'Heating System': 4, 'Dryer': 5, 'Oven': 6, 'Microwave': 7, 'Other':8},
    value=1,
    description='Apparaat:',
    )
text_new=widgets.Text()
def text_field(value):
    if(value==8):
        display(text_new)
        text_new.on_submit(handle_submit)
    else:
        text_new.visible(False) #Doesn't work but I want something like this
print("Today you had an increase in electricity consumption, would you like to name this device?") #This just be above the dropdown menu and be stuck
i=widgets.interactive(text_field, value=dropdown)
display(i)

What this does now: When "Other" is checked in the dropdown menu, a text box appears where the user can type something. However, when checking another machine, the text box stays there. I just need a "hide" function but I can't seem to find one that works.

Also, after checking another option on the dropdown, the print dissapears, not coming back.

like image 576
aze45sq6d Avatar asked Mar 10 '17 09:03

aze45sq6d


Video Answer


3 Answers

Had same problem so i found in

boton.layout.visibility = 'hidden'

or

check.layout.display = 'none'

they made some changes... i got if from here Cannot create a widget whose initial state is visible=False

like image 168
p1r0 Avatar answered Oct 24 '22 09:10

p1r0


Given a widget:

import ipywidgets
button = ipywidgets.Button()

There are two direct ways to hide the the widget, with a notable difference.

Hide and unhide the widget without affecting overall page layout:

# Turn the widget "invisible" without affecting layout
button.layout.visibility = "hidden"

# Make the widget visible again, layout unaffected
button.layout.visibility = "visible"

Hide and unhide the widget and collapse the space that the widget took up:

# Hide widget and collapse empty space
button.layout.display = "none"

# Re-add the widget, adjusting page layout as necessary.
button.layout.display = "block"

When to use each one? As a rule of thumb, use layout.visibility so the page layout is not constantly jumping around as visibility is toggled. However, for very large widgets, consider using layout.display to avoid huge blank spaces.


For more general CSS information that applies here, see What is the difference between visibility:hidden and display:none?

like image 37
pphysch Avatar answered Oct 24 '22 10:10

pphysch


In addition to the accepted answer, if you want to dynamically change the visibility of a control, you can declare the layout variable and reuse.

layout_hidden  = widgets.Layout(visibility = 'hidden')
layout_visible = widgets.Layout(visibility = 'visible')

Like attach to an event:

def visible_txt(b):
    text_box.layout = layout_visible

def hidden_txt(b):
    text_box.layout = layout_hidden

btn_visible.on_click(visible_txt)
btn_hidden.on_click(hidden_txt)
like image 6
Duc Filan Avatar answered Oct 24 '22 10:10

Duc Filan