Calmcode - rich: constructions

How to combine Rich components in Python

1 2 3 4 5 6 7 8 9 10 11 12

Columns, Panels and Markdown

Rich is a flexible system of components that can really click together too. To demonstrate this we'll combine the Columns, Panel and Markdown classes together in the example below.

from rich.columns import Columns
from rich.console import Console
from rich.panel import Panel
from rich.markdown import Markdown

md1 = """
# Hello World

## This is Markdown

And it renders *very* **nicely**!
"""

md2 = """
## This is Markdown, Again

With code!

```python
print("hello world")
```
"""

console = Console(record=True)
panel_1 = Panel.fit(Markdown(md1), title="panel one", width=60)
panel_2 = Panel.fit(Markdown(md2), title="panel two", width=60)
console.print(Columns([panel_1, panel_2]))

Just remember that you can mix and match these components as you see fit. The goal of rich is that you're able to use each component as if it is a lego-brick.