Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exporting console content to SVG #2101

Merged
merged 31 commits into from Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a8d2bb2
Merge branch 'master' of github.com:Textualize/rich
darrenburns Mar 21, 2022
2364e97
Add skeleton export_svg and save_svg methods to Console
darrenburns Mar 21, 2022
fb2f98b
Exporting SVG
darrenburns Mar 22, 2022
18d3638
SVG export - Writing HTML foreign object into naively calculated SVG …
darrenburns Mar 22, 2022
31dadb8
Exporting as foreign object SVG
darrenburns Mar 23, 2022
00a4bd1
Working with drop-shadow
darrenburns Mar 23, 2022
0bc4530
Update terminal output style to include tab and background/border
darrenburns Mar 24, 2022
6c3e385
Add more terminal themes, support dim, reverse in SVG output
darrenburns Mar 24, 2022
9fe6487
Fix some HTML export tests
darrenburns Mar 24, 2022
8a362c3
Allow for templating of SVG output
darrenburns Mar 24, 2022
66dfef8
Fix mypy issue involving shadowed variable in SVG export
darrenburns Mar 24, 2022
a6b630c
Remove unused code from main block in console.py
darrenburns Mar 24, 2022
c3ed655
Remove unused record=True in __main__.py Console init
darrenburns Mar 24, 2022
b6f93e4
Small tidy ups in console.py SVG export
darrenburns Mar 24, 2022
7225b35
Add test for exporting to SVG
darrenburns Mar 24, 2022
d3113e7
Add tests for export SVG and save SVG
darrenburns Mar 24, 2022
bd55ee3
Update docs with info on SVG exports
darrenburns Mar 28, 2022
645b588
Add support for blink and blink2 to SVG export, use Fira Code webfont…
darrenburns Mar 28, 2022
bb69a6e
Update tests for SVG exporting
darrenburns Mar 28, 2022
4834f03
Add more information to docs about SVG exporting
darrenburns Mar 28, 2022
6afbd68
Update SVG exporting tests
darrenburns Mar 28, 2022
c5c852c
Explain how to use different terminal theme in SVG export docs
darrenburns Mar 28, 2022
218adba
Remove some development testing code
darrenburns Mar 28, 2022
122c7be
Remove some more testing code
darrenburns Mar 28, 2022
5f82117
Improve docs, fix typo
darrenburns Mar 28, 2022
77b167d
Fixing a typo
darrenburns Mar 28, 2022
657fa36
Add note to changelog about SVG export functionality
darrenburns Mar 28, 2022
33f9f9f
Use CSS styling instead of inline styles on SVG export
darrenburns Mar 29, 2022
0d27674
Fix issues noted in code review, fix reverse style
darrenburns Mar 29, 2022
0f1bd55
Merge branch 'master' of github.com:Textualize/rich into export-svg
darrenburns Mar 29, 2022
8520041
Update SVG used in Rich docs
darrenburns Mar 29, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/update_benchmark_website.yml
Expand Up @@ -13,7 +13,7 @@ jobs:
python-version: '3.10.2'
- run: 'pip install asv'
- run: 'asv publish'
- uses: pina/github-action-push-to-another-repository@v1.4.1
- uses: cpina/github-action-push-to-another-repository@v1.4.1
name: 'Copy files to Textualize/rich-benchmarks repo'
env:
API_TOKEN_GITHUB: ${{ secrets.PUBLISH_ASV }}
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- SVG export functionality https://github.com/Textualize/rich/pull/2101

### Fixed

- Add missing `end` keyword argument to `Text.from_markup`
Expand Down
176 changes: 176 additions & 0 deletions docs/images/svg_export.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 29 additions & 3 deletions docs/source/console.rst
Expand Up @@ -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
-------------

Expand Down Expand Up @@ -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.

Expand Down
8 changes: 6 additions & 2 deletions rich/__main__.py
Expand Up @@ -51,7 +51,6 @@ def make_test_card() -> Table:
pad_edge=False,
)
color_table.add_row(
# "[bold yellow]256[/] colors or [bold green]16.7 million[/] colors [blue](if supported by your terminal)[/].",
(
"✓ [bold green]4-bit color[/]\n"
"✓ [bold blue]8-bit color[/]\n"
Expand Down Expand Up @@ -226,7 +225,12 @@ def iter_last(values: Iterable[T]) -> Iterable[Tuple[bool, T]]:
console.print(test_card)
taken = round((process_time() - start) * 1000.0, 1)

Console().print(test_card)
c = Console(record=True)
c.print(test_card)
c.save_svg(
path="/Users/darrenburns/Library/Application Support/JetBrains/PyCharm2021.3/scratches/scratch_7.svg",
title="Rich can export to SVG",
)

print(f"rendered in {pre_cache_taken}ms (cold cache)")
print(f"rendered in {taken}ms (warm cache)")
Expand Down