summaryrefslogtreecommitdiffstats
path: root/rich/jupyter.py
blob: f76380f5ad34fd390c08816ea0a6d70f5fafd048 (plain)
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
from typing import Iterable, List, TYPE_CHECKING

from . import get_console
from .segment import Segment
from .terminal_theme import DEFAULT_TERMINAL_THEME

if TYPE_CHECKING:
    from .console import RenderableType

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) -> None:
        self.html = html

    @classmethod
    def render(cls, rich_renderable: "RenderableType") -> str:
        console = get_console()
        segments = console.render(rich_renderable, console.options)
        html = _render_segments(segments)
        return html

    def _repr_html_(self) -> str:
        return self.html


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

    def _repr_html_(self) -> str:
        console = get_console()
        segments = list(console.render(self, console.options))  # type: ignore
        html = _render_segments(segments)
        return html


def _render_segments(segments: Iterable[Segment]) -> str:
    def escape(text: str) -> str:
        """Escape html."""
        return text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")

    fragments: List[str] = []
    append_fragment = fragments.append
    theme = DEFAULT_TERMINAL_THEME
    for text, style, is_control in Segment.simplify(segments):
        if is_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]) -> None:
    """Render segments to Jupyter."""
    from IPython.display import display as ipython_display

    html = _render_segments(segments)
    jupyter_renderable = JupyterRenderable(html)
    ipython_display(jupyter_renderable)


def print(*args, **kwargs) -> None:
    """Proxy for Console print."""
    console = get_console()
    return console.print(*args, **kwargs)