Skip to content

Instantly share code, notes, and snippets.

@hamelsmu
Created December 16, 2020 21:59
Show Gist options
  • Save hamelsmu/9c6231e5ee6a578d59a829ee3df8f23b to your computer and use it in GitHub Desktop.
Save hamelsmu/9c6231e5ee6a578d59a829ee3df8f23b to your computer and use it in GitHub Desktop.
My notes on Rich
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from rich import print as pr"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from rich.panel import Panel"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">╭─────────────╮\n",
"│ <span style=\"color: #808000; font-weight: bold\">Hi I'm here</span> │\n",
"╰─────────────╯\n",
"</pre>\n"
],
"text/plain": [
"<rich.panel.Panel at 0x7fbca0bedee0>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Panel.fit(\"[bold yellow]Hi I'm here\", width=50000)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #000080\">╭─────────────────────────────── </span><span style=\"color: #000080; font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">class</span><span style=\"color: #000000\"> </span><span style=\"color: #008000\">'rich.color.Color'</span><span style=\"color: #000080; font-weight: bold\">&gt;</span><span style=\"color: #000080\"> ────────────────────────────────╮</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #008080\">Terminal color definition.</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #808000; font-style: italic\">is_default</span> = <span style=\"color: #ff0000; font-style: italic\">False</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #808000; font-style: italic\">is_system_defined</span> = <span style=\"color: #00ff00; font-style: italic\">True</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #808000; font-style: italic\">name</span> = <span style=\"color: #008000\">'red'</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #808000; font-style: italic\">number</span> = <span style=\"color: #000080; font-weight: bold\">1</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #808000; font-style: italic\">system</span> = <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">ColorSystem.STANDARD:</span><span style=\"color: #000000\"> </span><span style=\"color: #000080; font-weight: bold\">1</span><span style=\"font-weight: bold\">&gt;</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #808000; font-style: italic\">triplet</span> = <span style=\"color: #800080; font-style: italic\">None</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #808000; font-style: italic\">type</span> = <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">ColorType.STANDARD:</span><span style=\"color: #000000\"> </span><span style=\"color: #000080; font-weight: bold\">1</span><span style=\"font-weight: bold\">&gt;</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #808000; font-style: italic\">count</span> = <span style=\"color: #00ffff; font-style: italic\">def </span><span style=\"color: #800000; font-weight: bold\">count</span><span style=\"font-weight: bold\">(</span>value, <span style=\"color: #800080\">/</span><span style=\"font-weight: bold\">)</span>: <span style=\"color: #7f7f7f\">Return number of occurrences of value.</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #808000; font-style: italic\">default</span> = <span style=\"color: #00ffff; font-style: italic\">def </span><span style=\"color: #800000; font-weight: bold\">default</span><span style=\"font-weight: bold\">()</span> -&gt; <span style=\"color: #008000\">'Color'</span>: <span style=\"color: #7f7f7f\">Get a Color instance representing the </span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #7f7f7f\">default color.</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #808000; font-style: italic\">downgrade</span> = <span style=\"color: #00ffff; font-style: italic\">def </span><span style=\"color: #800000; font-weight: bold\">downgrade</span><span style=\"font-weight: bold\">(</span>system: rich.color.ColorSystem<span style=\"font-weight: bold\">)</span> -&gt; <span style=\"color: #008000\">'Color'</span>: <span style=\"color: #7f7f7f\">Downgrade a</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #7f7f7f\">color system to a system with fewer colors.</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #808000; font-style: italic\">from_ansi</span> = <span style=\"color: #00ffff; font-style: italic\">def </span><span style=\"color: #800000; font-weight: bold\">from_ansi</span><span style=\"font-weight: bold\">(</span>number: int<span style=\"font-weight: bold\">)</span> -&gt; <span style=\"color: #008000\">'Color'</span>: <span style=\"color: #7f7f7f\">Create a Color number from </span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #7f7f7f\">it's </span><span style=\"color: #7f7fbf; font-weight: bold\">8</span><span style=\"color: #7f7f7f\">-bit ansi number.</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #808000; font-style: italic\">from_rgb</span> = <span style=\"color: #00ffff; font-style: italic\">def </span><span style=\"color: #800000; font-weight: bold\">from_rgb</span><span style=\"font-weight: bold\">(</span>red: float, green: float, blue: float<span style=\"font-weight: bold\">)</span> -&gt; <span style=\"color: #008000\">'Color'</span>: <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #7f7f7f\">Create a truecolor from three color components in the range</span><span style=\"color: #7f7f7f; font-weight: bold\">(</span><span style=\"color: #7f7fbf; font-weight: bold\">0</span><span style=\"color: #7f7f7f\">-&gt;</span><span style=\"color: #7f7fbf; font-weight: bold\">255</span><span style=\"color: #7f7f7f; font-weight: bold\">)</span><span style=\"color: #7f7f7f\">.</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #808000; font-style: italic\">from_triplet</span> = <span style=\"color: #00ffff; font-style: italic\">def </span><span style=\"color: #800000; font-weight: bold\">from_triplet</span><span style=\"font-weight: bold\">(</span>triplet: <span style=\"color: #008000\">'ColorTriplet'</span><span style=\"font-weight: bold\">)</span> -&gt; <span style=\"color: #008000\">'Color'</span>: <span style=\"color: #7f7f7f\">Create a </span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #7f7f7f\">truecolor RGB color from a triplet of values.</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #808000; font-style: italic\">get_ansi_codes</span> = <span style=\"color: #00ffff; font-style: italic\">def </span><span style=\"color: #800000; font-weight: bold\">get_ansi_codes</span><span style=\"font-weight: bold\">(</span>foreground: bool = <span style=\"color: #00ff00; font-style: italic\">True</span><span style=\"font-weight: bold\">)</span> -&gt; Tuple<span style=\"font-weight: bold\">[</span>str, <span style=\"color: #808000\">...</span><span style=\"font-weight: bold\">]</span>: <span style=\"color: #7f7f7f\">Get </span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #7f7f7f\">the ANSI escape codes for this color.</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #808000; font-style: italic\">get_truecolor</span> = <span style=\"color: #00ffff; font-style: italic\">def </span><span style=\"color: #800000; font-weight: bold\">get_truecolor</span><span style=\"font-weight: bold\">(</span>theme: <span style=\"color: #008000\">'TerminalTheme'</span> = <span style=\"color: #800080; font-style: italic\">None</span>, <span style=\"color: #808000\">foreground</span>=<span style=\"color: #00ff00; font-style: italic\">True</span><span style=\"font-weight: bold\">)</span> -&gt; <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> rich.color_triplet.ColorTriplet: <span style=\"color: #7f7f7f\">Get an equivalent color triplet for </span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #7f7f7f\">this color.</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #808000; font-style: italic\">index</span> = <span style=\"color: #00ffff; font-style: italic\">def </span><span style=\"color: #800000; font-weight: bold\">index</span><span style=\"font-weight: bold\">(</span>value, <span style=\"color: #808000\">start</span>=<span style=\"color: #000080; font-weight: bold\">0</span>, <span style=\"color: #808000\">stop</span>=<span style=\"color: #000080; font-weight: bold\">9223372036854775807</span>, <span style=\"color: #800080\">/</span><span style=\"font-weight: bold\">)</span>: <span style=\"color: #7f7f7f\">Return first </span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #7f7f7f\">index of value.</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">│</span> <span style=\"color: #808000; font-style: italic\">parse</span> = <span style=\"color: #00ffff; font-style: italic\">def </span><span style=\"color: #800000; font-weight: bold\">parse</span><span style=\"font-weight: bold\">(</span>color: str<span style=\"font-weight: bold\">)</span> -&gt; <span style=\"color: #008000\">'Color'</span>: <span style=\"color: #7f7f7f\">Parse a color definition.</span> <span style=\"color: #000080\">│</span>\n",
"<span style=\"color: #000080\">╰───────────────────────────────────────────────────────────────────────────────────────────╯</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd044e670>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich import inspect\n",
"from rich.color import Color\n",
"color = Color.parse(\"red\")\n",
"inspect(color, methods=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Console API"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from rich.console import Console"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"console = Console()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #c0c0c0; background-color: #000080\">hello world! </span><span style=\"color: #000080; background-color: #000080; font-weight: bold\">323</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0527070>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"console.print(\"hello world! 323\", style=\"white on blue\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Hamel Camel <span style=\"font-weight: bold\">{</span><span style=\"color: #008000\">'__name__'</span>: <span style=\"color: #008000\">'__main__'</span>, <span style=\"color: #008000\">'__doc__'</span>: <span style=\"color: #008000\">'Automatically created module for IPython interactive environment'</span>, <span style=\"color: #008000\">'__package__'</span>: <span style=\"color: #800080; font-style: italic\">None</span>, <span style=\"color: #008000\">'__loader__'</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\">'__builtin__'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">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\">&gt;</span>, <span style=\"color: #008000\">'__builtins__'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">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\">&gt;</span>, <span style=\"color: #008000\">'_ih'</span>: <span style=\"font-weight: bold\">[</span><span style=\"color: #008000\">''</span>, <span style=\"color: #008000\">'from rich import print as pr'</span>, <span style=\"color: #008000\">'from rich.panel import Panel'</span>, <span style=\"color: #008000\">'Panel.fit(\"[bold yellow]Hi I\\'m here\", width=50000)'</span>, <span style=\"color: #008000\">'from rich import inspect\\nfrom rich.color import Color\\ncolor = Color.parse(\"red\")\\ninspect(color, methods=True)'</span>, <span style=\"color: #008000\">'from rich.console import Console'</span>, <span style=\"color: #008000\">'console = Console()'</span>, <span style=\"color: #008000\">'console.print(\"hello world! 323\", style=\"white on blue\")'</span>, <span style=\"color: #008000\">'console.out(\"Hamel\", \"Camel\", locals())'</span><span style=\"font-weight: bold\">]</span>, <span style=\"color: #008000\">'_oh'</span>: <span style=\"font-weight: bold\">{</span><span style=\"color: #000080; font-weight: bold\">3</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">rich.panel.Panel</span><span style=\"color: #000000\"> object at </span><span style=\"color: #000080; font-weight: bold\">0x7fbca0bedee0</span><span style=\"font-weight: bold\">&gt;}</span>, <span style=\"color: #008000\">'_dh'</span>: <span style=\"font-weight: bold\">[</span><span style=\"color: #008000\">'/Users/hamelsmu/github/ghtop/nbs'</span><span style=\"font-weight: bold\">]</span>, <span style=\"color: #008000\">'In'</span>: <span style=\"font-weight: bold\">[</span><span style=\"color: #008000\">''</span>, <span style=\"color: #008000\">'from rich import print as pr'</span>, <span style=\"color: #008000\">'from rich.panel import Panel'</span>, <span style=\"color: #008000\">'Panel.fit(\"[bold yellow]Hi I\\'m here\", width=50000)'</span>, <span style=\"color: #008000\">'from rich import inspect\\nfrom rich.color import Color\\ncolor = Color.parse(\"red\")\\ninspect(color, methods=True)'</span>, <span style=\"color: #008000\">'from rich.console import Console'</span>, <span style=\"color: #008000\">'console = Console()'</span>, <span style=\"color: #008000\">'console.print(\"hello world! 323\", style=\"white on blue\")'</span>, <span style=\"color: #008000\">'console.out(\"Hamel\", \"Camel\", locals())'</span><span style=\"font-weight: bold\">]</span>, <span style=\"color: #008000\">'Out'</span>: <span style=\"font-weight: bold\">{</span><span style=\"color: #000080; font-weight: bold\">3</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">rich.panel.Panel</span><span style=\"color: #000000\"> object at </span><span style=\"color: #000080; font-weight: bold\">0x7fbca0bedee0</span><span style=\"font-weight: bold\">&gt;}</span>, <span style=\"color: #008000\">'get_ipython'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">bound</span><span style=\"color: #000000\"> method InteractiveShell.get_ipython of &lt;ipykernel.zmqshell.ZMQInteractiveShell object at </span><span style=\"color: #000080; font-weight: bold\">0x7fbcc0492cd0</span><span style=\"font-weight: bold\">&gt;</span>&gt;, <span style=\"color: #008000\">'exit'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">IPython.core.autocall.ZMQExitAutocall</span><span style=\"color: #000000\"> object at </span><span style=\"color: #000080; font-weight: bold\">0x7fbcd03c7af0</span><span style=\"font-weight: bold\">&gt;</span>, <span style=\"color: #008000\">'quit'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">IPython.core.autocall.ZMQExitAutocall</span><span style=\"color: #000000\"> object at </span><span style=\"color: #000080; font-weight: bold\">0x7fbcd03c7af0</span><span style=\"font-weight: bold\">&gt;</span>, <span style=\"color: #008000\">'_'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">rich.panel.Panel</span><span style=\"color: #000000\"> object at </span><span style=\"color: #000080; font-weight: bold\">0x7fbca0bedee0</span><span style=\"font-weight: bold\">&gt;</span>, <span style=\"color: #008000\">'__'</span>: <span style=\"color: #008000\">''</span>, <span style=\"color: #008000\">'___'</span>: <span style=\"color: #008000\">''</span>, <span style=\"color: #008000\">'_i'</span>: <span style=\"color: #008000\">'console.print(\"hello world! 323\", style=\"white on blue\")'</span>, <span style=\"color: #008000\">'_ii'</span>: <span style=\"color: #008000\">'console = Console()'</span>, <span style=\"color: #008000\">'_iii'</span>: <span style=\"color: #008000\">'from rich.console import Console'</span>, <span style=\"color: #008000\">'_i1'</span>: <span style=\"color: #008000\">'from rich import print as pr'</span>, <span style=\"color: #008000\">'pr'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">function</span><span style=\"color: #000000\"> print at </span><span style=\"color: #000080; font-weight: bold\">0x7fbcd045c1f0</span><span style=\"font-weight: bold\">&gt;</span>, <span style=\"color: #008000\">'_i2'</span>: <span style=\"color: #008000\">'from rich.panel import Panel'</span>, <span style=\"color: #008000\">'Panel'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">class</span><span style=\"color: #000000\"> </span><span style=\"color: #008000\">'rich.panel.Panel'</span><span style=\"font-weight: bold\">&gt;</span>, <span style=\"color: #008000\">'_i3'</span>: <span style=\"color: #008000\">'Panel.fit(\"[bold yellow]Hi I\\'m here\", width=50000)'</span>, <span style=\"color: #008000\">'_3'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">rich.panel.Panel</span><span style=\"color: #000000\"> object at </span><span style=\"color: #000080; font-weight: bold\">0x7fbca0bedee0</span><span style=\"font-weight: bold\">&gt;</span>, <span style=\"color: #008000\">'_i4'</span>: <span style=\"color: #008000\">'from rich import inspect\\nfrom rich.color import Color\\ncolor = Color.parse(\"red\")\\ninspect(color, methods=True)'</span>, <span style=\"color: #008000\">'inspect'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">function</span><span style=\"color: #000000\"> inspect at </span><span style=\"color: #000080; font-weight: bold\">0x7fbcd045c3a0</span><span style=\"font-weight: bold\">&gt;</span>, <span style=\"color: #008000\">'Color'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">class</span><span style=\"color: #000000\"> </span><span style=\"color: #008000\">'rich.color.Color'</span><span style=\"font-weight: bold\">&gt;</span>, <span style=\"color: #008000\">'color'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">color</span><span style=\"color: #000000\"> </span><span style=\"color: #008000\">'red'</span><span style=\"color: #000000\"> </span><span style=\"color: #000000; font-weight: bold\">(</span><span style=\"color: #000000\">standard</span><span style=\"color: #000000; font-weight: bold\">)</span><span style=\"font-weight: bold\">&gt;</span>, <span style=\"color: #008000\">'_i5'</span>: <span style=\"color: #008000\">'from rich.console import Console'</span>, <span style=\"color: #008000\">'Console'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">class</span><span style=\"color: #000000\"> </span><span style=\"color: #008000\">'rich.console.Console'</span><span style=\"font-weight: bold\">&gt;</span>, <span style=\"color: #008000\">'_i6'</span>: <span style=\"color: #008000\">'console = Console()'</span>, <span style=\"color: #008000\">'console'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">console</span><span style=\"color: #000000\"> </span><span style=\"color: #808000\">width</span><span style=\"color: #000000\">=</span><span style=\"color: #000080; font-weight: bold\">93</span><span style=\"color: #000000\"> ColorSystem.TRUECOLOR</span><span style=\"font-weight: bold\">&gt;</span>, <span style=\"color: #008000\">'_i7'</span>: <span style=\"color: #008000\">'console.print(\"hello world! 323\", style=\"white on blue\")'</span>, <span style=\"color: #008000\">'_i8'</span>: <span style=\"color: #008000\">'console.out(\"Hamel\", \"Camel\", locals())'</span><span style=\"font-weight: bold\">}</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbce0237ac0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"console.out(\"Hamel\", \"Camel\", locals())"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800000; font-weight: bold\">Hamel</span> <span style=\"color: #00ff00\">───────────────────────────────────────────────────────────────────────────────────────</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbca0bede20>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"console.rule(\"[bold red]Hamel\", align='left')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #c0c0c0; background-color: #000080; font-weight: bold\">Rich</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0527f70>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #c0c0c0; background-color: #000080; font-weight: bold\">Rich </span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0527c40>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #c0c0c0; background-color: #000080; font-weight: bold\"> Rich </span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0527c70>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #c0c0c0; background-color: #000080; font-weight: bold\"> Rich</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0527a60>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"console = Console(width=10)\n",
"style = \"bold white on blue\"\n",
"console.print(\"Rich\", style=style)\n",
"console.print(\"Rich\", style=style, justify='left')\n",
"console.print(\"Rich\", style=style, justify=\"center\")\n",
"console.print(\"Rich\", style=style, justify=\"right\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"from rich.console import Console, OverflowMethod\n",
"overflow_methods: List[OverflowMethod] = [\"fold\", \"crop\", \"ellipsis\", \"ignore\"]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #00ff00\">── </span>fold<span style=\"color: #00ff00\"> ──</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd05217f0>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #000080; font-weight: bold\">supercalif</span>\n",
"<span style=\"color: #000080; font-weight: bold\">ragilistic</span>\n",
"<span style=\"color: #000080; font-weight: bold\">expialidoc</span>\n",
"<span style=\"color: #000080; font-weight: bold\">ious</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbce0237e80>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0527ac0>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #00ff00\">── </span>crop<span style=\"color: #00ff00\"> ──</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd05275b0>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #000080; font-weight: bold\">supercalif</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbce0237cd0>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbce0237cd0>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #00ff00\">─ </span>ellip…<span style=\"color: #00ff00\"> ─</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbce0237e80>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #000080; font-weight: bold\">supercali…</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbce0237e80>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd05275b0>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #00ff00\">─ </span>ignore<span style=\"color: #00ff00\"> ─</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd05271c0>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #000080; font-weight: bold\">supercalif</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0527eb0>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd05271c0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"supercali = \"supercalifragilisticexpialidocious\"\n",
"for overflow in overflow_methods:\n",
" console.rule(overflow)\n",
" console.print(supercali, overflow=overflow, style=\"bold blue\")\n",
" console.print()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800000; font-weight: bold\">This is an error!</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0521130>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import sys\n",
"from rich.console import Console\n",
"error_console = Console(file=sys.stderr)\n",
"error_console.print(\"[bold red]This is an error!\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0521dc0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich.console import Console\n",
"console = Console()\n",
"with console.capture() as capture:\n",
" console.print(\"[bold red]Hello[/] World\")\n",
"str_output = capture.get()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'\\x1b[1;31mHello\\x1b[0m World\\n'"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"str_output"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Rich features \n",
" \n",
" Colors ✓ 4-bit color ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ \n",
" ✓ 8-bit color ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ \n",
" ✓ Truecolor (16.7 million) ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ \n",
" ✓ Dumb terminals ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ \n",
" ✓ Automatic color conversion ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ \n",
" \n",
" Styles All ansi styles: bold, dim, italic, underline, strikethrough, reverse, and \n",
" even blink. \n",
" \n",
" Text Word wrap text. Justify left, center, right or full. \n",
" \n",
" Lorem ipsum dolor Lorem ipsum dolor Lorem ipsum dolor Lorem ipsum dolor \n",
" sit amet, sit amet, sit amet, sit amet, \n",
" consectetur consectetur consectetur consectetur \n",
" adipiscing elit. adipiscing elit. adipiscing elit. adipiscing elit. \n",
" Quisque in metus Quisque in metus Quisque in metus Quisque in metus \n",
" sed sapien sed sapien sed sapien sed sapien \n",
" ultricies pretium a ultricies pretium ultricies pretium a ultricies pretium a \n",
" at justo. Maecenas a at justo. at justo. Maecenas at justo. Maecenas \n",
" luctus velit et Maecenas luctus luctus velit et luctus velit et \n",
" auctor maximus. velit et auctor auctor maximus. auctor maximus. \n",
" maximus. \n",
" \n",
" Asian 🇨🇳 该库支持中文,日文和韩文文本! \n",
" language 🇯🇵 ライブラリは中国語、日本語、韓国語のテキストをサポートしています \n",
" support 🇰🇷 이 라이브러리는 중국어, 일본어 및 한국어 텍스트를 지원합니다 \n",
" \n",
" Markup Rich supports a simple bbcode like markup for color, style, and emoji! 👍 🍎 \n",
" 🐜 🐻 🥖 🚌 \n",
" \n",
" Tables Date Title Production Budget Box Office \n",
" ────────────────────────────────────────────────────────────────────────────── \n",
" Dec 20, 2019 Star Wars: The Rise of $275,000,000 $375,126,118 \n",
" Skywalker \n",
" May 25, 2018 Solo: A Star Wars Story $275,000,000 $393,151,347 \n",
" Dec 15, 2017 Star Wars Ep. VIII: The $262,000,000 $1,332,539,889 \n",
" Last Jedi \n",
" May 19, 1999 Star Wars Ep. I: The $115,000,000 $1,027,044,677 \n",
" phantom Menace \n",
" \n",
" Syntax 1 def iter_last(values: Iterable[T] { \n",
" highlighting 2 │ \"\"\"Iterate and generate a tup │ 'foo': [ \n",
" & 3 │ iter_values = iter(values) │ │ 3.1427, \n",
" pretty 4 │ try: │ │ ( \n",
" printing 5 │ │ previous_value = next(ite │ │ │ 'Paul Atriedies', \n",
" 6 │ except StopIteration: │ │ │ 'Vladimir Harkonnen', \n",
" 7 │ │ return │ │ │ 'Thufir Haway' \n",
" 8 │ for value in iter_values: │ │ ) \n",
" 9 │ │ yield False, previous_val │ ], \n",
" 10 │ │ previous_value = value │ 'atomic': (False, True, None) \n",
" 11 │ yield True, previous_value } \n",
" \n",
" Markdown # Markdown ╔════════════════════════════════════╗ \n",
" ║ Markdown ║ \n",
" Supports much of the *markdown*, ╚════════════════════════════════════╝ \n",
" __syntax__! \n",
" Supports much of the markdown, syntax! \n",
" - Headers \n",
" - Basic formatting: **bold**, • Headers \n",
" *italic*, `code` • Basic formatting: bold, italic, \n",
" - Block quotes code \n",
" - Lists, and more... • Block quotes \n",
" • Lists, and more... \n",
" \n",
" +more! Progress bars, columns, styled logging handler, tracebacks, etc... \n",
" \n"
]
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcb0132520>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich.__main__ import make_test_card\n",
"from rich.console import Console\n",
"\n",
"console = Console()\n",
"with console.pager():\n",
" console.print(make_test_card())"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #7fbfbf\">This is information</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbce025eac0>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800080\">The pod bay doors are locked</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0521e50>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800000\">Something terrible happened!</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0521610>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich.console import Console\n",
"from rich.theme import Theme\n",
"custom_theme = Theme({\n",
" \"info\" : \"dim cyan\",\n",
" \"warning\": \"magenta\",\n",
" \"danger\": \"red frame\"\n",
"})\n",
"console = Console(theme=custom_theme)\n",
"console.print(\"This is information\", style=\"info\")\n",
"console.print(\"[warning]The pod bay doors are locked[/warning]\")\n",
"console.print(\"Something terrible happened!\", style=\"danger\")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">see the default <span style=\"color: #000080; font-weight: bold\">43</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcc04a87c0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"console = Console()\n",
"console.print(\"see the default 43\")"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Visit my <a href=\"https://www.willmcgugan.com\">blog</a>!\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbce025e430>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pr(\"Visit my [link=https://www.willmcgugan.com]blog[/link]!\")"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">foohello\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcc04c8310>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich import print\n",
"print(\"foo[bar]hello[/bar]\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Highlighting"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Send funds to <span style=\"color: #800080; font-weight: bold\">[email protected]</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcc04a8ca0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich.console import Console\n",
"from rich.highlighter import RegexHighlighter\n",
"from rich.theme import Theme\n",
"\n",
"class EmailHighlighter(RegexHighlighter):\n",
" \"\"\"Apply style to anything that looks like an email.\"\"\"\n",
"\n",
" base_style = \"example.\"\n",
" highlights = [r\"(?P<foo>[\\w-]+@([\\w-]+\\.)+[\\w-]+)\"]\n",
"\n",
"\n",
"theme = Theme({\"example.foo\": \"bold magenta\"})\n",
"console = Console(highlighter=EmailHighlighter(), theme=theme)\n",
"console.print(\"Send funds to [email protected]\")"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Send funds to <span style=\"color: #800080; font-weight: bold\">[email protected]</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcc04c8970>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"console = Console(theme=theme)\n",
"highlight_emails = EmailHighlighter()\n",
"console.print(highlight_emails(\"Send funds to [email protected]\"))"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #ff87af\">I</span><span style=\"color: #87ff5f\"> </span><span style=\"color: #ff5f87\">m</span><span style=\"color: #87d787\">u</span><span style=\"color: #d7ff00\">s</span><span style=\"color: #878787\">t</span><span style=\"color: #5fafff\"> </span><span style=\"color: #878700\">n</span><span style=\"color: #d7005f\">o</span><span style=\"color: #5fff5f\">t</span><span style=\"color: #d7ffaf\"> </span><span style=\"color: #878700\">f</span><span style=\"color: #dadada\">e</span><span style=\"color: #00d787\">a</span><span style=\"color: #ffff87\">r</span><span style=\"color: #008787\">.</span><span style=\"color: #4e4e4e\"> </span><span style=\"color: #005f87\">F</span><span style=\"color: #005f5f\">e</span><span style=\"color: #87d7ff\">a</span><span style=\"color: #87005f\">r</span><span style=\"color: #af0000\"> </span><span style=\"color: #5fd75f\">i</span><span style=\"color: #767676\">s</span><span style=\"color: #af875f\"> </span><span style=\"color: #afd7ff\">t</span><span style=\"color: #5faf87\">h</span><span style=\"color: #ff5f5f\">e</span><span style=\"color: #121212\"> </span><span style=\"color: #5fd7af\">m</span><span style=\"color: #303030\">i</span><span style=\"color: #00af00\">n</span><span style=\"color: #d7d787\">d</span><span style=\"color: #d75fff\">-</span><span style=\"color: #5f0000\">k</span><span style=\"color: #00af87\">i</span><span style=\"color: #87ffff\">l</span><span style=\"color: #afffaf\">l</span><span style=\"color: #af87ff\">e</span><span style=\"color: #afaf00\">r</span><span style=\"color: #af875f\">.</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0490670>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from random import randint\n",
"\n",
"from rich import print\n",
"from rich.highlighter import Highlighter\n",
"\n",
"\n",
"class RainbowHighlighter(Highlighter):\n",
" def highlight(self, text):\n",
" for index in range(len(text)):\n",
" text.stylize(f\"color({randint(16, 255)})\", index, index + 1)\n",
"\n",
"\n",
"rainbow = RainbowHighlighter()\n",
"print(rainbow(\"I must not fear. Fear is the mind-killer.\"))"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #7fbfbf\">[09:29:39] </span><span style=\"color: #000080\">INFO </span> Hello, World! <a href=\"file://<ipython-input-24-fd274252165b>\"><span style=\"color: #7f7f7f\">&lt;ipython-input-24-fd274252165b&gt;</span></a><span style=\"color: #7f7f7f\">:10</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd05279a0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import logging\n",
"from rich.logging import RichHandler\n",
"\n",
"FORMAT = \"%(message)s\"\n",
"logging.basicConfig(\n",
" level=\"NOTSET\", format=FORMAT, datefmt=\"[%X]\", handlers=[RichHandler()]\n",
")\n",
"\n",
"log = logging.getLogger(\"rich\")\n",
"log.info(\"Hello, World!\")"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #7fbfbf\"> </span><span style=\"color: #000080\">INFO </span> Hello, World! <a href=\"file://<ipython-input-25-d3619869086d>\"><span style=\"color: #7f7f7f\">&lt;ipython-input-25-d3619869086d&gt;</span></a><span style=\"color: #7f7f7f\">:5</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbca0cd2760>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import logging\n",
"from rich.logging import RichHandler\n",
"\n",
"log = logging.getLogger(\"rich\")\n",
"log.info(\"Hello, World!\")"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #7fbfbf\"> </span><span style=\"color: #800000; font-weight: bold\">ERROR </span> unable print! <a href=\"file://<ipython-input-26-e72859521f95>\"><span style=\"color: #7f7f7f\">&lt;ipython-input-26-e72859521f95&gt;</span></a><span style=\"color: #7f7f7f\">:15</span>\n",
" Traceback <span style=\"font-weight: bold\">(</span>most recent call last<span style=\"font-weight: bold\">)</span>: \n",
" File \n",
" <span style=\"color: #008000\">\"&lt;ipython-input-26-e72859521f95&gt;\"</span>, \n",
" line <span style=\"color: #000080; font-weight: bold\">13</span>, in <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; font-weight: bold\">module</span><span style=\"font-weight: bold\">&gt;</span> \n",
" print<span style=\"font-weight: bold\">(</span><span style=\"color: #000080; font-weight: bold\">1</span> <span style=\"color: #800080\">/</span> <span style=\"color: #000080; font-weight: bold\">0</span><span style=\"font-weight: bold\">)</span> \n",
" ZeroDivisionError: division by zero \n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0593f70>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import logging\n",
"from rich.logging import RichHandler\n",
"\n",
"logging.basicConfig(\n",
" level=\"NOTSET\",\n",
" format=\"%(message)s\",\n",
" datefmt=\"[%X]\",\n",
" handlers=[RichHandler(rich_tracebacks=True)]\n",
")\n",
"\n",
"log = logging.getLogger(\"rich\")\n",
"try:\n",
" print(1 / 0)\n",
"except Exception:\n",
" log.exception(\"unable print!\")"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"def foo():\n",
" var = 'hamel'\n",
" return 1/0"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #ff0000\">╭──────────────────────────── </span><span style=\"color: #800000; font-weight: bold\">Traceback </span><span style=\"color: #bf7f7f; font-weight: bold\">(most recent call last)</span><span style=\"color: #ff0000\"> ────────────────────────────╮</span>\n",
"<span style=\"color: #ff0000\">│</span> <span style=\"color: #bfbf7f\">/Users/hamelsmu/github/ghtop/nbs/</span><span style=\"color: #808000; font-weight: bold\">&lt;ipython-input-28-268879839189&gt;</span>:<span style=\"color: #0000ff\">2</span> in <span style=\"color: #00ff00\">&lt;module&gt;</span> <span style=\"color: #ff0000\">│</span>\n",
"<span style=\"color: #ff0000\">│</span> <span style=\"color: #ff0000\">│</span>\n",
"<span style=\"color: #ff0000\">│</span> <span style=\"color: #bfbf7f\">/Users/hamelsmu/github/ghtop/nbs/</span><span style=\"color: #808000; font-weight: bold\">&lt;ipython-input-27-0a5881f1752c&gt;</span>:<span style=\"color: #0000ff\">3</span> in <span style=\"color: #00ff00\">foo</span> <span style=\"color: #ff0000\">│</span>\n",
"<span style=\"color: #ff0000\">╰───────────────────────────────────────────────────────────────────────────────────────────╯</span>\n",
"<span style=\"color: #ff0000; font-weight: bold\">ZeroDivisionError: </span>division by zero\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcc04c8fd0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"try:\n",
" foo()\n",
"except:\n",
" console.print_exception()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Styles"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800080\">Hello</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0593280>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"console.print(\"Hello\", style=\"magenta\")"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800080\">Hello</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd05934c0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"console.print(\"Hello\", style=\"color(5)\")"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #af00ff\">Hello</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0593370>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #af00ff\">Hello</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbca0cd2e50>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"console.print(\"Hello\", style=\"#af00ff\")\n",
"console.print(\"Hello\", style=\"rgb(175,0,255)\")"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800000; background-color: #c0c0c0\">DANGER!</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0593730>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"console.print(\"DANGER!\", style=\"red on white\")"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800000; font-weight: bold\">Danger, Will Robinson!</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0593940>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich.style import Style\n",
"danger_style = Style(color=\"red\", blink=True, bold=True)\n",
"console.print(\"Danger, Will Robinson!\", style=danger_style)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Prompt"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0593820>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter your name: Hamel\n"
]
}
],
"source": [
"from rich.prompt import Prompt\n",
"name = Prompt.ask(\"Enter your name\")\n"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0527be0>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter your name [Paul/Jessica/Duncan] (Paul): Paul\n"
]
}
],
"source": [
"from rich.prompt import Prompt\n",
"name = Prompt.ask(\"Enter your name\", choices=[\"Paul\", \"Jessica\", \"Duncan\"], default=\"Paul\")"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd0527c40>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter your name [1/2/3] (2): 1\n"
]
}
],
"source": [
"from rich.prompt import IntPrompt\n",
"name = IntPrompt.ask(\"Enter your name\", choices=['1', '2', '3'], default=2)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcc04a8fa0>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Do you like rich? [y/n]: y\n"
]
}
],
"source": [
"from rich.prompt import Confirm\n",
"is_rich_great = Confirm.ask(\"Do you like rich?\")\n",
"assert is_rich_great"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Table"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-style: italic\"> Star Wars Movies </span>\n",
"┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┓\n",
"┃<span style=\"font-weight: bold\"> Released </span>┃<span style=\"font-weight: bold\"> Title </span>┃<span style=\"font-weight: bold\"> Box Office </span>┃\n",
"┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━┩\n",
"│<span style=\"color: #008080\"> Dec 20, 2019 </span>│<span style=\"color: #800080\"> Star Wars: The Rise of Skywalker </span>│<span style=\"color: #008000\"> $952,110,690 </span>│\n",
"│<span style=\"color: #008080\"> May 25, 2018 </span>│<span style=\"color: #800080\"> Solo: A Star Wars Story </span>│<span style=\"color: #008000\"> $393,151,347 </span>│\n",
"│<span style=\"color: #008080\"> Dec 15, 2017 </span>│<span style=\"color: #800080\"> Star Wars Ep. V111: The Last Jedi </span>│<span style=\"color: #008000\"> $1,332,539,889 </span>│\n",
"│<span style=\"color: #008080\"> Dec 16, 2016 </span>│<span style=\"color: #800080\"> Rogue One: A Star Wars Story </span>│<span style=\"color: #008000\"> $1,332,439,889 </span>│\n",
"└──────────────┴───────────────────────────────────┴────────────────┘\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcc04c8ee0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich.console import Console\n",
"from rich.table import Table\n",
"\n",
"table = Table(title=\"Star Wars Movies\")\n",
"\n",
"table.add_column(\"Released\", justify=\"right\", style=\"cyan\", no_wrap=True)\n",
"table.add_column(\"Title\", style=\"magenta\")\n",
"table.add_column(\"Box Office\", justify=\"right\", style=\"green\")\n",
"\n",
"table.add_row(\"Dec 20, 2019\", \"Star Wars: The Rise of Skywalker\", \"$952,110,690\")\n",
"table.add_row(\"May 25, 2018\", \"Solo: A Star Wars Story\", \"$393,151,347\")\n",
"table.add_row(\"Dec 15, 2017\", \"Star Wars Ep. V111: The Last Jedi\", \"$1,332,539,889\")\n",
"table.add_row(\"Dec 16, 2016\", \"Rogue One: A Star Wars Story\", \"$1,332,439,889\")\n",
"\n",
"console = Console()\n",
"console.print(table)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-style: italic\">Star Wars Movies</span>\n",
" \n",
" <span style=\"font-weight: bold\"> Released </span> \n",
" ══════════════ \n",
" <span style=\"color: #008080\"> Dec 20, 2019 </span> \n",
" \n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd057eee0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich import box\n",
"table = Table(title=\"Star Wars Movies\", box=box.MINIMAL_DOUBLE_HEAD)\n",
"table.add_column(\"Released\", justify=\"right\", style=\"cyan\", no_wrap=True)\n",
"table.add_row(\"Dec 20, 2019\")\n",
"console = Console()\n",
"console.print(table)"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-style: italic\"> Star Wars Movies </span>\n",
"┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┓\n",
"┃<span style=\"font-weight: bold\"> Released </span>┃<span style=\"font-weight: bold\"> Title </span>┃<span style=\"font-weight: bold\"> Box Office </span>┃\n",
"┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━┩\n",
"│ Dec 20, 2019 │ Star Wars: The Rise of Skywalker │ $952,110,690 │\n",
"│ May 25, 2018 │ Solo: A Star Wars Story │ $393,151,347 │\n",
"│ Dec 15, 2017 │ Star Wars Ep. V111: The Last Jedi │ $1,332,539,889 │\n",
"│ Dec 16, 2016 │ Rogue One: A Star Wars Story │ $1,332,439,889 │\n",
"└──────────────┴───────────────────────────────────┴────────────────┘\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbce067a8e0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"table = Table(\"Released\", \"Title\", \"Box Office\", \n",
" title=\"Star Wars Movies\",)\n",
"table.add_row(\"Dec 20, 2019\", \"Star Wars: The Rise of Skywalker\", \"$952,110,690\")\n",
"table.add_row(\"May 25, 2018\", \"Solo: A Star Wars Story\", \"$393,151,347\")\n",
"table.add_row(\"Dec 15, 2017\", \"Star Wars Ep. V111: The Last Jedi\", \"$1,332,539,889\")\n",
"table.add_row(\"Dec 16, 2016\", \"Rogue One: A Star Wars Story\", \"$1,332,439,889\")\n",
"\n",
"console = Console()\n",
"console.print(table)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# More complex tables - nested"
]
},
{
"cell_type": "code",
"execution_count": 135,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-style: italic\"> Random Table </span>\n",
"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┓\n",
"┃<span style=\"font-weight: bold\"> A </span>┃<span style=\"font-weight: bold\"> B </span>┃<span style=\"font-weight: bold\"> C </span>┃\n",
"┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━┩\n",
"│ Dec 20, 2019 │ Star Wars: The Rise of Skywalker │ $952,110,690 │\n",
"│ <span style=\"font-style: italic\"> Star Wars Movies </span> │ d │ e │\n",
"│ ┏━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━┓ │ │ │\n",
"│ ┃<span style=\"font-weight: bold\"> Released </span>┃<span style=\"font-weight: bold\"> Title </span>┃<span style=\"font-weight: bold\"> Box </span>┃ │ │ │\n",
"│ ┃ ┃ ┃<span style=\"font-weight: bold\"> Office </span>┃ │ │ │\n",
"│ ┡━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━┩ │ │ │\n",
"│ │ Dec 20, │ Star Wars: │ $952,110… │ │ │ │\n",
"│ │ 2019 │ The Rise │ │ │ │ │\n",
"│ │ │ of │ │ │ │ │\n",
"│ │ │ Skywalker │ │ │ │ │\n",
"│ │ May 25, │ Solo: A │ $393,151… │ │ │ │\n",
"│ │ 2018 │ Star Wars │ │ │ │ │\n",
"│ │ │ Story │ │ │ │ │\n",
"│ │ Dec 15, │ Star Wars │ $1,332,5… │ │ │ │\n",
"│ │ 2017 │ Ep. V111: │ │ │ │ │\n",
"│ │ │ The Last │ │ │ │ │\n",
"│ │ │ Jedi │ │ │ │ │\n",
"│ │ Dec 16, │ Rogue One: │ $1,332,4… │ │ │ │\n",
"│ │ 2016 │ A Star │ │ │ │ │\n",
"│ │ │ Wars Story │ │ │ │ │\n",
"│ └────────────┴────────────┴───────────┘ │ │ │\n",
"│ <span style=\"font-style: italic\"> Star Wars Movies </span> │ │ │\n",
"│ ┏━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━┓ │ │ │\n",
"│ ┃<span style=\"font-weight: bold\"> Released </span>┃<span style=\"font-weight: bold\"> Title </span>┃<span style=\"font-weight: bold\"> Box </span>┃ │ │ │\n",
"│ ┃ ┃ ┃<span style=\"font-weight: bold\"> Office </span>┃ │ │ │\n",
"│ ┡━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━┩ │ │ │\n",
"│ │ Dec 20, │ Star Wars: │ $952,110… │ │ │ │\n",
"│ │ 2019 │ The Rise │ │ │ │ │\n",
"│ │ │ of │ │ │ │ │\n",
"│ │ │ Skywalker │ │ │ │ │\n",
"│ │ May 25, │ Solo: A │ $393,151… │ │ │ │\n",
"│ │ 2018 │ Star Wars │ │ │ │ │\n",
"│ │ │ Story │ │ │ │ │\n",
"│ │ Dec 15, │ Star Wars │ $1,332,5… │ │ │ │\n",
"│ │ 2017 │ Ep. V111: │ │ │ │ │\n",
"│ │ │ The Last │ │ │ │ │\n",
"│ │ │ Jedi │ │ │ │ │\n",
"│ │ Dec 16, │ Rogue One: │ $1,332,4… │ │ │ │\n",
"│ │ 2016 │ A Star │ │ │ │ │\n",
"│ │ │ Wars Story │ │ │ │ │\n",
"│ └────────────┴────────────┴───────────┘ │ │ │\n",
"└─────────────────────────────────────────┴──────────────────────────────────┴──────────────┘\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbce0bbc820>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"table2 = Table(\"A\", \"B\", \"C\", \n",
" title=\"Random Table\",)\n",
"rg = RenderGroup(table, table)\n",
"table2.add_row(\"Dec 20, 2019\", \"Star Wars: The Rise of Skywalker\", \"$952,110,690\")\n",
"table2.add_row(rg, \"d\", \"e\")\n",
"\n",
"console.print(table2)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Raising shields <span style=\"color: #800080; font-weight: bold\">COMPLETED </span><span style=\"color: #008000; font-weight: bold\">✔</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd057e250>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich import print as pr\n",
"from rich.table import Table\n",
"\n",
"grid = Table.grid(expand=True)\n",
"grid.add_column(\"col1\")\n",
"grid.add_column(\"col2\", justify=\"right\")\n",
"grid.add_row(\"Raising shields\", \"[bold magenta]COMPLETED [green]:heavy_check_mark:\")\n",
"\n",
"pr(grid)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Padding"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"> \n",
" Hello \n",
" \n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd057e130>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich import print\n",
"from rich.padding import Padding\n",
"test = Padding(\"Hello\", 1)\n",
"print(test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Panel"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">╭───────────────────────────────────────────────────────────────────────────────────────────╮\n",
"│ Hello, <span style=\"color: #800000\">World!</span> │\n",
"╰───────────────────────────────────────────────────────────────────────────────────────────╯\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcd057ef70>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich import print as pr\n",
"from rich.panel import Panel\n",
"pr(Panel(\"Hello, [red]World!\"))"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">╭───────────────╮\n",
"│ Hello, <span style=\"color: #800000\">World!</span> │\n",
"╰───────────────╯\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbca0cd2730>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pr(Panel.fit(\"Hello, [red]World!\"))"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">╭───────────────╮\n",
"│ Hello, <span style=\"color: #800000\">World!</span> │\n",
"╰───────────────╯\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcc06588e0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pr(Panel(\"Hello, [red]World!\", expand=False))"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">╭───────────────────────────────────────── Welcome ─────────────────────────────────────────╮\n",
"│ Hello, World! │\n",
"│ foo │\n",
"╰───────────────────────────────────────────────────────────────────────────────────────────╯\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbca0eef4f0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pr(Panel(\"Hello, World!\\nfoo\", title=\"Welcome\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Render Groups"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">╭───────────────────────────────────────────────────────────────────────────────────────────╮\n",
"│ <span style=\"background-color: #c0c0c0\">╭──────────────────────────────────────── Hamel ────────────────────────────────────────╮</span> │\n",
"│ <span style=\"background-color: #c0c0c0\">│ Hello │</span> │\n",
"│ <span style=\"background-color: #c0c0c0\">╰───────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
"│ <span style=\"background-color: #c0c0c0\">╭───────────────────────────────────────────────────────────────────────────────────────╮</span> │\n",
"│ <span style=\"background-color: #c0c0c0\">│ World │</span> │\n",
"│ <span style=\"background-color: #c0c0c0\">╰───────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
"╰───────────────────────────────────────────────────────────────────────────────────────────╯\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbca0eb1f10>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich import print\n",
"from rich.console import RenderGroup\n",
"from rich.panel import Panel\n",
"\n",
"panel_group = RenderGroup(\n",
" Panel(\"Hello\", style=\"on white\", title=\"Hamel\"),\n",
" Panel(\"World\", style=\"on white\"),\n",
")\n",
"print(Panel(panel_group))"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">╭───────────────────────────────────────────────────────────────────────────────────────────╮\n",
"│ <span style=\"background-color: #c0c0c0\">╭───────────────────────────────────────────────────────────────────────────────────────╮</span> │\n",
"│ <span style=\"background-color: #c0c0c0\">│</span><span style=\"background-color: #c0c0c0\">Hello </span><span style=\"background-color: #c0c0c0\">│</span> │\n",
"│ <span style=\"background-color: #c0c0c0\">╰───────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
"│ <span style=\"background-color: #c0c0c0\">╭───────────────────────────────────────────────────────────────────────────────────────╮</span> │\n",
"│ <span style=\"background-color: #c0c0c0\">│</span><span style=\"background-color: #c0c0c0\">Hello </span><span style=\"background-color: #c0c0c0\">│</span> │\n",
"│ <span style=\"background-color: #c0c0c0\">╰───────────────────────────────────────────────────────────────────────────────────────╯</span> │\n",
"╰───────────────────────────────────────────────────────────────────────────────────────────╯\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbc8035c880>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich import print\n",
"from rich.console import render_group\n",
"from rich.panel import Panel\n",
"\n",
"@render_group()\n",
"def get_panels():\n",
" for i in range(2):\n",
" yield Panel(\"Hello\", style=\"on white\", padding=0, border_style=\"frame\")\n",
"\n",
"@render_group()\n",
"def get_panels_bottom():\n",
" for i in range(2):\n",
" yield Panel(\"Bottom\", style=\"on white\", padding=0, border_style=\"frame\")\n",
" \n",
"\n",
"print(Panel(get_panels()))"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"background-color: #c0c0c0\">╭──────╮╭──────╮</span>\n",
"<span style=\"background-color: #c0c0c0\">│</span><span style=\"background-color: #c0c0c0\">Hello </span><span style=\"background-color: #c0c0c0\">││</span><span style=\"background-color: #c0c0c0\">Hello </span><span style=\"background-color: #c0c0c0\">│</span>\n",
"<span style=\"background-color: #c0c0c0\">╰──────╯╰──────╯</span>\n",
"<span style=\"background-color: #c0c0c0\">╭──────╮╭──────╮</span>\n",
"<span style=\"background-color: #c0c0c0\">│</span><span style=\"background-color: #c0c0c0\">Hello </span><span style=\"background-color: #c0c0c0\">││</span><span style=\"background-color: #c0c0c0\">Hello </span><span style=\"background-color: #c0c0c0\">│</span>\n",
"<span style=\"background-color: #c0c0c0\">╰──────╯╰──────╯</span>\n",
"<span style=\"background-color: #c0c0c0\">╭──────╮╭──────╮</span>\n",
"<span style=\"background-color: #c0c0c0\">│</span><span style=\"background-color: #c0c0c0\">Bottom</span><span style=\"background-color: #c0c0c0\">││</span><span style=\"background-color: #c0c0c0\">Bottom</span><span style=\"background-color: #c0c0c0\">│</span>\n",
"<span style=\"background-color: #c0c0c0\">╰──────╯╰──────╯</span>\n",
"<span style=\"background-color: #c0c0c0\">╭──────╮╭──────╮</span>\n",
"<span style=\"background-color: #c0c0c0\">│</span><span style=\"background-color: #c0c0c0\">Bottom</span><span style=\"background-color: #c0c0c0\">││</span><span style=\"background-color: #c0c0c0\">Bottom</span><span style=\"background-color: #c0c0c0\">│</span>\n",
"<span style=\"background-color: #c0c0c0\">╰──────╯╰──────╯</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbc80a0cbb0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"grid = Table.grid()\n",
"grid.add_column(\"col1\")\n",
"grid.add_column(\"col2\")\n",
"grid.add_row(get_panels(), get_panels())\n",
"grid.add_row(get_panels_bottom(), get_panels_bottom())\n",
"\n",
"pr(grid)"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [],
"source": [
"def get_panels():\n",
" for i in range(2):\n",
" yield Panel(\"Hello\", style=\"on white\", padding=0, border_style=\"frame\")\n"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"background-color: #c0c0c0\">╭─────╮</span> <span style=\"background-color: #c0c0c0\">╭─────╮</span>\n",
"<span style=\"background-color: #c0c0c0\">│</span><span style=\"background-color: #c0c0c0\">Hello</span><span style=\"background-color: #c0c0c0\">│</span> <span style=\"background-color: #c0c0c0\">│</span><span style=\"background-color: #c0c0c0\">Hello</span><span style=\"background-color: #c0c0c0\">│</span>\n",
"<span style=\"background-color: #c0c0c0\">╰─────╯</span> <span style=\"background-color: #c0c0c0\">╰─────╯</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbc80b41e20>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich.columns import Columns\n",
"columns = Columns(get_panels())\n",
"pr(columns)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Live Display"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3dc7c6c9e03642e3b8673d5d929f89d4",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Output()"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import time\n",
"\n",
"from rich.live import Live\n",
"from rich.table import Table\n",
"\n",
"table = Table()\n",
"table.add_column(\"Row ID\")\n",
"table.add_column(\"Description\")\n",
"table.add_column(\"Level\")\n",
"\n",
"with Live(table, refresh_per_second=4): # update 4 times a second to feel fluid\n",
" for row in range(12):\n",
" time.sleep(0.4) # arbitrary delay\n",
" # update the renderable internally\n",
" table.add_row(f\"{row}\", f\"description {row}\", \"[red]ERROR\")"
]
},
{
"cell_type": "code",
"execution_count": 148,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Output()"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbc80a02940>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import random\n",
"import time\n",
"\n",
"from rich.live import Live\n",
"from rich.table import Table\n",
"\n",
"\n",
"def generate_table() -> Table:\n",
"\n",
" table = Table()\n",
" table.add_column(\"ID\")\n",
" table.add_column(\"Value\")\n",
" table.add_column(\"Status\")\n",
"\n",
" for row in range(random.randint(2, 6)):\n",
" value = random.random() * 100\n",
" table.add_row(\n",
" f\"{row}\", f\"{value:3.2f}\", \"[red]ERROR\" if value < 50 else \"[green]SUCCESS\"\n",
" )\n",
" return table\n",
"\n",
"\n",
"with Live(refresh_per_second=4, transient=True) as live:\n",
" for _ in range(40):\n",
" time.sleep(0.4)\n",
" live.update(generate_table())\n",
" live.refresh()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Progress"
]
},
{
"cell_type": "code",
"execution_count": 140,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "888545ca0d3f41e1baffc8598112e8eb",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Output()"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich.progress import track\n",
"n=10\n",
"def do_work(x): time.sleep(.2)\n",
" \n",
"for n in track(range(n), description=\"Processing...\"):\n",
" do_work(n)"
]
},
{
"cell_type": "code",
"execution_count": 144,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "eb3a0ebc2e08479bb2764d558760c189",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Output()"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-144-2ad3aef4fd9b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mprogress\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtask2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0madvance\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0.3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0mprogress\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtask3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0madvance\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0.9\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0.12\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"from rich.progress import Progress\n",
"import time\n",
"with Progress() as progress:\n",
"\n",
" task1 = progress.add_task(\"[red]Downloading...\", total=1000, start=False)\n",
" task2 = progress.add_task(\"[green]Processing...\", total=1000)\n",
" task3 = progress.add_task(\"[cyan]Cooking...\", total=1000)\n",
"\n",
" time.sleep(2)\n",
" progress.start_task(task1)\n",
" while not progress.finished:\n",
" progress.update(task1, advance=0.5)\n",
" progress.update(task2, advance=0.3)\n",
" progress.update(task3, advance=0.9)\n",
" time.sleep(0.12)"
]
},
{
"cell_type": "code",
"execution_count": 146,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "686efdc543ab4903b411f5b956a1797c",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Output()"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich.progress import Progress, BarColumn, TimeRemainingColumn\n",
"import time\n",
"with Progress(\n",
" \"[progress.description]{task.description}\",\n",
" BarColumn(),\n",
" \"[progress.percentage]{task.percentage:>3.0f}%\",\n",
" TimeRemainingColumn(),\n",
") as progress:\n",
"\n",
" task1 = progress.add_task(\"[red]Downloading...\", total=1000, start=False)\n",
" task2 = progress.add_task(\"[green]Processing...\", total=1000)\n",
" task3 = progress.add_task(\"[cyan]Cooking...\", total=1000)\n",
"\n",
" time.sleep(2)\n",
" progress.start_task(task1)\n",
" while not progress.finished:\n",
" progress.update(task1, advance=0.5)\n",
" progress.update(task2, advance=0.3)\n",
" progress.update(task3, advance=0.9)\n",
" time.sleep(0.12)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Markdown"
]
},
{
"cell_type": "code",
"execution_count": 149,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">╔═══════════════════════════════════════════════════════════════════════════════════════════╗\n",
"║ <span style=\"font-weight: bold\">This is an h1</span> ║\n",
"╚═══════════════════════════════════════════════════════════════════════════════════════════╝\n",
"\n",
"Rich can do a pretty <span style=\"font-style: italic\">decent</span> job of rendering markdown. \n",
"\n",
"<span style=\"color: #808000; font-weight: bold\"> 1 </span>This is a list item \n",
"<span style=\"color: #808000; font-weight: bold\"> 2 </span>This is another list item \n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbce0be6940>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"MARKDOWN = \"\"\"\n",
"# This is an h1\n",
"\n",
"Rich can do a pretty *decent* job of rendering markdown.\n",
"\n",
"1. This is a list item\n",
"2. This is another list item\n",
"\"\"\"\n",
"from rich.console import Console\n",
"from rich.markdown import Markdown\n",
"\n",
"console = Console()\n",
"md = Markdown(MARKDOWN)\n",
"console.print(md)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Syntax"
]
},
{
"cell_type": "code",
"execution_count": 180,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #e3e3dd; background-color: #272822; font-weight: bold\"> </span><span style=\"color: #656660; background-color: #272822\"> 1 </span><span style=\"color: #f8f8f2; background-color: #272822\">__all__ </span><span style=\"color: #f92672; background-color: #272822\">=</span><span style=\"color: #f8f8f2; background-color: #272822\"> [</span><span style=\"color: #e6db74; background-color: #272822\">'term'</span><span style=\"color: #f8f8f2; background-color: #272822\">, </span><span style=\"color: #e6db74; background-color: #272822\">'github_auth_device'</span><span style=\"color: #f8f8f2; background-color: #272822\">, </span><span style=\"color: #e6db74; background-color: #272822\">'limit_cb'</span><span style=\"color: #f8f8f2; background-color: #272822\">, </span><span style=\"color: #e6db74; background-color: #272822\">'Events'</span><span style=\"color: #f8f8f2; background-color: #272822\">, </span><span style=\"color: #e6db74; background-color: #272822\">'print_event'</span><span style=\"color: #f8f8f2; background-color: #272822\">, </span><span style=\"color: #e6db74; background-color: #272822\">'tail_even</span>\n",
"<span style=\"color: #e3e3dd; background-color: #272822; font-weight: bold\"> </span><span style=\"color: #656660; background-color: #272822\"> 2 </span><span style=\"background-color: #272822\"> </span>\n",
"<span style=\"color: #e3e3dd; background-color: #272822; font-weight: bold\"> </span><span style=\"color: #656660; background-color: #272822\"> 3 </span><span style=\"color: #f8f8f2; background-color: #272822\"> </span><span style=\"color: #e6db74; background-color: #272822\">'simple'</span><span style=\"color: #f8f8f2; background-color: #272822\">, </span><span style=\"color: #e6db74; background-color: #272822\">'main'</span><span style=\"color: #f8f8f2; background-color: #272822\">]</span><span style=\"background-color: #272822\"> </span>\n",
"<span style=\"color: #e3e3dd; background-color: #272822; font-weight: bold\"> </span><span style=\"color: #656660; background-color: #272822\"> 4 </span><span style=\"background-color: #272822\"> </span>\n",
"<span style=\"color: #e3e3dd; background-color: #272822; font-weight: bold\"> </span><span style=\"color: #656660; background-color: #272822\"> 5 </span><span style=\"background-color: #272822\"> </span>\n",
"<span style=\"color: #e3e3dd; background-color: #272822; font-weight: bold\"> </span><span style=\"color: #656660; background-color: #272822\"> 6 </span><span style=\"background-color: #272822\"> </span>\n",
"<span style=\"color: #e3e3dd; background-color: #272822; font-weight: bold\"> </span><span style=\"color: #656660; background-color: #272822\"> 7 </span><span style=\"background-color: #272822\"> </span>\n",
"<span style=\"color: #e3e3dd; background-color: #272822; font-weight: bold\"> </span><span style=\"color: #656660; background-color: #272822\"> 8 </span><span style=\"background-color: #272822\"> </span>\n",
"<span style=\"color: #e3e3dd; background-color: #272822; font-weight: bold\"> </span><span style=\"color: #656660; background-color: #272822\"> 9 </span><span style=\"color: #f92672; background-color: #272822\">import</span><span style=\"color: #f8f8f2; background-color: #272822\"> sys</span><span style=\"color: #f92672; background-color: #272822\">,</span><span style=\"color: #f8f8f2; background-color: #272822\"> signal</span><span style=\"color: #f92672; background-color: #272822\">,</span><span style=\"color: #f8f8f2; background-color: #272822\"> shutil</span><span style=\"color: #f92672; background-color: #272822\">,</span><span style=\"color: #f8f8f2; background-color: #272822\"> os</span><span style=\"color: #f92672; background-color: #272822\">,</span><span style=\"color: #f8f8f2; background-color: #272822\"> json</span><span style=\"color: #f92672; background-color: #272822\">,</span><span style=\"color: #f8f8f2; background-color: #272822\"> emoji</span><span style=\"color: #f92672; background-color: #272822\">,</span><span style=\"color: #f8f8f2; background-color: #272822\"> enlighten</span><span style=\"background-color: #272822\"> </span>\n",
"<span style=\"color: #e3e3dd; background-color: #272822; font-weight: bold\"> </span><span style=\"color: #656660; background-color: #272822\">10 </span><span style=\"background-color: #272822\"> </span>\n",
"<span style=\"color: #e3e3dd; background-color: #272822; font-weight: bold\"> </span><span style=\"color: #656660; background-color: #272822\">11 </span><span style=\"color: #f92672; background-color: #272822\">from</span><span style=\"color: #f8f8f2; background-color: #272822\"> dashing </span><span style=\"color: #f92672; background-color: #272822\">import</span><span style=\"color: #f8f8f2; background-color: #272822\"> </span><span style=\"color: #f92672; background-color: #272822\">*</span><span style=\"background-color: #272822\"> </span>\n",
"<span style=\"color: #e3e3dd; background-color: #272822; font-weight: bold\"> </span><span style=\"color: #656660; background-color: #272822\">12 </span><span style=\"background-color: #272822\"> </span>\n",
"<span style=\"color: #e3e3dd; background-color: #272822; font-weight: bold\"> </span><span style=\"color: #656660; background-color: #272822\">13 </span><span style=\"color: #f92672; background-color: #272822\">from</span><span style=\"color: #f8f8f2; background-color: #272822\"> collections </span><span style=\"color: #f92672; background-color: #272822\">import</span><span style=\"color: #f8f8f2; background-color: #272822\"> defaultdict</span><span style=\"background-color: #272822\"> </span>\n",
"<span style=\"color: #e3e3dd; background-color: #272822; font-weight: bold\"> </span><span style=\"color: #656660; background-color: #272822\">14 </span><span style=\"background-color: #272822\"> </span>\n",
"<span style=\"color: #e3e3dd; background-color: #272822; font-weight: bold\"> </span><span style=\"color: #656660; background-color: #272822\">15 </span><span style=\"color: #f92672; background-color: #272822\">from</span><span style=\"color: #f8f8f2; background-color: #272822\"> warnings </span><span style=\"color: #f92672; background-color: #272822\">import</span><span style=\"color: #f8f8f2; background-color: #272822\"> warn</span><span style=\"background-color: #272822\"> </span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7fbcc04bbf10>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich.console import Console\n",
"from rich.syntax import Syntax\n",
"\n",
"console = Console()\n",
"\n",
"with open(\"/Users/hamelsmu/github/ghtop/ghtop/ghtop.py\", \"rt\") as code_file:\n",
" code = '\\n'.join(code_file.readlines()[:10])\n",
" syntax = Syntax(code, \"python\", line_numbers=True)\n",
"console.print(syntax)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Console Protocol"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hamel\n"
]
}
],
"source": [
"from rich.console import Console, ConsoleOptions, RenderResult, Segment, Style\n",
"console = Console()\n",
"class MyObject:\n",
" def __rich__(self) -> str:\n",
" return \"[bold cyan]MyObject()\"\n",
" def __str__(self): return \"hamel\"\n",
"\n",
" \n",
"mo = MyObject()\n",
"print(mo)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #008080; font-weight: bold\">MyObject()</span>\n",
"</pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7f7f521c12e0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"console.print(mo)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"class MyObject:\n",
" def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderResult:\n",
" yield Segment(\"My\", Style(color=\"magenta\"))\n",
" yield Segment(\"Object\", Style(color=\"green\"))\n",
" yield Segment(\"()\", Style(color=\"cyan\"))\n",
" \n",
"mo = MyObject()"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<__main__.MyObject object at 0x7f7f51f905b0>\n"
]
}
],
"source": [
"print(mo)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800080\">My</span><span style=\"color: #008000\">Object</span><span style=\"color: #008080\">()</span></pre>\n"
],
"text/plain": [
"<rich.jupyter.JupyterRenderable at 0x7f7f51eb1700>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"console.print(mo)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Notes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Jeremy used Panel to draw the box around the thing."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Use the status spinners https://rich.readthedocs.io/en/latest/console.html#status. I like the ping pong ball and the box being drawn in the demo `python -m rich.spinner`\n",
"\n",
"2. Use the rich prompt https://rich.readthedocs.io/en/latest/prompt.html\n",
"3. Render progress bars and update it , and customize what is shown https://rich.readthedocs.io/en/latest/progress.html#columns"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment