The Wayback Machine - https://web.archive.org/web/20201114170942/https://github.com/googlecolab/colabtools/issues/166
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Androbin opened this issue May 22, 2018 · 8 comments
Closed

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

Androbin opened this issue May 22, 2018 · 8 comments

Comments

@Androbin
Copy link

@Androbin Androbin commented May 22, 2018

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.

@blois
Copy link
Contributor

@blois blois commented May 23, 2018

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?

@Androbin
Copy link
Author

@Androbin Androbin commented May 23, 2018

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.

@blois
Copy link
Contributor

@blois blois commented May 23, 2018

@Androbin
Copy link
Author

@Androbin Androbin commented May 23, 2018

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.

@Androbin Androbin closed this May 23, 2018
@blois
Copy link
Contributor

@blois blois commented May 23, 2018

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.

@Androbin
Copy link
Author

@Androbin Androbin commented May 23, 2018

I am able to reproduce with these three lines:

!apt-get install cmake
!pip install gym
!pip install gym[atari]
@blois
Copy link
Contributor

@blois blois commented May 29, 2018

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

@JSTHoffman
Copy link

@JSTHoffman JSTHoffman commented Mar 11, 2020

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
3 participants
You can’t perform that action at this time.