jupyter-sphinx
jupyter-sphinx copied to clipboard
Strip out ANSI codes from tracebacks under certain conditions
In this line we raise an exception if a cell produced errors. The produced traceback usually includes ANSI escape sequences to color the output, which are interpreted by the displaying terminal. This is very nice when interactively building documentation.
When building documentation on ReadTheDocs, however, the build log viewer does not interpret these ANSI escape sequences and just display the raw characters, which makes the traceback hard to read. See this issue for an example.
Fixing this requires 2 steps:
- Figure out how to strip out ANSI escqpe sequences from a string (the concensus on stackoverflow is to use a regex, but perhaps there is something in the standard library for this)
- Conditionally apply this preprocessing to the traceback. A minimal implementation would be to just check some arbitrary env var, but it would be good if this would "just work" on e.g. redirected stderr, and with some common env vars (e.g. ReadTheDocs specifies a
NO_COLORenv var, which seems to be a standard)