Understanding Output Widget

I am trying to update an external output widget from within an interactive output function. For some reason using clear on the external output widget also clears the plot. i.e. This works normally:

from ipywidgets import *

out1 = widgets.Output(layout = {"border": "1px solid black"})

a=IntSlider()
b=IntSlider()
c=IntSlider()
ui=HBox([a,b,c])
def f(a,b,c):
    plt.imshow(np.random.random([10,10]))
    #out1.clear_output()
    out1.append_stdout(f"FROM INTERACTIVE FUNCTION!!!! {a}")

out = interactive_output(f, {'a': a, 'b': b, 'c': c})
display(VBox([HBox([a,b,c])]), out, out1)

while this does not work and clears the plot as a result of clear_output call on out1:

from ipywidgets import *

out1 = widgets.Output(layout = {"border": "1px solid black"})

a=IntSlider()
b=IntSlider()
c=IntSlider()
ui=HBox([a,b,c])
def f(a,b,c):
    plt.imshow(np.random.random([10,10]))
    out1.clear_output()
    out1.append_stdout(f"FROM INTERACTIVE FUNCTION!!!! {a}")

out = interactive_output(f, {'a': a, 'b': b, 'c': c})
display(VBox([HBox([a,b,c])]), out, out1)

I would expect the second sample to clear the out1 widget and simply add new line in the cleared output but for some reason it clears the plot as well. Looking for any tips or suggestion to resolve this issue.

‘append_stdout’ will update the previous output.You need to use ‘display’ instead.

1 Like