from typing import Iterable, List from . import get_console from .segment import Segment from .terminal_theme import DEFAULT_TERMINAL_THEME JUPYTER_HTML_FORMAT = """\
{code}
""" 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, exclude, **kwargs): 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.""" def _repr_mimebundle_(self, include, exclude, **kwargs): 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'{text}' if rule else text if style.link: text = f'{text}' 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, **kwargs) -> None: """Proxy for Console print.""" console = get_console() return console.print(*args, **kwargs)