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

No longer works for output in Jupyter #28

Closed
mcrutch opened this issue Nov 7, 2022 · 14 comments
Closed

No longer works for output in Jupyter #28

mcrutch opened this issue Nov 7, 2022 · 14 comments

Comments

@mcrutch
Copy link

mcrutch commented Nov 7, 2022

  • Version 2.0.1 worked with Jupyter/JupyterLab
  • Version 2.1.0 does not.

Pretty sure the _can_do_color function is the culprit. While it might not have been intentional that it worked in Jupyter, it did.

def _can_do_colour() -> bool:

@hugovk
Copy link
Member

hugovk commented Nov 7, 2022

Please can you give more info?

A minimal reproducible example would help:

https://stackoverflow.com/help/minimal-reproducible-example

What did you do?

What did you expect?

What actually happened?

@mcrutch
Copy link
Author

mcrutch commented Nov 7, 2022

  • Run Jupyter Notebook or Jupyter Lab
  • Create a new notebook
  • Execute this cell
!pip install termcolor==2.0.1
from termcolor import colored
print(colored("Hello World","red"))

Result: Prints Hello World in red in the output cell
image

  • Restart kernel
  • update cell and execute
!pip install termcolor==2.1.0
from termcolor import colored
print(colored("Hello World","red"))

Results: Prints Hello World in theme default color
image

@hugovk
Copy link
Member

hugovk commented Nov 7, 2022

Thank you!

Please can you let me know the value of each of the conditions in _can_do_colour?

print("ANSI_COLORS_DISABLED" in os.environ)
print("NO_COLOR" in os.environ)
print("FORCE_COLOR" in os.environ)
print(hasattr(sys.stdout, "isatty"))
print(hasattr(sys.stdout, "isatty") and sys.stdout.isatty())
print(os.environ.get("TERM") != "dumb")

@mcrutch
Copy link
Author

mcrutch commented Nov 7, 2022

False
False
False
True
False
True

Possibly of value, sys.stdout is a ipykernel.iostream.OutStream

@hugovk
Copy link
Member

hugovk commented Nov 8, 2022

Thanks, so the problem is Jupyter is returning False for sys.isatty, despite being interactive.

Looks like they were going to set it to return True, but instead decided to default it to True and make it configurable to avoid problems with things like pagers:

ipython/ipykernel#683

I'll dig a bit more, the short term workaround is to set an env var FORCE_COLOR=1, or configure ipykernel.iostream.OutStream so isatty=True.

@hugovk
Copy link
Member

hugovk commented Nov 19, 2022

I asked over at ipython/ipykernel#1024.

@Nyaaa

This comment was marked as resolved.

@hugovk

This comment was marked as resolved.

@hugovk
Copy link
Member

hugovk commented Nov 21, 2022

@mcrutch Please can you retest with IPython Kernel for Jupyter v6.18.0? This should now be fixed.

@realtimeprojects
Copy link

A simple example how to reproduce this problem with the tee command:

python3 -m termcolor | tee test.log

@hugovk
Copy link
Member

hugovk commented Nov 23, 2022

@realtimeprojects This issue is for Jupyter. If you're piping the output then we have no idea if the receiving program can accept the colour codes, that's why we're doing tty detection. If you want to force it, please use the env var:

FORCE_COLOR=1 python3 -m termcolor | tee test.log

@realtimeprojects
Copy link

realtimeprojects commented Nov 28, 2022

@hugovk:

I was just commenting on this issue because I think this is a generic problem and not specific to Jupyter. Sorry for that!

I think it is a bad idea to check the sys.stdout for "isatty()" since you don't know where your colorized message will go to. It is not guaruanteed that it will be streamed to sys.stdout (or sys.stderr). This is very application specific. And even if the output stream is not a tty it might still support colorization, what I assume is the case for Jupyter or any integrated environment simulating a terminal.

I use termcolor for colorizing "python logging" messages, which go to sys.stderr and if I run my app and redirect stdout to a file, the logging messages are not colorized as well.

This check also fails when used in azure devops pipelines.

Furthermore, I consider the "isatty" check as rather "expensive" when done for each single message and you have a lot of log messages to colorize.

@hugovk
Copy link
Member

hugovk commented Dec 16, 2022

Closing this issue as fixed in Jupyter: #28 (comment)


@realtimeprojects Thank you for the feedback.

Can you use the FORCE_COLOR=1 env var?

I'm considering adding a no_color parameter (#2 (comment)), would a force_color parameter help?

This check also fails when used in azure devops pipelines.

Yes, I guess this is like GitHub Actions: actions/runner#241

I recommend adding FORCE_COLOR=1 as an env var to your workflow (or similar), this will help for other tools as well:

@hugovk
Copy link
Member

hugovk commented Jan 15, 2023

Please see PR #38 to add parameters to override the environment variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants