【问题标题】:How to include multiple interactive widgets in the same cell in Jupyter notebook如何在 Jupyter 笔记本的同一单元格中包含多个交互式小部件
【发布时间】:2019-05-22 07:46:46
【问题描述】:

我的目标是让 Jupyter 笔记本中的一个单元格显示多个交互式小部件。具体来说,我想有四个滑块来裁剪图像,然后另一个单独的滑块来旋转这个裁剪的图像。当然,当我运行代码时,应该显示这两个图。这是我所拥有的。

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,d

interactive_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_plot

def image_rot(i):
    img_rot=scipy.ndimage.rotate(frame_slic.T,i)

    plt.figure(figsize=(8,8))
    plt.imshow(img_rot,cmap='RdBu')

    return i

interactive_plot_2 = interactive(image_rot, i = 
widgets.IntSlider(min=-180,max=180,step=1,value=0,description='Rotation'))

我可以将它放在两个单元格中(第一个单元格,而第二个单元格旋转),但不能在一个单元格中。

【问题讨论】:

    标签: python widget jupyter-notebook interactive


    【解决方案1】:

    Jupyter 将只显示一个小部件,因为它始终只显示单元格中最后一个命令的输出,因此您希望将两个小部件放在一个命令中。 您可以使用Layout 执行此操作,例如Box

    from ipywidgets import Box
    
    items = [interactive_plot, interactive_plot_2]
    box = Box(children=items)
    
    box # <- this one command displays all children
    

    【讨论】:

      猜你喜欢
      • 2017-08-04
      • 1970-01-01
      • 2016-07-04
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      • 2017-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多