从笔记本中清除Jupyter笔记本中单元格的小部件区域

从笔记本中清除Jupyter笔记本中单元格的小部件区域

本文介绍了从笔记本中清除Jupyter笔记本中单元格的小部件区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能从笔记本侧面(即在Python中)清除Jupyter笔记本中单元格的小部件区域. IPython.display.clear_output()仅清除单元格的输出区域,而不清除小部件区域.

I'm wondering if it's possible to clear the widget area of a cell in a Jupyter notebook from the notebook side (ie within Python). IPython.display.clear_output() only clears the cell's output area not the widget area.

更新:这在最新的Notebook和ipywidget中似乎仍然是一个问题.这是两个最小的示例,它们说明了我正在努力解决的问题.我要清除的小部件输出尤其是 qgrid 呈现的数据帧.在这两种情况下,尽管试图清除先前的窗口小部件输出,但随后的选择都会导致在前一个窗口之后附加一个表.每个新表都作为带有类p-Widget的div追加.

Update: this still seems to be a problem in latest Notebook and ipywidgets. Here are two minimal examples illustrating the problem I'm struggling with. The widget output that I'm trying to clear in particular are the data frames rendered by qgrid. In both cases, despite trying to clear the previous widget output, subsequent selections cause a table to be appended after the previous one. Each new table is appended as a div with the class p-Widget.

import pandas as pd
import numpy as np
import qgrid
from ipywidgets import interact
from IPython.display import display, clear_output
import notebook
import ipywidgets

print('Jupyter Notebook version: {}'.format(notebook.__version__))
print('ipywidgets version: {}'.format(ipywidgets.__version__))

max_columns = 10
max_rows = 10
col_opts = list(range(1, max_columns + 1))
row_opts = list(range(1, max_rows + 1))

首次尝试使用交互:

@interact(columns=col_opts, rows=row_opts)
def submit(columns, rows):
    df = pd.DataFrame(np.random.randint(0, 100, size=(rows, columns)))
    clear_output()
    display(qgrid.QGridWidget(df=df)

使用输出"小部件的第二次尝试:

Second attempt using the Output widget:

output = ipywidgets.Output()
display(output)

def submit2(change):
    rows = row_input.value
    columns = col_input.value
    df = pd.DataFrame(np.random.randint(0, 100, size=(rows, columns)))

    with output:
        output.clear_output()
        display(qgrid.QGridWidget(df=df))

col_input = ipywidgets.Dropdown(options=col_opts)
row_input = ipywidgets.Dropdown(options=row_opts)

col_input.observe(submit2, 'value')
row_input.observe(submit2, 'value')

display(col_input)
display(row_input)

推荐答案

从ipywidgets 7.0版开始,小部件就被视为与其他任何输出一样.为防止在执行clear_output()时清除窗口小部件(但清除文本输出),请使用输出小部件.像这样的东西:

From ipywidgets version 7.0 onwards, widgets are considered like any other output. To prevent the widgets from clearing(but clearing the text output) when you do clear_output(), use the Output widget.Something like this :

from IPython.display import display, clear_output
import ipywidgets as widgets

b = widgets.Button(
    description='Show Random',
    disabled=False,
    button_style='info',
    tooltip='Click me',
    icon='check'
)
display(b)

out = widgets.Output()
display(out)

def on_button_clicked(b):
    with out:
        clear_output()
        print("A new hello on each button click")

b.on_click(on_button_clicked)

这篇关于从笔记本中清除Jupyter笔记本中单元格的小部件区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 18:17