mirror of
https://github.com/Textualize/rich.git
synced 2025-08-04 10:08:40 +00:00
Exporting console content to SVG (#2101)
* Add skeleton export_svg and save_svg methods to Console * Exporting SVG * SVG export - Writing HTML foreign object into naively calculated SVG rect background * Exporting as foreign object SVG * Working with drop-shadow * Update terminal output style to include tab and background/border * Add more terminal themes, support dim, reverse in SVG output * Fix some HTML export tests * Allow for templating of SVG output * Fix mypy issue involving shadowed variable in SVG export * Remove unused code from main block in console.py * Remove unused record=True in __main__.py Console init * Small tidy ups in console.py SVG export * Add test for exporting to SVG * Add tests for export SVG and save SVG * Update docs with info on SVG exports * Add support for blink and blink2 to SVG export, use Fira Code webfont fallback * Update tests for SVG exporting * Add more information to docs about SVG exporting * Update SVG exporting tests * Explain how to use different terminal theme in SVG export docs * Remove some development testing code * Remove some more testing code * Improve docs, fix typo * Fixing a typo * Add note to changelog about SVG export functionality * Use CSS styling instead of inline styles on SVG export * Fix issues noted in code review, fix reverse style * Update SVG used in Rich docs
This commit is contained in:
parent
bb8c0d3521
commit
9f43cccfce
8 changed files with 1391 additions and 17 deletions
|
@ -249,15 +249,41 @@ If Python's builtin :mod:`readline` module is previously loaded, elaborate line
|
|||
Exporting
|
||||
---------
|
||||
|
||||
The Console class can export anything written to it as either text or html. To enable exporting, first set ``record=True`` on the constructor. This tells Rich to save a copy of any data you ``print()`` or ``log()``. Here's an example::
|
||||
The Console class can export anything written to it as either text, svg, or html. To enable exporting, first set ``record=True`` on the constructor. This tells Rich to save a copy of any data you ``print()`` or ``log()``. Here's an example::
|
||||
|
||||
from rich.console import Console
|
||||
console = Console(record=True)
|
||||
|
||||
After you have written content, you can call :meth:`~rich.console.Console.export_text` or :meth:`~rich.console.Console.export_html` to get the console output as a string. You can also call :meth:`~rich.console.Console.save_text` or :meth:`~rich.console.Console.save_html` to write the contents directly to disk.
|
||||
After you have written content, you can call :meth:`~rich.console.Console.export_text`, :meth:`~rich.console.Console.export_svg` or :meth:`~rich.console.Console.export_html` to get the console output as a string. You can also call :meth:`~rich.console.Console.save_text`, :meth:`~rich.console.Console.save_svg`, or :meth:`~rich.console.Console.save_html` to write the contents directly to disk.
|
||||
|
||||
For examples of the html output generated by Rich Console, see :ref:`appendix-colors`.
|
||||
|
||||
Exporting SVGs
|
||||
^^^^^^^^^^^^^^
|
||||
|
||||
When using :meth:`~rich.console.Console.export_svg` or :meth:`~rich.console.Console.save_svg`, the width of the generated SVG will
|
||||
match the width (in terms of character cells) of your terminal window. The height of the exported SVG will scale automatically to accommodate the console output.
|
||||
|
||||
The generated SVG can be viewed inside any web browser, and can be included on a webpage either by directly including the SVG markup
|
||||
or by referencing the file itself using an ``<img>`` tag. For finer control over the dimensions, you'll have to use an ``<img>`` tag.
|
||||
|
||||
The image below shows an example of an SVG exported by Rich.
|
||||
|
||||
.. image:: ../images/svg_export.svg
|
||||
|
||||
You can customise the theme used during SVG export by importing the desired theme from the :mod:`rich.terminal_theme` module and passing it to
|
||||
:meth:`~rich.console.Console.export_svg` or :meth:`~rich.console.Console.save_svg` via the ``theme`` parameter::
|
||||
|
||||
|
||||
from rich.console import Console
|
||||
from rich.terminal_theme import MONOKAI
|
||||
|
||||
console = Console(record=True)
|
||||
console.save_svg("example.svg", theme=MONOKAI)
|
||||
|
||||
Alternatively, you can create your own theme by constructing a :class:`rich.terminal_theme.TerminalTheme` instance
|
||||
yourself and passing that in.
|
||||
|
||||
Error console
|
||||
-------------
|
||||
|
||||
|
@ -381,7 +407,7 @@ If Rich detects that it is not writing to a terminal it will strip control codes
|
|||
Letting Rich auto-detect terminals is useful as it will write plain text when you pipe output to a file or other application.
|
||||
|
||||
Interactive mode
|
||||
~~~~~~~~~~~~~~~~
|
||||
----------------
|
||||
|
||||
Rich will remove animations such as progress bars and status indicators when not writing to a terminal as you probably don't want to write these out to a text file (for example). You can override this behavior by setting the ``force_interactive`` argument on the constructor. Set it to True to enable animations or False to disable them.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue