Skip to content

Commit e34eadb

Browse files
authored
Merge pull request #1158 from willmcgugan/jupyter-echosystem
Jupyter echosystem
2 parents 53698d1 + 6f0eae2 commit e34eadb

File tree

6 files changed

+33
-24
lines changed

6 files changed

+33
-24
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [10.1.0] - 2020-04-03
9+
10+
### Fixed
11+
12+
- Fixed support for jupyter qtconsole and similar Jupyter environments
13+
814
## [10.0.1] - 2021-03-30
915

1016
### Fixed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "rich"
33
homepage = "https://github.com/willmcgugan/rich"
44
documentation = "https://rich.readthedocs.io/en/latest/"
5-
version = "10.0.1"
5+
version = "10.1.0"
66
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
77
authors = ["Will McGugan <[email protected]>"]
88
license = "MIT"

rich/console.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,7 @@ def _check_buffer(self) -> None:
16811681
if self.is_jupyter: # pragma: no cover
16821682
from .jupyter import display
16831683

1684-
display(self._buffer)
1684+
display(self._buffer, self._render_buffer(self._buffer[:]))
16851685
del self._buffer[:]
16861686
else:
16871687
text = self._render_buffer(self._buffer[:])

rich/jupyter.py

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
from typing import Iterable, List, TYPE_CHECKING
1+
from typing import Iterable, List
22

33
from . import get_console
44
from .segment import Segment
55
from .terminal_theme import DEFAULT_TERMINAL_THEME
66

7-
if TYPE_CHECKING:
8-
from .console import RenderableType
97

108
JUPYTER_HTML_FORMAT = """\
119
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">{code}</pre>
@@ -15,28 +13,33 @@
1513
class JupyterRenderable:
1614
"""A shim to write html to Jupyter notebook."""
1715

18-
def __init__(self, html: str) -> None:
16+
def __init__(self, html: str, text: str) -> None:
1917
self.html = html
18+
self.text = text
2019

21-
@classmethod
22-
def render(cls, rich_renderable: "RenderableType") -> str:
23-
console = get_console()
24-
segments = console.render(rich_renderable, console.options)
25-
html = _render_segments(segments)
26-
return html
27-
28-
def _repr_html_(self) -> str:
29-
return self.html
20+
def _repr_mimebundle_(self, include, exclude, **kwargs):
21+
data = {"text/plain": self.text, "text/html": self.html}
22+
if include:
23+
data = {k: v for (k, v) in data.items() if k in include}
24+
if exclude:
25+
data = {k: v for (k, v) in data.items() if k not in exclude}
26+
return data
3027

3128

3229
class JupyterMixin:
3330
"""Add to an Rich renderable to make it render in Jupyter notebook."""
3431

35-
def _repr_html_(self) -> str:
32+
def _repr_mimebundle_(self, include, exclude, **kwargs):
3633
console = get_console()
3734
segments = list(console.render(self, console.options)) # type: ignore
3835
html = _render_segments(segments)
39-
return html
36+
text = console._render_buffer(segments)
37+
data = {"text/plain": text, "text/html": html}
38+
if include:
39+
data = {k: v for (k, v) in data.items() if k in include}
40+
if exclude:
41+
data = {k: v for (k, v) in data.items() if k not in exclude}
42+
return data
4043

4144

4245
def _render_segments(segments: Iterable[Segment]) -> str:
@@ -64,12 +67,12 @@ def escape(text: str) -> str:
6467
return html
6568

6669

67-
def display(segments: Iterable[Segment]) -> None:
70+
def display(segments: Iterable[Segment], text: str) -> None:
6871
"""Render segments to Jupyter."""
6972
from IPython.display import display as ipython_display
7073

7174
html = _render_segments(segments)
72-
jupyter_renderable = JupyterRenderable(html)
75+
jupyter_renderable = JupyterRenderable(html, text)
7376
ipython_display(jupyter_renderable)
7477

7578

rich/layout.py

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
Union,
1515
)
1616

17-
from typing_extensions import Literal
18-
1917
from ._ratio import ratio_resolve
2018
from .align import Align
2119
from .console import Console, ConsoleOptions, RenderableType, RenderResult

rich/pretty.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from array import array
55
from collections import Counter, defaultdict, deque
66
from dataclasses import dataclass, fields, is_dataclass
7-
import inspect
87
from itertools import islice
98
from typing import (
109
TYPE_CHECKING,
@@ -27,7 +26,7 @@
2726
from .abc import RichRenderable
2827
from .cells import cell_len
2928
from .highlighter import ReprHighlighter
30-
from .jupyter import JupyterRenderable
29+
from .jupyter import JupyterMixin, JupyterRenderable
3130
from .measure import Measurement
3231
from .text import Text
3332

@@ -99,6 +98,9 @@ def ipy_display_hook(value: Any) -> None: # pragma: no cover
9998
if console.is_jupyter and any(attr.startswith("_repr_") for attr in dir(value)):
10099
return
101100

101+
if hasattr(value, "_repr_mimebundle_"):
102+
return
103+
102104
# certain renderables should start on a new line
103105
if isinstance(value, ConsoleRenderable):
104106
console.line()
@@ -130,7 +132,7 @@ def ipy_display_hook(value: Any) -> None: # pragma: no cover
130132
sys.displayhook = display_hook
131133

132134

133-
class Pretty:
135+
class Pretty(JupyterMixin):
134136
"""A rich renderable that pretty prints an object.
135137
136138
Args:

0 commit comments

Comments
 (0)