Generate an SVG from the console contents (requires record=True in Console constructor). Args: title (str, optional): The title of the tab in the output image theme (TerminalTheme, optional): The ``TerminalTheme`` object to use to style the terminal
(
self,
*,
title: str = "Rich",
theme: Optional[TerminalTheme] = None,
clear: bool = True,
code_format: str = CONSOLE_SVG_FORMAT,
font_aspect_ratio: float = 0.61,
unique_id: Optional[str] = None,
)
| 2350 | write_file.write(html) |
| 2351 | |
| 2352 | def export_svg( |
| 2353 | self, |
| 2354 | *, |
| 2355 | title: str = "Rich", |
| 2356 | theme: Optional[TerminalTheme] = None, |
| 2357 | clear: bool = True, |
| 2358 | code_format: str = CONSOLE_SVG_FORMAT, |
| 2359 | font_aspect_ratio: float = 0.61, |
| 2360 | unique_id: Optional[str] = None, |
| 2361 | ) -> str: |
| 2362 | """ |
| 2363 | Generate an SVG from the console contents (requires record=True in Console constructor). |
| 2364 | |
| 2365 | Args: |
| 2366 | title (str, optional): The title of the tab in the output image |
| 2367 | theme (TerminalTheme, optional): The ``TerminalTheme`` object to use to style the terminal |
| 2368 | clear (bool, optional): Clear record buffer after exporting. Defaults to ``True`` |
| 2369 | code_format (str, optional): Format string used to generate the SVG. Rich will inject a number of variables |
| 2370 | into the string in order to form the final SVG output. The default template used and the variables |
| 2371 | injected by Rich can be found by inspecting the ``console.CONSOLE_SVG_FORMAT`` variable. |
| 2372 | font_aspect_ratio (float, optional): The width to height ratio of the font used in the ``code_format`` |
| 2373 | string. Defaults to 0.61, which is the width to height ratio of Fira Code (the default font). |
| 2374 | If you aren't specifying a different font inside ``code_format``, you probably don't need this. |
| 2375 | unique_id (str, optional): unique id that is used as the prefix for various elements (CSS styles, node |
| 2376 | ids). If not set, this defaults to a computed value based on the recorded content. |
| 2377 | """ |
| 2378 | |
| 2379 | import zlib |
| 2380 | from html import escape |
| 2381 | |
| 2382 | from rich.cells import cell_len |
| 2383 | |
| 2384 | style_cache: Dict[Style, str] = {} |
| 2385 | |
| 2386 | def get_svg_style(style: Style) -> str: |
| 2387 | """Convert a Style to CSS rules for SVG.""" |
| 2388 | if style in style_cache: |
| 2389 | return style_cache[style] |
| 2390 | css_rules = [] |
| 2391 | color = ( |
| 2392 | _theme.foreground_color |
| 2393 | if (style.color is None or style.color.is_default) |
| 2394 | else style.color.get_truecolor(_theme) |
| 2395 | ) |
| 2396 | bgcolor = ( |
| 2397 | _theme.background_color |
| 2398 | if (style.bgcolor is None or style.bgcolor.is_default) |
| 2399 | else style.bgcolor.get_truecolor(_theme) |
| 2400 | ) |
| 2401 | if style.reverse: |
| 2402 | color, bgcolor = bgcolor, color |
| 2403 | if style.dim: |
| 2404 | color = blend_rgb(color, bgcolor, 0.4) |
| 2405 | css_rules.append(f"fill: {color.hex}") |
| 2406 | if style.bold: |
| 2407 | css_rules.append("font-weight: bold") |
| 2408 | if style.italic: |
| 2409 | css_rules.append("font-style: italic;") |