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

Created on 23 May 2018  路  8Comments  路  Source: googlecolab/colabtools

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.

Most helpful comment

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鈥攐verwriting the previous line鈥攁nd is much simpler than the solutions linked above.

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

All 8 comments

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?

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.

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.

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.

I am able to reproduce with these three lines:

!apt-get install cmake
!pip install gym
!pip install gym[atari]

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

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鈥攐verwriting the previous line鈥攁nd is much simpler than the solutions linked above.

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

Was this page helpful?
0 / 5 - 0 ratings