The internal servers have no access to outside package repositories and run Python 3.8/3.9, so the UI moves from hand-drawn curses to Textual 6.2.1 (the last release supporting 3.8.1), vendored with its pure-Python dependencies in vendor/. The launcher puts vendor/ first on sys.path, so a plain checkout runs with no pip and no network. - dsflow/: the single script split into a package (pure moves of the backend sections); action rules moved to ops.py, free of Textual - dsflow/ui/: tabs, detail pane, live sanity-run panel, log viewer, modal dialogs, filter, themes, mouse support - tests/test_ui.py: headless UI tests through Textual's pilot - tools/vendor.py (refresh vendor/), tools/screenshots.py (README images) - README, CLAUDE.md, TODO and the feature doc updated for the new layout, the Python 3.8.1 floor and the vendoring rules Verified: 11 tests green on Python 3.12 and 3.8.20; the shipped file set runs under `python -I -S` on both. Co-Authored-By: Claude Opus 5.5 <[email protected]>
102 lines
3.2 KiB
Python
Vendored
102 lines
3.2 KiB
Python
Vendored
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Sequence
|
|
|
|
if TYPE_CHECKING:
|
|
from rich.console import ConsoleRenderable
|
|
|
|
from . import get_console
|
|
from .segment import Segment
|
|
from .terminal_theme import DEFAULT_TERMINAL_THEME
|
|
|
|
if TYPE_CHECKING:
|
|
from rich.console import ConsoleRenderable
|
|
|
|
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: Sequence[str], exclude: Sequence[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: "ConsoleRenderable",
|
|
include: Sequence[str],
|
|
exclude: Sequence[str],
|
|
**kwargs: Any,
|
|
) -> Dict[str, str]:
|
|
console = get_console()
|
|
segments = list(console.render(self, console.options))
|
|
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}" target="_blank">{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."""
|
|
html = _render_segments(segments)
|
|
jupyter_renderable = JupyterRenderable(html, text)
|
|
try:
|
|
from IPython.display import display as ipython_display
|
|
|
|
ipython_display(jupyter_renderable)
|
|
except ModuleNotFoundError:
|
|
# Handle the case where the Console has force_jupyter=True,
|
|
# but IPython is not installed.
|
|
pass
|
|
|
|
|
|
def print(*args: Any, **kwargs: Any) -> None:
|
|
"""Proxy for Console print."""
|
|
console = get_console()
|
|
return console.print(*args, **kwargs)
|