Skip to content

Commit 42421c8

Browse files
author
jax authors
committed
Merge pull request #10688 from sharadmv:color-by-default
PiperOrigin-RevId: 448723842
2 parents f26133c + b8a523f commit 42421c8

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

jax/_src/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ def update_thread_local_jit_state(**kw):
476476

477477
flags.DEFINE_bool(
478478
'jax_pprint_use_color',
479-
bool_env('JAX_PPRINT_USE_COLOR', False),
479+
bool_env('JAX_PPRINT_USE_COLOR', True),
480480
help='Enable jaxpr pretty-printing with colorful syntax highlighting.'
481481
)
482482

jax/_src/pretty_printer.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import abc
2929
import enum
30+
import sys
3031
from functools import partial
3132
from typing import List, NamedTuple, Optional, Sequence, Tuple, Union
3233
from jax.config import config
@@ -36,13 +37,31 @@
3637
except ImportError:
3738
colorama = None
3839

40+
def _can_use_color() -> bool:
41+
try:
42+
# Check if we're in IPython or Colab
43+
ipython = get_ipython() # type: ignore[name-defined]
44+
shell = ipython.__class__.__name__
45+
if shell == "ZMQInteractiveShell":
46+
# Jupyter Notebook
47+
return True
48+
elif "colab" in str(ipython.__class__):
49+
# Google Colab (external or internal)
50+
return True
51+
except NameError:
52+
pass
53+
# Otherwise check if we're in a terminal
54+
return sys.stdout.isatty()
55+
56+
CAN_USE_COLOR = _can_use_color()
3957

4058
class Doc(abc.ABC):
4159
__slots__ = ()
4260

43-
def format(self, width: int = 80, use_color: bool = False,
61+
def format(self, width: int = 80, use_color: Optional[bool] = None,
4462
annotation_prefix=" # ") -> str:
45-
use_color = use_color or config.FLAGS.jax_pprint_use_color
63+
if use_color is None:
64+
use_color = CAN_USE_COLOR and config.FLAGS.jax_pprint_use_color
4665
return _format(self, width, use_color=use_color,
4766
annotation_prefix=annotation_prefix)
4867

0 commit comments

Comments
 (0)