How to include multiple interactive widgets in the same cell in Jupyter notebook

2024/10/8 14:42:35

My goal is to have one cell in Jupyter notebook displaying multiple interactive widgets. Specifically, I would like to have four slider for cropping an image and then another separate slider for rotating this cropped image. Of course, both plots should be displayed when I run the code. Here is what I have.

def image_crop(a,b,c,d):img_slic=frame[a:b,c:d]plt.figure(figsize=(8,8))    plt.imshow(img_slic,cmap='RdBu')return a,b,c,dinteractive_plot = interactive(image_crop, a = widgets.IntSlider(min=0,max=2000,step=10,value=500,description='Vertical_Uppper'),b = widgets.IntSlider(min=0,max=2000,step=10,value=500,description='Vertical_Lower'),c = widgets.IntSlider(min=0,max=1000,step=10,value=500,description='Horizontal_Left'),d = widgets.IntSlider(min=0,max=1000,step=10,value=500,description='Horizontal_Right') )
interactive_plotdef image_rot(i):img_rot=scipy.ndimage.rotate(frame_slic.T,i)plt.figure(figsize=(8,8))plt.imshow(img_rot,cmap='RdBu')return iinteractive_plot_2 = interactive(image_rot, i = 
widgets.IntSlider(min=-180,max=180,step=1,value=0,description='Rotation'))

I can have this in two cells (the first one crops while the second one rotates), but not in one.

Answer

Jupyter will display only one widget because it always displays only the output of the last command in the cell, so you want to put both widgets in one command. You can do this using Layout, for example, a Box:

from ipywidgets import Boxitems = [interactive_plot, interactive_plot_2]
box = Box(children=items)box # <- this one command displays all children
https://en.xdnf.cn/q/70112.html

Related Q&A

SWIG - Problem with namespaces

Im having trouble getting the following simple example to work with SWIG 1.3.40 (and I also tried 1.3.31). The Foo structure comes through as a Python module as long as I dont wrap it in a namespace, b…

Django access to subclasses items from abstract class

class Animal(models.Model):....class Meta:abstract = Trueclass Cat(models.Model, Animal):...class Dog(models.Model, Animal):....I want to be able to return all instances of querysets of all the subclas…

Django BinaryField retrieved as memory position?

Ive written a short unit test with the following code:my_object = MyObject() my_object.data = b12345my_object.save() saved_object = MyObject.objects.first()assert saved_object.data == my_object.datawhe…

difflib.SequenceMatcher isjunk argument not considered?

In the python difflib library, is the SequenceMatcher class behaving unexpectedly, or am I misreading what the supposed behavior is?Why does the isjunk argument seem to not make any difference in this…

PyCharm Code Folding/Outlining Generates Wrong Boundaries

Im having a very frustrating issue with PyCharm in that it does not want to properly outline the code so that blocks fold correctly. Ive looked all over the place and couldnt find any help with this pa…

How to clear the conda environment variables?

While I was setting an environment variable on a conda base env, I made an error in the path that was supposed to be assigned to the variable. I was trying to set the $PYSPARK_PYTHON env variable on th…

Create duplicates in the list

I havelist = [a, b, c, d]andnumbers = [2, 4, 3, 1]I want to get a list of the type of:new_list = [a, a, b, b, b, b, c, c, c, d]This is what I have so far:new_list=[] for i in numbers: for x in list: f…

cxfreeze aiohttp cannot import compat

Im trying to use cx_freeze to build a binary dist for an web application written in Python 3 using the aiohttp package.Basically I did:cxfreeze server.pyand got a dist outputBut when running the ./serv…

Python open() requires full path [duplicate]

This question already has answers here:open() gives FileNotFoundError / IOError: [Errno 2] No such file or directory(11 answers)Closed 9 months ago.I am writing a script to read a csv file. The csv fil…

Pandas to parquet file

I am trying to save a pandas object to parquet with the following code: LABL = datetime.now().strftime("%Y%m%d_%H%M%S") df.to_parquet("/data/TargetData_Raw_{}.parquet".format(LABL))…