问题
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.
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))
First attempt using interact:
@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)
回答1:
It is! It's IPython.display.clear_output()
:
from IPython.display import clear_output
for i in range(10):
clear_output()
print(i)
http://ipython.org/ipython-doc/dev/api/generated/IPython.display.html#IPython.display.clear_output
UPDATE:
from IPython.display import clear_output
from ipywidgets import widgets
widgets.IntSlider(1)
clear_output()
# Note that `clear_output` will not clear widgets displayed using `display(widgets.IntSlider(1))`
widgets.IntSlider(100)
回答2:
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)
回答3:
From Jupyter notebook you can clear all the info (text and widgets) doing "Cell/Current Outputs/Clear".
If you want to do it programatically you have to do a clear_output()
in order to clear text and w.close()
(being w a widget) in every widget you created. More info here ("Closing widgets"):
http://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Basics.html
In your example (in your second attempt at least), yo need to add:
col_input.close()
row_input.close()
I have tested it with these versions:
Jupyter Notebook version: 5.0.0
ipywidgets version: 6.0.0
回答4:
Since ipywidgets 7.0 the widgets are rendered in the same way as any other output. As a consequence clear_output()
will clear all the output of cell including widgets.
Widgets are now displayed in the output area in the classic notebook and are treated as any other output. This allows the widgets to work more naturally with other cell output. To delete a widget, clear the output from the cell. Output from functions triggered by a widget view is appended to the output area that contains the widget view. This means that printed text will be appended to the output, and calling clear_output() will delete the entire output, including the widget view. (#1274, #1353)
Source: ipywidgets changelog
来源:https://stackoverflow.com/questions/42782902/clear-widget-area-of-a-cell-in-a-jupyter-notebook-from-within-notebook