Below are a few additional example files you can add to expand your collection of Rich demonstrations. These new examples illustrate other useful features of Rich, such as displaying hierarchical data, creating layouts, and showing a spinner for asynchronous tasks.
This file demonstrates how to use the Tree class to display a hierarchical structure.
#!/usr/bin/env python3
from rich.console import Console
from rich.tree import Tree
console = Console()
# Create a root node
tree = Tree("Root")
# Add child nodes
child1 = tree.add("Child 1")
child1.add("Grandchild 1.1")
child1.add("Grandchild 1.2")
child2 = tree.add("Child 2")
child2.add("Grandchild 2.1")
child2.add("Grandchild 2.2")
console.print(tree)Explanation:
This script builds a tree structure with a root and two child branches, each with its own grandchildren. It then prints the tree to the terminal.
This file demonstrates how to create a terminal layout using the Layout class. It arranges several panels on the screen.
#!/usr/bin/env python3
from rich.console import Console
from rich.layout import Layout
from rich.panel import Panel
console = Console()
# Create the main layout
layout = Layout()
layout.split(
Layout(name="upper", size=3),
Layout(name="lower")
)
# Update the upper panel
layout["upper"].update(Panel("This is the upper panel", title="Upper"))
# Split the lower layout into two columns
layout["lower"].split_row(
Layout(Panel("Left lower panel", title="Left")),
Layout(Panel("Right lower panel", title="Right"))
)
console.print(layout)Explanation:
The script defines a layout with an upper section and a lower section. The lower section is further divided into two columns, each containing its own panel.
This file demonstrates how to display a spinner using Rich’s status context manager. This is useful for showing an animated spinner while performing a task.
#!/usr/bin/env python3
import time
from rich.console import Console
console = Console()
with console.status("[bold green]Processing...[/bold green]", spinner="dots"):
# Simulate a task taking some time
time.sleep(3)
console.print("[bold green]Done![/bold green]")Explanation:
This script uses console.status() to display a spinner with the text "Processing..." while it simulates a task by sleeping for three seconds. Once the task is complete, it prints "Done!".
You can update your project documentation to include these new files. For instance, add entries to your main menu (if desired) and include descriptions in your Markdown tutorial under a new section for additional examples.
With these extra examples, your project now covers:
- Basic coloured text
- Emojis and Markdown
- Syntax highlighting
- Tables
- Progress bars
- Live updates
- Logging
- Hierarchical trees
- Custom layouts
- Spinners
Feel free to adjust the examples further to suit your needs. Enjoy experimenting with Rich!
Below is the updated Table of Contents that includes the new examples:
- Prerequisites
- Project Setup
- Creating the Example Files
- Creating the Main Menu
- Running the Project
- Customisation and Further Improvements
You can paste this updated TOC directly into your documentation.