File: rich.py

package info (click to toggle)
markdown-exec 1.10.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 740 kB
  • sloc: python: 2,497; javascript: 180; makefile: 32; sh: 30
file content (38 lines) | stat: -rw-r--r-- 1,335 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os

from rich.console import Console
from rich.padding import Padding
from rich.syntax import Syntax

# Here we hardcode the code snippet we want to render,
# but we could instead include it from somewhere else using the `pymdownx.snippets` extension
# (https://facelessuser.github.io/pymdown-extensions/extensions/snippets/)
# or by reading it dynamically from Python.
code = """
    from contextlib import asynccontextmanager
    import httpx


    class BookClient(httpx.AsyncClient):
        async def get_book(self, book_id: int) -> str:
            response = await self.get(f"/books/{book_id}")
            return response.text


    @asynccontextmanager
    async def book_client(*args, **kwargs):
        async with BookClient(*args, **kwargs) as client:
            yield client
"""

# We prevent Rich from actually writing to the terminal.
with open(os.devnull, "w") as devnull:
    console = Console(record=True, width=65, file=devnull, markup=False)
    renderable = Syntax(code, "python", theme="material")
    renderable = Padding(renderable, (0,), expand=False)
    console.print(renderable, markup=False)
svg = console.export_svg(title="async context manager")

# Wrapping the SVG in a div prevents it from being wrapped in a paragraph,
# which would add unnecessary space around it.
print(f"<div>{svg}</div>")