yaspin
yaspin copied to clipboard
Switch to deactivate spinner
When using the spinner in a non-tty console (for example the logs of a job in GitHub Actions), we get way too many logs (one every _timer milliseconds (default is 80ms)) because there is no refresh of the lines.
To avoid this behavior, the easy solution is to use a switch depending on the environment:
use_spinner = os.environ.get("CI") # or sys.stdout.isatty()
if use_spinner:
with yaspin(text="Processing..."):
# do something
...
spinner.ok("Done.")
else:
print("Processing...") # fallback log
# do something
...
print("Done.")
It would be nice to have a boolean parameter enabled in yaspin() that would make it act as a simple print("Processing") when set to true. We then could simplify the code above as:
use_spinner = os.environ.get("CI") # or sys.stdout.isatty()
with yaspin(text="Processing...", enabled=use_spinner):
# do something
spinner.ok("Done.")
Hello,
Thanks for raising this issue. Env variable approach looks reasonable. I'll take a closer look on the implementation side. Also, please feel free to submit PR if you already have something.
With v3.3.0, it is possible to set custom streams now. So, for your example, it would be something like:
use_spinner = os.environ.get("CI")
stream = sys.stderr if use_spinner else sys.stdout
if use_spinner:
with yaspin(text="Processing...", stream=stream) as spinner:
# do something
...
spinner.ok("Done.")
else:
print("Processing...") # fallback log
# do something
...
print("Done.")
I'm closing this now. If proposed solution is not sufficient, feel free to re-open.