Based on an answer elsewhere I've managed now to create a drop-down in Jupyter. On a selection change, I would like a selection-related pandas DataFrame to be displayed. This generally works, except that once a new value (from the drop-down) is selected, the already displayed DataFrame does not get cleared: the outputs (DataFrames) end up stacking on top of one another in the Output
widget. I do use clear_output
but it does not seem to be helping.
A full minimal snippet demonstrating the issue can be found below:
import pandas as pd
from IPython.display import display, clear_output
import ipywidgets as widgets
out = widgets.Output()
w = widgets.Dropdown(
options=['','one', 'two', 'three', 'four', 'five'],
value='',
description='Select:',
)
def on_change(change):
if change['type'] == 'change' and change['name'] == 'value':
print("changed to %s" % change['new']) # this gets logged correctly to the console
df = pd.DataFrame([change['new']], columns=['Selected'])
clear_output(wait=False) # I would expect this to prevent the output DataFrames piling up
out.append_display_data(df)
w.observe(on_change)
display(w, out)
Any help greatly appreciated.
*Edit: ipywidgets
version is 7.6.3
**Edit: The issue is not related to pandas at all. The same happens, say, for a dictionary dd = {'Selected': change['new']}
that one tries to output - they also end up piling up, rather then "previously selected disappearing, the newly selected showing up".