Skip to content

Commit

Permalink
Create rich-console-output.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jatonline authored Aug 19, 2024
1 parent 45adf45 commit 2100297
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Python/rich-console-output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Rich console output, pretty printing and formatting

If you want your script's console output to have colours and other formatting, check out [Rich](https://github.com/Textualize/rich).

See also: A [video introduction to Rich](https://calmcode.io/rich/introduction.html) by calmcode.

## Basic printing

You can easily add colours, emojis and other formatting with basic tags using an [improved version of `print()`](https://github.com/Textualize/rich#rich-print):

```python
from rich import print

print("Hello, [bold magenta]World[/bold magenta]! :vampire:")
```

Any variables you print also get nicer formatting and colour syntax highlighting.

## Inspecting objects

If you have an object with multiple attributes and methods, you can a nice overview of everything using [`rich.inspect()`](https://github.com/Textualize/rich#rich-inspect). I've used this quite a bit.

```python
from rich import inspect

my_list = ["foo", "bar"]
inspect(my_list, methods=True)
```

## Example

Via the [Rich GitHub repo](https://github.com/Textualize/rich):

![Example console output printed by `python -m rich`](https://raw.githubusercontent.com/textualize/rich/master/imgs/features.png)

0 comments on commit 2100297

Please sign in to comment.