Merge pull request #2332 from AntonyMilneQB/master

Allow Jupyter to vary width/height using COLUMNS/LINES
This commit is contained in:
Will McGugan 2022-06-20 11:50:02 +01:00 committed by GitHub
commit fc4af2752a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 3 deletions

View file

@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- Environment variables `JUPYTER_COLUMNS` and `JUPYTER_LINES` to control width and height of console in Jupyter
### Changed
- Default width of Jupyter console size is increased to 115
### Fixed
- Fix Rich clobbering cursor style on Windows https://github.com/Textualize/rich/pull/2339

View file

@ -25,6 +25,7 @@ The following people have contributed to the development of Rich:
- [Alexander Mancevice](https://github.com/amancevice)
- [Will McGugan](https://github.com/willmcgugan)
- [Paul McGuire](https://github.com/ptmcg)
- [Antony Milne](https://github.com/AntonyMilneQB)
- [Nathan Page](https://github.com/nathanrpage97)
- [Avi Perl](https://github.com/avi-perl)
- [Laurent Peuch](https://github.com/psycojoker)

View file

@ -421,3 +421,5 @@ Rich respects some standard environment variables.
Setting the environment variable ``TERM`` to ``"dumb"`` or ``"unknown"`` will disable color/style and some features that require moving the cursor, such as progress bars.
If the environment variable ``NO_COLOR`` is set, Rich will disable all color in the output.
If ``width``/``height`` arguments are not explicitly provided as arguments to ``Console`` then the environment variables ``COLUMNS``/``LINES`` can be used to set the console width/height. ``JUPYTER_COLUMNS``/``JUPYTER_LINES`` behave similarly and are used in Jupyter.

View file

@ -72,6 +72,8 @@ if TYPE_CHECKING:
from .live import Live
from .status import Status
JUPYTER_DEFAULT_COLUMNS = 115
JUPYTER_DEFAULT_LINES = 100
WINDOWS = platform.system() == "Windows"
HighlighterType = Callable[[Union[str, "Text"]], "Text"]
@ -656,8 +658,18 @@ class Console:
self.is_jupyter = _is_jupyter() if force_jupyter is None else force_jupyter
if self.is_jupyter:
width = width or 93
height = height or 100
if width is None:
jupyter_columns = self._environ.get("JUPYTER_COLUMNS")
if jupyter_columns is not None and jupyter_columns.isdigit():
width = int(jupyter_columns)
else:
width = JUPYTER_DEFAULT_COLUMNS
if height is None:
jupyter_lines = self._environ.get("JUPYTER_LINES")
if jupyter_lines is not None and jupyter_lines.isdigit():
height = int(jupyter_lines)
else:
height = JUPYTER_DEFAULT_LINES
self.soft_wrap = soft_wrap
self._width = width

View file

@ -22,6 +22,8 @@ def report() -> None: # pragma: no cover
"TERM_PROGRAM",
"COLUMNS",
"LINES",
"JUPYTER_COLUMNS",
"JUPYTER_LINES",
"JPY_PARENT_PID",
"VSCODE_VERBOSE_LOGGING",
)

View file

@ -3,5 +3,30 @@ from rich.console import Console
def test_jupyter():
console = Console(force_jupyter=True)
assert console.width == 93
assert console.width == 115
assert console.height == 100
assert console.color_system == "truecolor"
def test_jupyter_columns_env():
console = Console(_environ={"JUPYTER_COLUMNS": "314"}, force_jupyter=True)
assert console.width == 314
# width take precedence
console = Console(width=40, _environ={"JUPYTER_COLUMNS": "314"}, force_jupyter=True)
assert console.width == 40
# Should not fail
console = Console(
width=40, _environ={"JUPYTER_COLUMNS": "broken"}, force_jupyter=True
)
def test_jupyter_lines_env():
console = Console(_environ={"JUPYTER_LINES": "220"}, force_jupyter=True)
assert console.height == 220
# height take precedence
console = Console(height=40, _environ={"JUPYTER_LINES": "220"}, force_jupyter=True)
assert console.height == 40
# Should not fail
console = Console(
width=40, _environ={"JUPYTER_LINES": "broken"}, force_jupyter=True
)