11

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)
nedned
  • 3,552
  • 8
  • 38
  • 41
  • Possible duplicate of [ipython notebook clear cell output in code](https://stackoverflow.com/questions/24816237/ipython-notebook-clear-cell-output-in-code) – breandan Feb 26 '18 at 06:29

4 Answers4

12

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
import random

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("New hello from each button click!. This hello from {}.".format(random.choice(list(range(100)))))

b.on_click(on_button_clicked)
Deepak Saini
  • 2,810
  • 1
  • 19
  • 26
  • 1
    Great example! I am trying to use the way you `clear_output()` in my example in which I need to put the code as part of a `Class`. Would you mind checking my post in case you know? https://stackoverflow.com/questions/59719207/clean-output-whith-onclick-button-widget-python – GCGM Jan 14 '20 at 09:06
6

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)
senornestor
  • 4,075
  • 2
  • 33
  • 33
  • I tried that, but it appears as though this only clears the cell output subarea but not the cell's widget subarea. ie the contents of the div with class "widget-subarea". Is this unexpected? – nedned Mar 15 '17 at 02:38
  • 1
    This is fixed in ipywidgets 6 but depends on notebook 5 which is available as a beta on PyPI and will be officially released in about a week. See https://github.com/ipython/ipywidgets/pull/1098 for details. – senornestor Mar 16 '17 at 23:12
  • I just tried using notebook 5/ipywidgets 6 and still doesn't seem to be working. Have updated my question with my minimal examples showing my attempts. any ideas? – nedned Apr 08 '17 at 02:05
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
rvilla
  • 165
  • 1
  • 2
  • 10
2

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

Kon Pal
  • 546
  • 1
  • 3
  • 13