Output widget's clear_output not clearing when using append_stdout
See original GitHub issueI stumbled upon a question on Stack Overflow where the first line printed in the output widget doesn’t get cleared by the first call to clear_output
.
import ipywidgets as widgets
def clear_output():
change_output_button = widgets.Button(description="Change output?")
the_output = widgets.Output()
clear_output_widget = widgets.VBox([change_output_button, the_output])
clear_output_widget.click_count = 0
def button_clicked(_button):
clear_output_widget.click_count += 1
the_output.clear_output()
the_output.append_stdout(f"button clicked {clear_output_widget.click_count} times.")
change_output_button.on_click(button_clicked)
return clear_output_widget`
Replacing append_stdout
with the context manager fixes the issue:
def button_clicked(_button):
clear_output_widget.click_count += 1
the_output.clear_output()
with the_output:
print(f"button clicked {clear_output_widget.click_count} times.")
but I am unsure of the reasons behind the erratic behaviour. I tried to look at the source of append_stdout
but I haven’t found it so far. Happy to submit a PR once I get to the bottom of this issue.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Output widgets: leveraging Jupyter's display system - ipywidgets
We can clear the output by either using IPython.display.clear_output within the context manager, or we can call the widget's clear_output method directly.
Read more >python - Jupyter Output widget not clearing - Stack Overflow
An option using out.outputs = [] instead of clear_output(). Based on https://stackoverflow.com/a/64103274/8508004 .
Read more >Python IPython.display.clear_output() Examples
This function does not generate a user interface for the widgets (unlike `interact`). ... clear the content of the output widget at every...
Read more >How to use ipywidgets to make your Jupyter notebook ...
Jupyter widgets can make notebooks be more interactive and make data ... While this article shows the output, the best way to experience...
Read more >Command Line Interface and Environment Variables Reference
Sub-commands¶ · delete¶. Delete a connection · export¶. All connections can be exported in STDOUT using the following command: airflow connections export -...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi!
I did have the same issue. But turned out that I could not even use the context manager, as I was trying to clear / update the Output from a thread, so that my notebook would still be available, and the output would update with data from rest calls. And that does not work very well. I assume that the
with out:
is using some static variable that don’t play well with threading.I worked around it by using a
out = widgets.HTML()
and callingout.value = f"<pref>{my_message}</pre>"
in the thread function.workaround for this is posted on another thread: https://github.com/jupyter-widgets/ipywidgets/issues/3260#issuecomment-907715980