Skip to content

Commit

Permalink
Fix colors in Jupyter Notebook and Google Colab (#494)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed May 2, 2022
1 parent bd59ca4 commit 0e54edf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

- Add a new ``watch`` optional argument to file sinks in order to automatically re-create possibly deleted or changed file (`#471 <https://github.com/Delgan/loguru/issues/471>`_).
- Make ``patch()`` calls cumulative instead of overriding the possibly existing patching function (`#462 <https://github.com/Delgan/loguru/issues/462>`_).
- Fix logs colorization not automatically enabled for Jupyter Notebook and Google Colab (`#494 <https://github.com/Delgan/loguru/issues/494>`_).


`0.6.0`_ (2022-01-29)
Expand Down
13 changes: 13 additions & 0 deletions loguru/_colorama.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ def should_colorize(stream):
if stream is None:
return False

try:
import ipykernel
import IPython

ipython = IPython.get_ipython()
is_jupyter_stream = isinstance(stream, ipykernel.iostream.OutStream)
is_jupyter_shell = isinstance(ipython, ipykernel.zmqshell.ZMQInteractiveShell)
except Exception:
pass
else:
if is_jupyter_stream and is_jupyter_shell:
return True

if stream is sys.__stdout__ or stream is sys.__stderr__:
if "PYCHARM_HOSTED" in os.environ:
return True
Expand Down
50 changes: 48 additions & 2 deletions tests/test_add_option_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import sys
from unittest.mock import MagicMock

import pytest

import loguru
import pytest
from loguru import logger

from .conftest import parse
Expand Down Expand Up @@ -151,6 +150,53 @@ def test_pycharm_ignored(monkeypatch, stream):
assert not loguru._colorama.should_colorize(stream)


def test_jupyter_fixed(monkeypatch):
class Shell:
pass

class Out:
pass

stream = MagicMock()
stream.__class__ = Out
stream.isatty.return_value = False
ipython = MagicMock()
ipykernel = MagicMock()
instance = MagicMock()
instance.__class__ = Shell
ipython.get_ipython.return_value = instance
ipykernel.zmqshell.ZMQInteractiveShell = Shell
ipykernel.iostream.OutStream = Out

monkeypatch.setitem(sys.modules, "IPython", ipython)
monkeypatch.setitem(sys.modules, "ipykernel", ipykernel)

assert not stream.isatty()
assert loguru._colorama.should_colorize(stream)


@pytest.mark.parametrize("stream", [None, Stream(False), Stream(None)])
def test_jupyter_ignored(monkeypatch, stream):
class Shell:
pass

class Out:
pass

ipython = MagicMock()
ipykernel = MagicMock()
instance = MagicMock()
instance.__class__ = Shell
ipython.get_ipython.return_value = instance
ipykernel.zmqshell.ZMQInteractiveShell = Shell
ipykernel.iostream.OutStream = Out

monkeypatch.setitem(sys.modules, "IPython", ipython)
monkeypatch.setitem(sys.modules, "ipykernel", ipykernel)

assert not loguru._colorama.should_colorize(stream)


@pytest.mark.parametrize("stream", [sys.__stdout__, sys.__stderr__])
def test_mintty_fixed_windows(monkeypatch, stream):
monkeypatch.setattr(os, "name", "nt")
Expand Down

0 comments on commit 0e54edf

Please sign in to comment.