Skip to content

Commit

Permalink
Merge pull request #2804 from minrk/jupyter-console
Browse files Browse the repository at this point in the history
Fix duplicate progress output in Jupyter
  • Loading branch information
willmcgugan committed Mar 4, 2023
2 parents 155bd04 + 178bbb9 commit f2ca04c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [13.3.2] - Unreleased

### Fixed
Expand All @@ -13,10 +14,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix syntax error when building with nuitka https://github.com/Textualize/rich/pull/2635
- Fixed pretty printing of empty dataclass https://github.com/Textualize/rich/issues/2819
- Fixes superfluous spaces in html output https://github.com/Textualize/rich/issues/2832
- Fixed duplicate output in Jupyter https://github.com/Textualize/rich/pulls/2804

### Added

- Added Polish README

## [13.3.1] - 2023-01-28

### Fixed
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Expand Up @@ -43,6 +43,7 @@ The following people have contributed to the development of Rich:
- [Kyle Pollina](https://github.com/kylepollina)
- [Sebastián Ramírez](https://github.com/tiangolo)
- [Felipe Guedes](https://github.com/guedesfelipe)
- [Min RK](https://github.com/minrk)
- [Clément Robert](https://github.com/neutrinoceros)
- [Brian Rutledge](https://github.com/bhrutledge)
- [Tushar Sadhwani](https://github.com/tusharsadhwani)
Expand Down
14 changes: 9 additions & 5 deletions rich/console.py
Expand Up @@ -711,11 +711,6 @@ def __init__(
self._force_terminal = None
if force_terminal is not None:
self._force_terminal = force_terminal
else:
# If FORCE_COLOR env var has any value at all, we force terminal.
force_color = self._environ.get("FORCE_COLOR")
if force_color is not None:
self._force_terminal = True

self._file = file
self.quiet = quiet
Expand Down Expand Up @@ -949,6 +944,15 @@ def is_terminal(self) -> bool:
# Return False for Idle which claims to be a tty but can't handle ansi codes
return False

if self.is_jupyter:
# return False for Jupyter, which may have FORCE_COLOR set
return False

# If FORCE_COLOR env var has any value at all, we assume a terminal.
force_color = self._environ.get("FORCE_COLOR")
if force_color is not None:
self._force_terminal = True

isatty: Optional[Callable[[], bool]] = getattr(self.file, "isatty", None)
try:
return False if isatty is None else isatty()
Expand Down
8 changes: 8 additions & 0 deletions tests/test_console.py
Expand Up @@ -971,3 +971,11 @@ def test_force_color(env_value):
# means is_terminal returns True.
console = Console(file=io.StringIO(), _environ={"FORCE_COLOR": env_value})
assert console.is_terminal


def test_force_color_jupyter():
# FORCE_COLOR above doesn't happen in a Jupyter kernel
console = Console(
file=io.StringIO(), _environ={"FORCE_COLOR": "1"}, force_jupyter=True
)
assert not console.is_terminal

0 comments on commit f2ca04c

Please sign in to comment.