Skip to content

Commit

Permalink
Merge pull request #10688 from sharadmv:color-by-default
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 448723842
  • Loading branch information
jax authors committed May 14, 2022
2 parents f26133c + b8a523f commit 42421c8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion jax/_src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def update_thread_local_jit_state(**kw):

flags.DEFINE_bool(
'jax_pprint_use_color',
bool_env('JAX_PPRINT_USE_COLOR', False),
bool_env('JAX_PPRINT_USE_COLOR', True),
help='Enable jaxpr pretty-printing with colorful syntax highlighting.'
)

Expand Down
23 changes: 21 additions & 2 deletions jax/_src/pretty_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import abc
import enum
import sys
from functools import partial
from typing import List, NamedTuple, Optional, Sequence, Tuple, Union
from jax.config import config
Expand All @@ -36,13 +37,31 @@
except ImportError:
colorama = None

def _can_use_color() -> bool:
try:
# Check if we're in IPython or Colab
ipython = get_ipython() # type: ignore[name-defined]
shell = ipython.__class__.__name__
if shell == "ZMQInteractiveShell":
# Jupyter Notebook
return True
elif "colab" in str(ipython.__class__):
# Google Colab (external or internal)
return True
except NameError:
pass
# Otherwise check if we're in a terminal
return sys.stdout.isatty()

CAN_USE_COLOR = _can_use_color()

class Doc(abc.ABC):
__slots__ = ()

def format(self, width: int = 80, use_color: bool = False,
def format(self, width: int = 80, use_color: Optional[bool] = None,
annotation_prefix=" # ") -> str:
use_color = use_color or config.FLAGS.jax_pprint_use_color
if use_color is None:
use_color = CAN_USE_COLOR and config.FLAGS.jax_pprint_use_color
return _format(self, width, use_color=use_color,
annotation_prefix=annotation_prefix)

Expand Down

0 comments on commit 42421c8

Please sign in to comment.