-
Notifications
You must be signed in to change notification settings - Fork 950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updating GridspecLayout
on click
#2942
Comments
After thinking a bit about this, I was able to solve the problem like this: class PlotGrid():
def __init__(self, nn):
self.plotgrid = ipyW.Box() # <----
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))
grid = ipyW.GridspecLayout(nrows, ncols)
n = 0
for i in range(grid.n_rows):
for j in range(grid.n_columns):
if (n < len(self.panels)):
grid[i, j] = self.panels[n]
n += 1
self.plotgrid.children = [grid] # <----
def show(self):
display(self.plotgrid) Looks a bit hacky, but the idea is that I don't rewrite the object, but rather have a constant one and just update its children. Will keep the issue open for a bit, in case people have other solutions. |
Hey @haykh your solution is probably the best approach, so congrats on figuring that out! I think the issue was that cell 1: some_var = 'a'
display_handle = display(some_var, display_id='some-unique-id')
some_var = 'b' Will have display_handle.update(some_var) now cell 1's output will be You can read more about this here, here and here. I would particularly recommend implementing the In your working example you are circumventing having to do something like that by using the widget update system. The |
Out of curiosity are you going to be using matplotlib to generate these plots? And if so are you using the Alternatively create the figure in the context of an |
I got something like this (modulo all the interactive stuff): import matplotlib.pyplot as plt
import ipywidgets as ipyW
from IPython.display import display
# i think this enables ipympl backend:
%matplotlib widget
class myPlot():
def __init__(self):
output = ipyW.Output()
plt.close('all')
with output:
self.fig, self.ax = plt.subplots()
self.ax.plot([0, 1], [0, 1])
def show(self):
display(self.fig)
plot = myPlot()
plot.show() So, yes, I use the |
Yup it does! Your approach is totally reasonable, it's the "ipywidgets" solution whereas I was suggesting the "matplotlib" solution. More about this in this unmerged PR updating ipympl examples: matplotlib/ipympl#244 I see you're using |
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).
So when I run this:
it does what it should: fills in the
.grid
object with panels. When I click on the button theappendGrid
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 runningplotgrid.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:
The text was updated successfully, but these errors were encountered: