Debian Codegraph @

docs/source/introduction.rst

raw
1 Introduction
2 ============
3
4 Rich is a Python library for writing *rich* text (with color and style) to the terminal, and for displaying advanced content such as tables, markdown, and syntax highlighted code.
5
6 Use Rich to make your command line applications visually appealing and present data in a more readable way. Rich can also be a useful debugging aid by pretty printing and syntax highlighting data structures.
7
8 Requirements
9 ------------
10
11 Rich works with macOS, Linux and Windows.
12
13 On Windows both the (ancient) cmd.exe terminal is supported and the new `Windows Terminal <https://github.com/microsoft/terminal/releases>`_. The latter has much improved support for color and style.
14
15 Rich requires Python 3.8.0 and above.
16
17 .. note::
18 PyCharm users will need to enable "emulate terminal" in output console option in run/debug configuration to see styled output.
19
20 Installation
21 ------------
22
23 You can install Rich from PyPI with `pip` or your favorite package manager::
24
25 pip install rich
26
27 Add the ``-U`` switch to update to the current version, if Rich is already installed.
28
29 If you intend to use Rich with Jupyter then there are some additional dependencies which you can install with the following command::
30
31 pip install "rich[jupyter]"
32
33 Demo
34 ----
35
36 To verify that Rich is installed correctly and to see a quick demonstration of its features, run the following command in your terminal::
37
38 python -m rich
39
40 Quick Start
41 -----------
42
43 The quickest way to get up and running with Rich is to import the alternative ``print`` function which takes the same arguments as the built-in ``print`` and may be used as a drop-in replacement. Here's how you would do that::
44
45 from rich import print
46
47 You can then print strings or objects to the terminal in the usual way. Rich will do some basic syntax :ref:`highlighting<highlighting>` and format data structures to make them easier to read.
48
49 Strings may contain :ref:`console_markup` which can be used to insert color and styles in to the output.
50
51 The following demonstrates both console markup and pretty formatting of Python objects::
52
53 >>> print("[italic red]Hello[/italic red] World!", locals())
54
55 This writes the following output to the terminal (including all the colors and styles):
56
57 .. raw:: html
58
59 <pre style="font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="color: #800000; font-style: italic">Hello</span> World!
60 <span style="font-weight: bold">{</span>
61 <span style="color: #008000">'__annotations__'</span>: <span style="font-weight: bold">{}</span>,
62 <span style="color: #008000">'__builtins__'</span>: <span style="font-weight: bold"><</span><span style="color: #ff00ff">module</span><span style="color: #000000"> </span><span style="color: #008000">'builtins'</span><span style="color: #000000"> </span><span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">built-in</span><span style="color: #000000; font-weight: bold">)</span><span style="font-weight: bold">></span>,
63 <span style="color: #008000">'__doc__'</span>: <span style="color: #800080; font-style: italic">None</span>,
64 <span style="color: #008000">'__loader__'</span>: <span style="font-weight: bold"><</span><span style="color: #ff00ff">class</span><span style="color: #000000"> </span><span style="color: #008000">'_frozen_importlib.BuiltinImporter'</span><span style="font-weight: bold">></span>,
65 <span style="color: #008000">'__name__'</span>: <span style="color: #008000">'__main__'</span>,
66 <span style="color: #008000">'__package__'</span>: <span style="color: #800080; font-style: italic">None</span>,
67 <span style="color: #008000">'__spec__'</span>: <span style="color: #800080; font-style: italic">None</span>,
68 <span style="color: #008000">'print'</span>: <span style="font-weight: bold"><</span><span style="color: #ff00ff">function</span><span style="color: #000000"> print at </span><span style="color: #000080; font-weight: bold">0x1027fd4c0</span><span style="font-weight: bold">></span>,
69 <span style="font-weight: bold">}</span> </pre>
70
71
72 If you would rather not shadow Python's built-in print, you can import ``rich.print`` as ``rprint`` (for example)::
73
74 from rich import print as rprint
75
76 Continue reading to learn about the more advanced features of Rich.
77
78 Rich in the REPL
79 ----------------
80
81 Rich may be installed in the REPL so that Python data structures are automatically pretty printed with syntax highlighting. Here's how::
82
83 >>> from rich import pretty
84 >>> pretty.install()
85 >>> ["Rich and pretty", True]
86
87 You can also use this feature to try out Rich *renderables*. Here's an example::
88
89 >>> from rich.panel import Panel
90 >>> Panel.fit("[bold yellow]Hi, I'm a Panel", border_style="red")
91
92 Read on to learn more about Rich renderables.
93
94 IPython Extension
95 ~~~~~~~~~~~~~~~~~
96
97 Rich also includes an IPython extension that will do this same pretty install + pretty tracebacks. Here's how to load it::
98
99 In [1]: %load_ext rich
100
101 You can also have it load by default by adding `"rich"` to the ``c.InteractiveShellApp.extension`` variable in
102 `IPython Configuration <https://ipython.readthedocs.io/en/stable/config/intro.html>`_.
103
104 Rich Inspect
105 ------------
106
107 Rich has an :meth:`~rich.inspect` function which can generate a report on any Python object. It is a fantastic debug aid, and a good example of the output that Rich can generate. Here is a simple example::
108
109 >>> from rich import inspect
110 >>> from rich.color import Color
111 >>> color = Color.parse("red")
112 >>> inspect(color, methods=True)