8000 Updating `GridspecLayout` on click · Issue #2942 · jupyter-widgets/ipywidgets · GitHub
[go: up one dir, main page]

Skip to content
Updating GridspecLayout on click #2942
@haykh

Description

@haykh

Hi, thanks to the developers for putting this thing together -- it looks awesome!

I have a usage question though. I'm trying to make a layout of plots with NxM rows and columns. Right now I'm prototyping with just buttons. The idea is to be able to expand the layout (i.e. add new panels) by clicking on one of the buttons.

Here's a class I have so far (probably not the best way, but anyway).

import ipywidgets as ipyW
from IPython.display import display

class PlotGrid():
  def __init__(self, nn):
    self.panels = []
    for n in range(nn):
      self.createNewPanel(n=n)
    self.redraw()
  
  def createNewPanel(self, **kwargs):
    button = ipyW.Button(description='Button {}'.format(kwargs['n']), 
                         layout=ipyW.Layout(height='auto', width='auto'))
    button.on_click(self.appendGrid)
    self.panels.append(button)
    
  def appendGrid(self, status):
    self.createNewPanel(n=len(self.panels))
    self.redraw()
  
  def redraw(self):
    ncols = int(np.sqrt(len(self.panels)))
    nrows = int(np.ceil(len(self.panels) / ncols))
    self.grid = ipyW.GridspecLayout(nrows, ncols)
    n = 0
    for i in range(self.grid.n_rows):
      for j in range(self.grid.n_columns):
        if (n < len(self.panels)):
          self.grid[i, j] = self.panels[n]
        n += 1
  def show(self):
    display(self.grid)

So when I run this:

plotgrid = PlotGrid(12)
plotgrid.show()

it does what it should: fills in the .grid object with panels. When I click on the button the appendGrid function is indeed called, and it does append the new panels to the grid as it should. But for some reason the result is not displayed in the cell: it just remains the same. If I later update the cell by running plotgrid.show() again -- it draws the new extended .grid as it should.

So I think I'm not understanding something fundamental with how the display function works and Jupyter handles the events.


These are the versions of the relevant packages I got through conda:

ipykernel                 5.2.0            py38h23f93f0_1    conda-forge
ipython                   7.13.0           py38h32f6830_2    conda-forge
jupyterlab                2.1.0                      py_0    conda-forge
widgetsnbextension        3.5.1                    py38_0    conda-forge

Metadata

Metadata

Assignees

No one assigned

    Labels

    resolved-lockedClosed issues are locked after 30 days inactivity. Please open a new issue for related discussion.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0