nglview icon indicating copy to clipboard operation
nglview copied to clipboard

Layout properly taken into account only the second time I display nglview widget

Open yakutovicha opened this issue 5 years ago • 9 comments

I am using nglview 2.7.1 and I encountered the following problem:

Screenshot_2020-01-07 Untitled1

any idea why the protein is centred properly only the second time I display the nglview widget? Is there a quick fix for that?

yakutovicha avatar Jan 07 '20 08:01 yakutovicha

This may help. If I do view.n_components = 1 before displaying the widget for the first time - it works without a problem.

yakutovicha avatar Jan 07 '20 09:01 yakutovicha

Hi, please use attribute to set the widget.

import nglview as nv

view = nv.demo()
view.layout.width = '50%'
view

hainm avatar Jan 07 '20 15:01 hainm

Thanks, @hainm, with setting attribute it works.

I was just wondering: is it expected behaviour? I had the impression that nglview's interface is very much in line with jupyter widgets, where both approaches do work.

yakutovicha avatar Jan 07 '20 16:01 yakutovicha

is it expected behaviour?

yes, that's expected behavior. The feature is not implemented yet. Currently a widget only watches for the changes in its layout's attributes. not the layout change itself.

https://github.com/arose/nglview/blob/0ad4c4ed69cca28c117ff5ee177ad2f3c64051e0/nglview/widget.py#L271-L279

I had the impression that nglview's interface is very much in line with jupyter widgets, where both approaches do work.

yeah, almost.

hainm avatar Jan 07 '20 16:01 hainm

If you still want to use view.layout = {'width': '50%'}, please call view.handle_resize() explicitly after that.

view = nv.demo()
view.layout = {'width': '50%'}
view.handle_resize()
view

hainm avatar Jan 07 '20 16:01 hainm

Alright, thanks for the explanation. Nice package by the way!

yakutovicha avatar Jan 07 '20 16:01 yakutovicha

Just discovered another issue: if I use view.layout.width = '50%' , as you suggested, everything works fine until I use the fullscreen. If I click on the fullscreen button and then return back to the notebook - the widget's height changes: before fullscreen: Screenshot_2020-01-07 Untitled1(1) after fullscreen: Screenshot_2020-01-07 Untitled1(2)

yakutovicha avatar Jan 07 '20 16:01 yakutovicha

However, with this approach:

view = nv.demo()
view.layout = {'width': '50%'}
view.handle_resize()
view

Everything seems to work fine.

yakutovicha avatar Jan 07 '20 16:01 yakutovicha

thanks for your report. yeah, there are still many minor issues here and there and we will keep this in mind. cheers.

hainm avatar Jan 07 '20 17:01 hainm