|
2 | 2 | from __future__ import annotations |
3 | 3 |
|
4 | 4 | import json |
| 5 | +import re |
5 | 6 | from typing import Any |
6 | 7 | from urllib.request import urlopen |
7 | 8 |
|
@@ -82,8 +83,9 @@ def _show_chart(chart: AltairChartType) -> tuple[KnownMimeType, str]: |
82 | 83 | if isinstance(mime_response, str): |
83 | 84 | if ( |
84 | 85 | mime_type == "image/svg+xml" |
85 | | - and not altair.renderers.options.get("inline") |
| 86 | + and not altair.renderers.options.get("raw_svg") |
86 | 87 | ): |
| 88 | + _maybe_warn_external_resources(mime_response) |
87 | 89 | svg_bytes = mime_response.encode() |
88 | 90 | data_url = io_to_data_url(svg_bytes, mime_type) |
89 | 91 | return (mime_type, data_url or "") |
@@ -135,6 +137,27 @@ def _format_png_mimebundle( |
135 | 137 | return "application/vnd.marimo+mimebundle", json.dumps(mimebundle) |
136 | 138 |
|
137 | 139 |
|
| 140 | +# Check if the SVG contains external resources that may not render |
| 141 | +# correctly when encoded as a Data URL. |
| 142 | +# https://github.com/marimo-team/marimo/pull/9104 |
| 143 | +def _maybe_warn_external_resources(svg: str) -> None: |
| 144 | + # Strictly detecting external resource usage in SVG is difficult; |
| 145 | + # as a heuristic, we check for 'href' or 'xlink:href' attributes |
| 146 | + # that point to external resources (ignoring internal '#' or 'data:' URLs). |
| 147 | + if re.search( |
| 148 | + r'<[^>]*\b(?:xlink:)?href\s*=\s*["\'](?!\s*(?:#|data:))[^"\']+', svg |
| 149 | + ): |
| 150 | + msg = "".join( |
| 151 | + [ |
| 152 | + "This SVG contains external resources (href/xlink:href) ", |
| 153 | + "that may not render correctly when encoded as a Data URL. ", |
| 154 | + "If images are missing, try enabling raw SVG rendering with: ", |
| 155 | + "altair.renderers.enable('svg', raw_svg=True).", |
| 156 | + ] |
| 157 | + ) |
| 158 | + LOGGER.warning(msg) |
| 159 | + |
| 160 | + |
138 | 161 | # This is only needed since it seems that altair does not |
139 | 162 | # handle this internally. |
140 | 163 | # https://github.com/marimo-team/marimo/issues/2302 |
|
0 commit comments