forked from Textualize/rich
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjupyter.py
88 lines (67 loc) · 2.79 KB
/
jupyter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from typing import Any, Dict, Iterable, List
from . import get_console
from .segment import Segment
from .terminal_theme import DEFAULT_TERMINAL_THEME
JUPYTER_HTML_FORMAT = """\
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">{code}</pre>
"""
class JupyterRenderable:
"""A shim to write html to Jupyter notebook."""
def __init__(self, html: str, text: str) -> None:
self.html = html
self.text = text
def _repr_mimebundle_(
self, include: Iterable[str], exclude: Iterable[str], **kwargs: Any
) -> Dict[str, str]:
data = {"text/plain": self.text, "text/html": self.html}
if include:
data = {k: v for (k, v) in data.items() if k in include}
if exclude:
data = {k: v for (k, v) in data.items() if k not in exclude}
return data
class JupyterMixin:
"""Add to an Rich renderable to make it render in Jupyter notebook."""
__slots__ = ()
def _repr_mimebundle_(
self, include: Iterable[str], exclude: Iterable[str], **kwargs: Any
) -> Dict[str, str]:
console = get_console()
segments = list(console.render(self, console.options)) # type: ignore
html = _render_segments(segments)
text = console._render_buffer(segments)
data = {"text/plain": text, "text/html": html}
if include:
data = {k: v for (k, v) in data.items() if k in include}
if exclude:
data = {k: v for (k, v) in data.items() if k not in exclude}
return data
def _render_segments(segments: Iterable[Segment]) -> str:
def escape(text: str) -> str:
"""Escape html."""
return text.replace("&", "&").replace("<", "<").replace(">", ">")
fragments: List[str] = []
append_fragment = fragments.append
theme = DEFAULT_TERMINAL_THEME
for text, style, control in Segment.simplify(segments):
if control:
continue
text = escape(text)
if style:
rule = style.get_html_style(theme)
text = f'<span style="{rule}">{text}</span>' if rule else text
if style.link:
text = f'<a href="{style.link}">{text}</a>'
append_fragment(text)
code = "".join(fragments)
html = JUPYTER_HTML_FORMAT.format(code=code)
return html
def display(segments: Iterable[Segment], text: str) -> None:
"""Render segments to Jupyter."""
from IPython.display import display as ipython_display
html = _render_segments(segments)
jupyter_renderable = JupyterRenderable(html, text)
ipython_display(jupyter_renderable)
def print(*args: Any, **kwargs: Any) -> None:
"""Proxy for Console print."""
console = get_console()
return console.print(*args, **kwargs)