rich/docs/source/introduction.rst

107 lines
5.4 KiB
ReStructuredText
Raw Normal View History

2019-12-29 18:03:42 +00:00
Introduction
============
2022-02-22 10:37:16 +00:00
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.
2019-12-30 22:08:02 +00:00
2020-07-31 17:51:46 +00:00
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.
2019-12-29 18:03:42 +00:00
2020-04-19 13:40:22 +00:00
Requirements
------------
Rich works with OSX, Linux and Windows.
2022-01-03 09:04:35 +00:00
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.
2020-05-25 10:19:22 +00:00
2020-04-19 13:40:22 +00:00
Rich requires Python 3.6.1 and above. Note that Python 3.6.0 is *not* supported due to lack of support for methods on NamedTuples.
2019-12-29 18:03:42 +00:00
2020-08-13 14:39:16 +00:00
.. note::
PyCharm users will need to enable "emulate terminal" in output console option in run/debug configuration to see styled output.
2019-12-29 18:03:42 +00:00
Installation
------------
2021-11-13 11:19:11 +00:00
You can install Rich from PyPI with `pip` or your favorite package manager::
2019-12-29 18:03:42 +00:00
pip install rich
2019-12-30 22:08:02 +00:00
Add the ``-U`` switch to update to the current version, if Rich is already installed.
2019-12-29 18:03:42 +00:00
2020-07-31 09:44:06 +00:00
If you intend to use Rich with Jupyter then there are some additional dependencies which you can install with the following command::
2020-07-10 15:52:01 +00:00
pip install rich[jupyter]
2019-12-29 18:03:42 +00:00
Quick Start
-----------
2020-10-03 11:06:19 +00:00
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::
2019-12-29 18:03:42 +00:00
from rich import print
2022-02-22 10:37:16 +00:00
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.
2019-12-29 18:03:42 +00:00
2020-07-31 09:44:06 +00:00
Strings may contain :ref:`console_markup` which can be used to insert color and styles in to the output.
2019-12-29 18:03:42 +00:00
2019-12-30 22:08:02 +00:00
The following demonstrates both console markup and pretty formatting of Python objects::
2020-03-27 09:16:39 +00:00
>>> print("[italic red]Hello[/italic red] World!", locals())
2019-12-30 22:08:02 +00:00
This writes the following output to the terminal (including all the colors and styles):
.. raw:: html
2019-12-29 18:03:42 +00:00
2022-02-22 10:37:16 +00:00
<pre style="font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="color: #800000; font-style: italic">Hello</span> World!
2020-01-08 11:51:49 +00:00
<span style="font-weight: bold">{</span>
<span style="color: #008000">'__annotations__'</span>: <span style="font-weight: bold">{}</span>,
<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>,
<span style="color: #008000">'__doc__'</span>: <span style="color: #800080; font-style: italic">None</span>,
<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>,
<span style="color: #008000">'__name__'</span>: <span style="color: #008000">'__main__'</span>,
<span style="color: #008000">'__package__'</span>: <span style="color: #800080; font-style: italic">None</span>,
<span style="color: #008000">'__spec__'</span>: <span style="color: #800080; font-style: italic">None</span>,
<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>,
2019-12-30 22:08:02 +00:00
<span style="font-weight: bold">}</span> </pre>
2022-01-03 09:04:35 +00:00
If you would rather not shadow Python's built-in print, you can import ``rich.print`` as ``rprint`` (for example)::
2019-12-30 22:08:02 +00:00
from rich import print as rprint
2019-12-29 18:03:42 +00:00
2020-08-08 16:38:57 +00:00
Continue reading to learn about the more advanced features of Rich.
2021-06-06 19:00:52 +00:00
Rich in the REPL
----------------
2020-08-08 16:38:57 +00:00
2020-08-13 14:39:16 +00:00
Rich may be installed in the REPL so that Python data structures are automatically pretty printed with syntax highlighting. Here's how::
2020-08-08 16:38:57 +00:00
>>> from rich import pretty
2022-02-22 10:37:16 +00:00
>>> pretty.install()
2020-08-08 16:38:57 +00:00
>>> ["Rich and pretty", True]
You can also use this feature to try out Rich *renderables*. Here's an example::
2020-08-09 01:15:58 +00:00
>>> from rich.panel import Panel
2020-08-08 16:38:57 +00:00
>>> Panel.fit("[bold yellow]Hi, I'm a Panel", border_style="red")
2020-08-09 01:15:58 +00:00
Read on to learn more about Rich renderables.
2020-09-07 16:29:05 +00:00
2021-06-06 19:00:52 +00:00
IPython Extension
~~~~~~~~~~~~~~~~~
Rich also includes an IPython extension that will do this same pretty install + pretty tracebacks. Here's how to load it::
In [1]: %load_ext rich
2022-02-22 10:37:16 +00:00
You can also have it load by default by adding `"rich"` to the ``c.InteractiveShellApp.extension`` variable in
2021-06-06 19:00:52 +00:00
`IPython Configuration <https://ipython.readthedocs.io/en/stable/config/intro.html>`_.
2020-09-07 16:29:05 +00:00
2021-03-08 21:47:27 +00:00
Rich Inspect
------------
2020-09-07 16:29:05 +00:00
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::
>>> from rich import inspect
>>> from rich.color import Color
>>> color = Color.parse("red")
2021-11-13 11:18:02 +00:00
>>> inspect(color, methods=True)