nglview
nglview copied to clipboard
Layout properly taken into account only the second time I display nglview widget
I am using nglview 2.7.1 and I encountered the following problem:

any idea why the protein is centred properly only the second time I display the nglview widget? Is there a quick fix for that?
This may help. If I do view.n_components = 1 before displaying the widget for the first time - it works without a problem.
Hi, please use attribute to set the widget.
import nglview as nv
view = nv.demo()
view.layout.width = '50%'
view
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.
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.
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
Alright, thanks for the explanation. Nice package by the way!
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:
after fullscreen:

However, with this approach:
view = nv.demo()
view.layout = {'width': '50%'}
view.handle_resize()
view
Everything seems to work fine.
thanks for your report. yeah, there are still many minor issues here and there and we will keep this in mind. cheers.