Skip to content

Commit 5fb2cf7

Browse files
committed
Refactor thumbnail rendering: remove fallback to matplotlib and streamline Playwright integration for dark-theme PNG generation
1 parent 032f796 commit 5fb2cf7

1 file changed

Lines changed: 3 additions & 39 deletions

File tree

docs/_sg_html_scraper.py

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
99
package that has ``_repr_html_``).
1010
2. Renders a **pixel-accurate dark-theme thumbnail PNG** by loading the widget's
1111
standalone HTML in headless Chromium (Playwright) — the exact same renderer
12-
the user sees in a notebook. Falls back to a plain matplotlib placeholder
13-
if Playwright is not installed.
12+
the user sees in a notebook.
1413
3. Writes the **full interactive HTML** (iframe + widget JS) alongside the PNG.
1514
4. Returns rST that embeds both: the PNG as a fallback image AND an iframe for
1615
interactive use, using a ``.. raw:: html`` block.
1716
"""
1817

1918
from __future__ import annotations
2019

21-
import io
20+
import tempfile
2221
from pathlib import Path
2322
from uuid import uuid4
2423

@@ -43,20 +42,7 @@ def _find_viewer(globals_dict: dict):
4342

4443

4544
def _make_thumbnail_png(widget) -> bytes:
46-
"""Render a thumbnail PNG of *widget* using headless Chromium (Playwright).
47-
48-
The widget is rendered at its native size with the dark theme forced on.
49-
Falls back to a minimal matplotlib placeholder if Playwright is not
50-
available in the current environment.
51-
"""
52-
try:
53-
return _playwright_thumbnail(widget)
54-
except Exception:
55-
return _matplotlib_fallback_png(widget)
56-
57-
58-
def _playwright_thumbnail(widget) -> bytes:
59-
"""Render *widget* in headless Chromium and return dark-theme PNG bytes.
45+
"""Render *widget* in headless Chromium and return a dark-theme PNG screenshot.
6046
6147
Mirrors the ``_screenshot_widget`` helper in ``tests/conftest.py`` but
6248
forces the dark Dracula theme by:
@@ -67,7 +53,6 @@ def _playwright_thumbnail(widget) -> bytes:
6753
``prefers-color-scheme`` media query also resolves to dark (the
6854
fallback path in ``_isDarkBg`` when no explicit background is set).
6955
"""
70-
import tempfile
7156
from playwright.sync_api import sync_playwright
7257
from anyplotlib._repr_utils import build_standalone_html
7358

@@ -119,27 +104,6 @@ def _playwright_thumbnail(widget) -> bytes:
119104
return png_bytes
120105

121106

122-
def _matplotlib_fallback_png(widget) -> bytes:
123-
"""Minimal dark-background placeholder used when Playwright is unavailable."""
124-
import matplotlib
125-
matplotlib.use("Agg")
126-
import matplotlib.pyplot as plt
127-
128-
kind = type(widget).__name__
129-
fig, ax = plt.subplots(figsize=(4, 3), dpi=72)
130-
ax.set_facecolor("#1e1e2e")
131-
fig.patch.set_facecolor("#1e1e2e")
132-
ax.text(0.5, 0.5, kind, ha="center", va="center",
133-
color="#cdd6f4", transform=ax.transAxes, fontsize=12)
134-
ax.axis("off")
135-
plt.tight_layout(pad=0.3)
136-
buf = io.BytesIO()
137-
fig.savefig(buf, format="png", dpi=72, facecolor=fig.get_facecolor())
138-
plt.close(fig)
139-
buf.seek(0)
140-
return buf.read()
141-
142-
143107
def _iframe_html(src: str, w: int, h: int) -> str:
144108
"""Return a single-line HTML snippet that embeds *src* responsively.
145109

0 commit comments

Comments
 (0)