Skip to content

[Bug] print(..., end="\r") doesn't produce any output #166

Closed

Description

@Androbin

None of the following produce any output:

  • print(..., end="\r")
  • print(..., end="\r", flush=True)
  • print(..., end="\r") directly followed by sys.stdout.flush()

not even after the cell has finished execution.
Meaning, it's not an issue with the buffering simply delaying the output.

Activity

  1. blois commented on May 23, 2018

    @blois

    Carriage returns are automatically clearing any contents back to the newline, regardless of what follows.

    Is there a specific scenario in which more advanced handling is desired?

  2. Androbin commented on May 23, 2018

    @Androbin
    Author

    Carriage returns don't (normally) directly override the last line.
    Subsequent text will overwrite the already printed text (or parts of it).
    At least with forced flushing, it should print some-thing.

    The common use case is to continuously print the state of the program:

    print("initializing", end="\r")
    # ...
    print("fetching data", end="\r")
    # ...
    for train_step in range(1000):
      # ...
      print("training | step:", train_step, "| loss:", loss, end="\r")
    # ...
    print("accuracy", accuracy)
    

    This way, you don't spam the notebook output with pages of redundant logging.

  3. blois commented on May 23, 2018

    @blois
  4. Androbin commented on May 23, 2018

    @Androbin
    Author

    Thanks! Good to know.

    But I just noticed that !pip install seems to have trouble printing:
    Running setup.py bdist_wheel for atari-py ... -� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �/� �-� �\� �|� �done
    It's supposed to animate the last symbol between - \ | / in place.

  5. blois commented on May 23, 2018

    @blois

    We fixed an issue with this about a week ago and I have no longer observed the issue during !pip install.

    A simple repro of it working is:
    https://colab.research.google.com/gist/blois/d8e4ae3df9eef1a8a99245403024883b/spinner.ipynb

    If you happen to have an example of containing the final result then we can take a look at it.

  6. Androbin commented on May 23, 2018

    @Androbin
    Author

    I am able to reproduce with these three lines:

    !apt-get install cmake
    !pip install gym
    !pip install gym[atari]
    
  7. blois commented on May 29, 2018

    @blois

    The fix will be rolling out in a few days, thank you for the report!

  8. JSTHoffman commented on Mar 11, 2020

    @JSTHoffman

    This is an old issue, but I've found that using end='' with a carriage return at the beginning of a string produces the desired effect—overwriting the previous line—and is much simpler than the solutions linked above.

    i.e. print('\ryour text here', end='')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions