Ipywidgets: Cannot create a widget whose initial state is visible=False

Created on 18 Jul 2016  Â·  25Comments  Â·  Source: jupyter-widgets/ipywidgets

In ipywidgets 5.2.2 the code below produces a widget that is visible:

check = Checkbox(description="A box", visible=False)
check

In ipywidgets 4, the checkbox wouldn't be displayed until I later set check.visible = True.

While this minimal example is pretty silly, I do rely heavily on being able to construct a widget with some components not visible until the user interacts with other elements.

A workaround or fix would be great.

bug

All 25 comments

The top-level widget styling attributes are deprecated. Instead, one should use the "layout" attribute.

check.layout.visibility = 'hidden'

or

check.layout.display = 'none'

depending on whether you want the invisible widget to take its place in the layout or not.

Regarding the visible attribute, we are aware of the behavior that you are observing. The deprecated top-level visible attribute is only a property that rebinds to the property of layout, but since there are then two things that modify the same css property, we decided that the new 'layout' method should win!

You can check out the example notebook

https://github.com/ipython/ipywidgets/blob/master/docs/source/examples/Widget%20Styling.ipynb

for more information about the use of layout.

Ah, that makes sense, sorry for the noise. It might be worth backporting that style notebook back to 5.x because the one currently there doesn't mention layout at all: https://github.com/ipython/ipywidgets/blob/5.x/docs/source/examples/Widget%20Styling.ipynb

good point, and this is not noise!

Actually, I think there is a bug here. Shouldn't the visible keyword in the constructor produce the desired result by (behind the scenes) setting visibility on layout, or emit a deprecation warning?

Deprecation warnings actually are not enabled by default.

Regarding the behavior, the problem is that the css property is read from
layout. I am not sure that there is a nice way to resolve the conflict. Do
you have something in mind?
On Jul 19, 2016 8:06 PM, "Matt Craig" [email protected] wrote:

Actually, I think there is a bug here. Shouldn't the visible keyword in
the constructor produce the desired result by (behind the scenes) setting
visibility on layout, or emit a deprecation warning?

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/ipython/ipywidgets/issues/674#issuecomment-233717073,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACSXFnKqe0d-8acorphZpvwZ81lXXcQrks5qXRI-gaJpZM4JOZy1
.

@SylvainCorlay

  • where can I find doc for visibility?

The website does not have anything: http://ipywidgets.readthedocs.io/en/latest/search.html?q=visibility&check_keywords=yes&area=default

if setting button.layout.visibility = 'hidden' will hide the button. How can I show it?
button.layout.visibility = 'shown' does not work (neither 'True', 'display', ...)

  • shouldn't be visibility enum rather Unicode?

This example also doesn't generate an invisible checkbox:

check = Checkbox(description="A box", visibility='hidden')
check

@hainm -- looks like setting visibility to 'visible' brings it back.

@SylvainCorlay -- I'd suggest two things (both of which may be impractical, I'm really not familiar with the codebase):

  1. The .visible attribute should accurately reflect whether the widget is visible. Right now setting .visibility='hidden' leaves visible set to True.
  2. If a user sets visible=False in the function call then I'd expect something like this in the constructor:
visible = kwd.pop('visible', True)
if visible:
     some_widget.layout.visibility = 'visible'
else:
     some_widget.layout.visibility = 'hidden'

It is unfortunate that DeprecationWarnings get hidden by default...

ok, I can try something like this - although beware that visible is not even there in master anymore. So this would be an ipywidgets 5.x only thing.

although beware that visible is not even there in master anymore. So this would be an ipywidgets 5.x only thing.

I will wait for 6.x.

If it will be gone in master I wouldn't worry too much about it...if I get a chance maybe I'll try to flesh out the wiki page with some more details.

One thing that we should do is consolidate

  • the "widget styling" which is the tutorial about styling widget and use of the layout
  • the "widget layout" which essentially tests that natural sizes of widgets work well together in arrangements of vboxes and hboxes.

Closing as fixed in master and will be in 6.0.

Just to make sure I understand, the visible property is gone in 6.0, right?

That is right!

Only layout.visibility.

On Nov 12, 2016 11:59 AM, "Matt Craig" [email protected] wrote:

Just to make sure I understand, the visible property is gone in 6.0,
right?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/ipython/ipywidgets/issues/674#issuecomment-260144503,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACSXFo3DgwX7SoBPtCHS91V5QYs5LFxFks5q9hqvgaJpZM4JOZy1
.

I just found we can also use .close() methods in case you want to remove it from your notebook

You can also use layout.display='none' to hide the widget.

Could you tell me why you remove visible property ?
https://github.com/aplavin/ipy-progressbar/issues/3

Rather than conflating the visibility with hiding, we now just simply expose the separate css properties as part of a unified way of handling the css for the widget as a whole, in the .layout attribute. Is that the explanation you are asking?

You can still do everything you used to - just using .layout.visibility and .layout.display.

I have multiple Box objects inside one container Box object. When I set the visibility of one of the smaller boxes to 'hidden' everything disappears. Is there a way to set only one Box object to be hidden? This problem seems to only happen if I try to set the property after it has been created.

Can you post some code reproducing this?

from ipywidgets import Layout, Label, SelectMultiple, Box
from IPython.display import display
box_layout = Layout(flex='auto', width='auto')
container_layout = Layout(display='flex', flex_flow='column', width='50%',)

title = Label(value='title text', layout=box_layout)
select = SelectMultiple(layout=box_layout)

box1 = Box(children=[title, select], layout=container_layout)
box2 = Box(children=[title, select], layout=container_layout)
main_container = Box(children=[box1, box2], layout=container_layout)
display(main_container)
box1.children[0].layout.visibility = 'hidden'

Here I am only trying to hide the title of the first box, but everything disappears.

The version of the notebook server is 5.0.0 and is running on:
Python 2.7.12 |Anaconda custom (64-bit)| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)]

I am making the interface for people using this version of anaconda. Thanks!

Both title and select use the same instance of Layout, and both widgets are used twice (it shows the same Label and SelectMultiple twice, not copies). In short, all the (non-box) widgets have the exact same instance of box_layout. Try creating one layout for each widget (and probably multiple value widgets).

Thanks @vidartf, didn't consider that.

Was this page helpful?
0 / 5 - 0 ratings