question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Output widget's clear_output not clearing when using append_stdout

See original GitHub issue

I 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:open
  • Created 4 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
wonsjbcommented, Oct 9, 2020

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 calling out.value = f"<pref>{my_message}</pre>" in the thread function.

0reactions
pnsvkcommented, Mar 15, 2022
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found