Files
sspai-100-hours-series-python/code/newsletter/N14/rich.ipynb

2062 lines
129 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 焕然一新的 print 与 pprint 函数"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, [bold red]100gle[/bold red]!\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\">Hello, <span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">100gle</span>!\n",
"</pre>\n"
],
"text/plain": [
"Hello, \u001b[1;31m100gle\u001b[0m!\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"print(\"Hello, [bold red]100gle[/bold red]!\")\n",
"\n",
"from rich import print as rprint\n",
"rprint(\"Hello, [bold red]100gle[/bold red]!\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">3</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">5</span><span style=\"font-weight: bold\">]</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[1m[\u001b[0m\u001b[1;36m1\u001b[0m, \u001b[1;36m2\u001b[0m, \u001b[1;36m3\u001b[0m, \u001b[1;36m4\u001b[0m, \u001b[1;36m5\u001b[0m\u001b[1m]\u001b[0m\n"
]
},
"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=\"font-weight: bold\">{</span>\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'__name__'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'__main__'</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'__doc__'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'Automatically created module for IPython interactive environment'</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'__package__'</span>: <span style=\"color: #800080; text-decoration-color: #800080; font-style: italic\">None</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'__loader__'</span>: <span style=\"color: #800080; text-decoration-color: #800080; font-style: italic\">None</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'__spec__'</span>: <span style=\"color: #800080; text-decoration-color: #800080; font-style: italic\">None</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'__builtin__'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff; font-weight: bold\">module</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #008000; text-decoration-color: #008000\">'builtins'</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #000000; text-decoration-color: #000000\">built-in</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">)</span><span style=\"font-weight: bold\">&gt;</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'__builtins__'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff; font-weight: bold\">module</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #008000; text-decoration-color: #008000\">'builtins'</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #000000; text-decoration-color: #000000\">built-in</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">)</span><span style=\"font-weight: bold\">&gt;</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'_ih'</span>: <span style=\"font-weight: bold\">[</span>\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ │ </span><span style=\"color: #008000; text-decoration-color: #008000\">''</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ │ </span><span style=\"color: #008000; text-decoration-color: #008000\">'print(\"Hello, [bold red]100gle[/bold red]!\")\\n\\nfrom rich import print as rprint\\nrprint(\"Hello, [bold red]100gle[/bold red]!\")'</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ │ </span><span style=\"color: #008000; text-decoration-color: #008000\">'from rich.pretty import pprint\\nnumbers = [1, 2, 3, 4, 5]\\npprint(numbers)\\npprint(locals())'</span>\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"font-weight: bold\">]</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'_oh'</span>: <span style=\"font-weight: bold\">{}</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'_dh'</span>: <span style=\"font-weight: bold\">[</span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">PosixPath</span><span style=\"font-weight: bold\">(</span><span style=\"color: #008000; text-decoration-color: #008000\">'/Users/Bobot/Repositories/Python-Courses/pandas-startup/code/newsletters/N14'</span><span style=\"font-weight: bold\">)]</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'In'</span>: <span style=\"font-weight: bold\">[</span>\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ │ </span><span style=\"color: #008000; text-decoration-color: #008000\">''</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ │ </span><span style=\"color: #008000; text-decoration-color: #008000\">'print(\"Hello, [bold red]100gle[/bold red]!\")\\n\\nfrom rich import print as rprint\\nrprint(\"Hello, [bold red]100gle[/bold red]!\")'</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ │ </span><span style=\"color: #008000; text-decoration-color: #008000\">'from rich.pretty import pprint\\nnumbers = [1, 2, 3, 4, 5]\\npprint(numbers)\\npprint(locals())'</span>\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"font-weight: bold\">]</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'Out'</span>: <span style=\"font-weight: bold\">{}</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'get_ipython'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff; font-weight: bold\">bound</span><span style=\"color: #000000; text-decoration-color: #000000\"> method InteractiveShell.get_ipython of &lt;ipykernel.zmqshell.ZMQInteractiveShell object at </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0x7f88a8732190</span><span style=\"font-weight: bold\">&gt;</span>&gt;,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'exit'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff; font-weight: bold\">IPython.core.autocall.ZMQExitAutocall</span><span style=\"color: #000000; text-decoration-color: #000000\"> object at </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0x7f88d838d280</span><span style=\"font-weight: bold\">&gt;</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'quit'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff; font-weight: bold\">IPython.core.autocall.ZMQExitAutocall</span><span style=\"color: #000000; text-decoration-color: #000000\"> object at </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0x7f88d838d280</span><span style=\"font-weight: bold\">&gt;</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'_'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">''</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'__'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">''</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'___'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">''</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'sys'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff; font-weight: bold\">module</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #008000; text-decoration-color: #008000\">'sys'</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">(</span><span style=\"color: #000000; text-decoration-color: #000000\">built-in</span><span style=\"color: #000000; text-decoration-color: #000000; font-weight: bold\">)</span><span style=\"font-weight: bold\">&gt;</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'os'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff; font-weight: bold\">module</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #008000; text-decoration-color: #008000\">'os'</span><span style=\"color: #000000; text-decoration-color: #000000\"> from </span><span style=\"color: #008000; text-decoration-color: #008000\">'/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py'</span><span style=\"font-weight: bold\">&gt;</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'__vsc_ipynb_file__'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'/Users/Bobot/Repositories/Python-Courses/pandas-startup/code/newsletters/N14/rich.ipynb'</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'_i'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'print(\"Hello, [bold red]100gle[/bold red]!\")\\n\\nfrom rich import print as rprint\\nrprint(\"Hello, [bold red]100gle[/bold red]!\")'</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'_ii'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">''</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'_iii'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">''</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'_i1'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'print(\"Hello, [bold red]100gle[/bold red]!\")\\n\\nfrom rich import print as rprint\\nrprint(\"Hello, [bold red]100gle[/bold red]!\")'</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'rprint'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff; font-weight: bold\">function</span><span style=\"color: #000000; text-decoration-color: #000000\"> print at </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0x7f88987e3a60</span><span style=\"font-weight: bold\">&gt;</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'_i2'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'from rich.pretty import pprint\\nnumbers = [1, 2, 3, 4, 5]\\npprint(numbers)\\npprint(locals())'</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'pprint'</span>: <span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff; font-weight: bold\">function</span><span style=\"color: #000000; text-decoration-color: #000000\"> pprint at </span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0x7f88d8816e50</span><span style=\"font-weight: bold\">&gt;</span>,\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ </span><span style=\"color: #008000; text-decoration-color: #008000\">'numbers'</span>: <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">3</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">5</span><span style=\"font-weight: bold\">]</span>\n",
"<span style=\"font-weight: bold\">}</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[1m{\u001b[0m\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'__name__'\u001b[0m: \u001b[32m'__main__'\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'__doc__'\u001b[0m: \u001b[32m'Automatically created module for IPython interactive environment'\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'__package__'\u001b[0m: \u001b[3;35mNone\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'__loader__'\u001b[0m: \u001b[3;35mNone\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'__spec__'\u001b[0m: \u001b[3;35mNone\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'__builtin__'\u001b[0m: \u001b[1m<\u001b[0m\u001b[1;95mmodule\u001b[0m\u001b[39m \u001b[0m\u001b[32m'builtins'\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m(\u001b[0m\u001b[39mbuilt-in\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1m>\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'__builtins__'\u001b[0m: \u001b[1m<\u001b[0m\u001b[1;95mmodule\u001b[0m\u001b[39m \u001b[0m\u001b[32m'builtins'\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m(\u001b[0m\u001b[39mbuilt-in\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1m>\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'_ih'\u001b[0m: \u001b[1m[\u001b[0m\n",
"\u001b[2;32m│ │ \u001b[0m\u001b[32m''\u001b[0m,\n",
"\u001b[2;32m│ │ \u001b[0m\u001b[32m'print\u001b[0m\u001b[32m(\u001b[0m\u001b[32m\"Hello, \u001b[0m\u001b[32m[\u001b[0m\u001b[32mbold red\u001b[0m\u001b[32m]\u001b[0m\u001b[32m100gle\u001b[0m\u001b[32m[\u001b[0m\u001b[32m/bold red\u001b[0m\u001b[32m]\u001b[0m\u001b[32m!\"\u001b[0m\u001b[32m)\u001b[0m\u001b[32m\\n\\nfrom rich import print as rprint\\nrprint\u001b[0m\u001b[32m(\u001b[0m\u001b[32m\"Hello, \u001b[0m\u001b[32m[\u001b[0m\u001b[32mbold red\u001b[0m\u001b[32m]\u001b[0m\u001b[32m100gle\u001b[0m\u001b[32m[\u001b[0m\u001b[32m/bold red\u001b[0m\u001b[32m]\u001b[0m\u001b[32m!\"\u001b[0m\u001b[32m)\u001b[0m\u001b[32m'\u001b[0m,\n",
"\u001b[2;32m│ │ \u001b[0m\u001b[32m'from rich.pretty import pprint\\nnumbers = \u001b[0m\u001b[32m[\u001b[0m\u001b[32m1, 2, 3, 4, 5\u001b[0m\u001b[32m]\u001b[0m\u001b[32m\\npprint\u001b[0m\u001b[32m(\u001b[0m\u001b[32mnumbers\u001b[0m\u001b[32m)\u001b[0m\u001b[32m\\npprint\u001b[0m\u001b[32m(\u001b[0m\u001b[32mlocals\u001b[0m\u001b[32m(\u001b[0m\u001b[32m)\u001b[0m\u001b[32m)\u001b[0m\u001b[32m'\u001b[0m\n",
"\u001b[2;32m│ \u001b[0m\u001b[1m]\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'_oh'\u001b[0m: \u001b[1m{\u001b[0m\u001b[1m}\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'_dh'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;35mPosixPath\u001b[0m\u001b[1m(\u001b[0m\u001b[32m'/Users/Bobot/Repositories/Python-Courses/pandas-startup/code/newsletters/N14'\u001b[0m\u001b[1m)\u001b[0m\u001b[1m]\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'In'\u001b[0m: \u001b[1m[\u001b[0m\n",
"\u001b[2;32m│ │ \u001b[0m\u001b[32m''\u001b[0m,\n",
"\u001b[2;32m│ │ \u001b[0m\u001b[32m'print\u001b[0m\u001b[32m(\u001b[0m\u001b[32m\"Hello, \u001b[0m\u001b[32m[\u001b[0m\u001b[32mbold red\u001b[0m\u001b[32m]\u001b[0m\u001b[32m100gle\u001b[0m\u001b[32m[\u001b[0m\u001b[32m/bold red\u001b[0m\u001b[32m]\u001b[0m\u001b[32m!\"\u001b[0m\u001b[32m)\u001b[0m\u001b[32m\\n\\nfrom rich import print as rprint\\nrprint\u001b[0m\u001b[32m(\u001b[0m\u001b[32m\"Hello, \u001b[0m\u001b[32m[\u001b[0m\u001b[32mbold red\u001b[0m\u001b[32m]\u001b[0m\u001b[32m100gle\u001b[0m\u001b[32m[\u001b[0m\u001b[32m/bold red\u001b[0m\u001b[32m]\u001b[0m\u001b[32m!\"\u001b[0m\u001b[32m)\u001b[0m\u001b[32m'\u001b[0m,\n",
"\u001b[2;32m│ │ \u001b[0m\u001b[32m'from rich.pretty import pprint\\nnumbers = \u001b[0m\u001b[32m[\u001b[0m\u001b[32m1, 2, 3, 4, 5\u001b[0m\u001b[32m]\u001b[0m\u001b[32m\\npprint\u001b[0m\u001b[32m(\u001b[0m\u001b[32mnumbers\u001b[0m\u001b[32m)\u001b[0m\u001b[32m\\npprint\u001b[0m\u001b[32m(\u001b[0m\u001b[32mlocals\u001b[0m\u001b[32m(\u001b[0m\u001b[32m)\u001b[0m\u001b[32m)\u001b[0m\u001b[32m'\u001b[0m\n",
"\u001b[2;32m│ \u001b[0m\u001b[1m]\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'Out'\u001b[0m: \u001b[1m{\u001b[0m\u001b[1m}\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'get_ipython'\u001b[0m: \u001b[1m<\u001b[0m\u001b[1;95mbound\u001b[0m\u001b[39m method InteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at \u001b[0m\u001b[1;36m0x7f88a8732190\u001b[0m\u001b[1m>\u001b[0m>,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'exit'\u001b[0m: \u001b[1m<\u001b[0m\u001b[1;95mIPython.core.autocall.ZMQExitAutocall\u001b[0m\u001b[39m object at \u001b[0m\u001b[1;36m0x7f88d838d280\u001b[0m\u001b[1m>\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'quit'\u001b[0m: \u001b[1m<\u001b[0m\u001b[1;95mIPython.core.autocall.ZMQExitAutocall\u001b[0m\u001b[39m object at \u001b[0m\u001b[1;36m0x7f88d838d280\u001b[0m\u001b[1m>\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'_'\u001b[0m: \u001b[32m''\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'__'\u001b[0m: \u001b[32m''\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'___'\u001b[0m: \u001b[32m''\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'sys'\u001b[0m: \u001b[1m<\u001b[0m\u001b[1;95mmodule\u001b[0m\u001b[39m \u001b[0m\u001b[32m'sys'\u001b[0m\u001b[39m \u001b[0m\u001b[1;39m(\u001b[0m\u001b[39mbuilt-in\u001b[0m\u001b[1;39m)\u001b[0m\u001b[1m>\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'os'\u001b[0m: \u001b[1m<\u001b[0m\u001b[1;95mmodule\u001b[0m\u001b[39m \u001b[0m\u001b[32m'os'\u001b[0m\u001b[39m from \u001b[0m\u001b[32m'/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py'\u001b[0m\u001b[1m>\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'__vsc_ipynb_file__'\u001b[0m: \u001b[32m'/Users/Bobot/Repositories/Python-Courses/pandas-startup/code/newsletters/N14/rich.ipynb'\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'_i'\u001b[0m: \u001b[32m'print\u001b[0m\u001b[32m(\u001b[0m\u001b[32m\"Hello, \u001b[0m\u001b[32m[\u001b[0m\u001b[32mbold red\u001b[0m\u001b[32m]\u001b[0m\u001b[32m100gle\u001b[0m\u001b[32m[\u001b[0m\u001b[32m/bold red\u001b[0m\u001b[32m]\u001b[0m\u001b[32m!\"\u001b[0m\u001b[32m)\u001b[0m\u001b[32m\\n\\nfrom rich import print as rprint\\nrprint\u001b[0m\u001b[32m(\u001b[0m\u001b[32m\"Hello, \u001b[0m\u001b[32m[\u001b[0m\u001b[32mbold red\u001b[0m\u001b[32m]\u001b[0m\u001b[32m100gle\u001b[0m\u001b[32m[\u001b[0m\u001b[32m/bold red\u001b[0m\u001b[32m]\u001b[0m\u001b[32m!\"\u001b[0m\u001b[32m)\u001b[0m\u001b[32m'\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'_ii'\u001b[0m: \u001b[32m''\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'_iii'\u001b[0m: \u001b[32m''\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'_i1'\u001b[0m: \u001b[32m'print\u001b[0m\u001b[32m(\u001b[0m\u001b[32m\"Hello, \u001b[0m\u001b[32m[\u001b[0m\u001b[32mbold red\u001b[0m\u001b[32m]\u001b[0m\u001b[32m100gle\u001b[0m\u001b[32m[\u001b[0m\u001b[32m/bold red\u001b[0m\u001b[32m]\u001b[0m\u001b[32m!\"\u001b[0m\u001b[32m)\u001b[0m\u001b[32m\\n\\nfrom rich import print as rprint\\nrprint\u001b[0m\u001b[32m(\u001b[0m\u001b[32m\"Hello, \u001b[0m\u001b[32m[\u001b[0m\u001b[32mbold red\u001b[0m\u001b[32m]\u001b[0m\u001b[32m100gle\u001b[0m\u001b[32m[\u001b[0m\u001b[32m/bold red\u001b[0m\u001b[32m]\u001b[0m\u001b[32m!\"\u001b[0m\u001b[32m)\u001b[0m\u001b[32m'\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'rprint'\u001b[0m: \u001b[1m<\u001b[0m\u001b[1;95mfunction\u001b[0m\u001b[39m print at \u001b[0m\u001b[1;36m0x7f88987e3a60\u001b[0m\u001b[1m>\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'_i2'\u001b[0m: \u001b[32m'from rich.pretty import pprint\\nnumbers = \u001b[0m\u001b[32m[\u001b[0m\u001b[32m1, 2, 3, 4, 5\u001b[0m\u001b[32m]\u001b[0m\u001b[32m\\npprint\u001b[0m\u001b[32m(\u001b[0m\u001b[32mnumbers\u001b[0m\u001b[32m)\u001b[0m\u001b[32m\\npprint\u001b[0m\u001b[32m(\u001b[0m\u001b[32mlocals\u001b[0m\u001b[32m(\u001b[0m\u001b[32m)\u001b[0m\u001b[32m)\u001b[0m\u001b[32m'\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'pprint'\u001b[0m: \u001b[1m<\u001b[0m\u001b[1;95mfunction\u001b[0m\u001b[39m pprint at \u001b[0m\u001b[1;36m0x7f88d8816e50\u001b[0m\u001b[1m>\u001b[0m,\n",
"\u001b[2;32m│ \u001b[0m\u001b[32m'numbers'\u001b[0m: \u001b[1m[\u001b[0m\u001b[1;36m1\u001b[0m, \u001b[1;36m2\u001b[0m, \u001b[1;36m3\u001b[0m, \u001b[1;36m4\u001b[0m, \u001b[1;36m5\u001b[0m\u001b[1m]\u001b[0m\n",
"\u001b[1m}\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich.pretty import pprint\n",
"numbers = [1, 2, 3, 4, 5]\n",
"pprint(numbers)\n",
"pprint(locals())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Markup 标记语法"
]
},
{
"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\">Hello, <span style=\"font-weight: bold\">100gle</span>!\n",
"</pre>\n"
],
"text/plain": [
"Hello, \u001b[1m100gle\u001b[0m!\n"
]
},
"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\">Hello, <span style=\"font-style: italic\">100gle</span>!\n",
"</pre>\n"
],
"text/plain": [
"Hello, \u001b[3m100gle\u001b[0m!\n"
]
},
"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\">Hello, <span style=\"text-decoration: underline\">100gle</span>\n",
"</pre>\n"
],
"text/plain": [
"Hello, \u001b[4m100gle\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich import print as rprint\n",
"\n",
"rprint(\"Hello, [bold]100gle[/bold]!\")\n",
"rprint(\"Hello, [italic]100gle[/italic]!\")\n",
"rprint(\"Hello, [u]100gle[/u]\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"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\">Hello, <span style=\"font-weight: bold\">100gle</span>!\n",
"</pre>\n"
],
"text/plain": [
"Hello, \u001b[1m100gle\u001b[0m!\n"
]
},
"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\">Hello, <span style=\"font-style: italic\">100gle</span>!\n",
"</pre>\n"
],
"text/plain": [
"Hello, \u001b[3m100gle\u001b[0m!\n"
]
},
"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\">Hello, <span style=\"text-decoration: underline\">100gle</span>\n",
"</pre>\n"
],
"text/plain": [
"Hello, \u001b[4m100gle\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"rprint(\"Hello, [bold]100gle[/]!\")\n",
"rprint(\"Hello, [italic]100gle[/]!\")\n",
"rprint(\"Hello, [u]100gle[/]\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"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-weight: bold\">[</span>info<span style=\"font-weight: bold\">]</span> Hello, world!\n",
"</pre>\n"
],
"text/plain": [
"\u001b[1m[\u001b[0minfo\u001b[1m]\u001b[0m Hello, world!\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"rprint(\"\\[info] Hello, world!\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"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\">Hello, SSPAI! 🤪\n",
"</pre>\n"
],
"text/plain": [
"Hello, SSPAI! 🤪\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"rprint(\"Hello, SSPAI! :zany_face:\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 开箱即用的 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\">Hello, <span style=\"font-weight: bold\">SSPAI</span>!\n",
"</pre>\n"
],
"text/plain": [
"Hello, \u001b[1mSSPAI\u001b[0m!\n"
]
},
"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": [
"👍\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich.console import Console\n",
"\n",
"console = Console()\n",
"console.print(\"Hello, [b]SSPAI[/]!\")\n",
"console.print(\":+1:\")"
]
},
{
"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\"><span style=\"color: #7fbfbf; text-decoration-color: #7fbfbf\">[11:17:10] </span>Hey, here! <a href=\"file:///var/folders/0t/s0c95rbs6ds7w_b0d471p0kc0000gn/T/ipykernel_9731/4013596842.py\" target=\"_blank\"><span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">4013596842.py</span></a><span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">:</span><a href=\"file:///var/folders/0t/s0c95rbs6ds7w_b0d471p0kc0000gn/T/ipykernel_9731/4013596842.py#1\" target=\"_blank\"><span style=\"color: #7f7f7f; text-decoration-color: #7f7f7f\">1</span></a>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[2;36m[11:17:10]\u001b[0m\u001b[2;36m \u001b[0mHey, here! \u001b]8;id=422532;file:///var/folders/0t/s0c95rbs6ds7w_b0d471p0kc0000gn/T/ipykernel_9731/4013596842.py\u001b\\\u001b[2m4013596842.py\u001b[0m\u001b]8;;\u001b\\\u001b[2m:\u001b[0m\u001b]8;id=556935;file:///var/folders/0t/s0c95rbs6ds7w_b0d471p0kc0000gn/T/ipykernel_9731/4013596842.py#1\u001b\\\u001b[2m1\u001b[0m\u001b]8;;\u001b\\\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"console.log(\"Hey, here!\")"
]
},
{
"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: #00ff00; text-decoration-color: #00ff00\">────────────────────────────────────────────────────── </span><span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\"> Good</span><span style=\"color: #00ff00; text-decoration-color: #00ff00\"> ──────────────────────────────────────────────────────</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[92m────────────────────────────────────────────────────── \u001b[0m\u001b[1;31m Good\u001b[0m\u001b[92m ──────────────────────────────────────────────────────\u001b[0m\n"
]
},
"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": [
"👍\n"
]
},
"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": [
"👍\n"
]
},
"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; text-decoration-color: #00ff00\">────────────────────────────────────────────────────── </span><span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\"> Bad</span><span style=\"color: #00ff00; text-decoration-color: #00ff00\"> ───────────────────────────────────────────────────────</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[92m────────────────────────────────────────────────────── \u001b[0m\u001b[1;34m Bad\u001b[0m\u001b[92m ───────────────────────────────────────────────────────\u001b[0m\n"
]
},
"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": [
"👎\n"
]
},
"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": [
"👎\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"console.rule(\"[bold red] Good\")\n",
"console.print(\":+1:\")\n",
"console.print(\":+1:\")\n",
"console.rule(\"[bold blue] Bad\")\n",
"console.print(\":-1:\")\n",
"console.print(\":-1:\")"
]
},
{
"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: #00ff00; text-decoration-color: #00ff00\">─────────────────────────────────────────────────────────────────────────────────────────────────────────────</span> <span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\"> Good</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[92m─────────────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m \u001b[1;31m Good\u001b[0m\n"
]
},
"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": [
"👍\n"
]
},
"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": [
"👍\n"
]
},
"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\">Bad <span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">───────────────────────────────────────────────────────────────────────────────────────────────────────────────</span>\n",
"</pre>\n"
],
"text/plain": [
"Bad \u001b[1;34m───────────────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n"
]
},
"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": [
"👎\n"
]
},
"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": [
"👎\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"console.rule(\"[bold red] Good\", align=\"right\")\n",
"console.print(\":+1:\")\n",
"console.print(\":+1:\")\n",
"console.rule(\"Bad\", align=\"left\", style=\"bold blue\")\n",
"console.print(\":-1:\")\n",
"console.print(\":-1:\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"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; text-decoration-color: #00ff00\">──── </span>fold<span style=\"color: #00ff00; text-decoration-color: #00ff00\"> ────</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[92m──── \u001b[0mfold\u001b[92m ────\u001b[0m\n"
]
},
"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; text-decoration-color: #000080; font-weight: bold\">supercalifragi</span>\n",
"<span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">listicexpialid</span>\n",
"<span style=\"color: #000080; text-decoration-color: #000080; font-weight: bold\">ocious</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[1;34msupercalifragi\u001b[0m\n",
"\u001b[1;34mlisticexpialid\u001b[0m\n",
"\u001b[1;34mocious\u001b[0m\n"
]
},
"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": [
"\n"
]
},
"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; text-decoration-color: #00ff00\">──── </span>crop<span style=\"color: #00ff00; text-decoration-color: #00ff00\"> ────</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[92m──── \u001b[0mcrop\u001b[92m ────\u001b[0m\n"
]
},
"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; text-decoration-color: #000080; font-weight: bold\">supercalifragi</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[1;34msupercalifragi\u001b[0m\n"
]
},
"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": [
"\n"
]
},
"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; text-decoration-color: #00ff00\">── </span>ellipsis<span style=\"color: #00ff00; text-decoration-color: #00ff00\"> ──</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[92m── \u001b[0mellipsis\u001b[92m ──\u001b[0m\n"
]
},
"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; text-decoration-color: #000080; font-weight: bold\">supercalifrag…</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[1;34msupercalifrag…\u001b[0m\n"
]
},
"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": [
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from typing import List\n",
"from rich.console import Console, OverflowMethod\n",
"\n",
"console = Console(width=14)\n",
"supercali = \"supercalifragilisticexpialidocious\"\n",
"\n",
"overflow_methods: List[OverflowMethod] = [\"fold\", \"crop\", \"ellipsis\"]\n",
"for overflow in overflow_methods:\n",
" console.rule(overflow)\n",
" console.print(supercali, overflow=overflow, style=\"bold blue\")\n",
" console.print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 输出定制\n",
"### 文本对象"
]
},
{
"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: #d70000; text-decoration-color: #d70000\">Hello, world!</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[38;5;160mHello, world!\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich import print as rprint\n",
"from rich.text import Text\n",
"\n",
"rprint(Text(\"Hello, world!\", style=\"red3\"))"
]
},
{
"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\">Hello, <span style=\"color: #d70000; text-decoration-color: #d70000\">world!</span>\n",
"</pre>\n"
],
"text/plain": [
"Hello, \u001b[38;5;160mworld!\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich import print as rprint\n",
"from rich.text import Text\n",
"\n",
"text = Text()\n",
"text.append(\"Hello, \")\n",
"text.append(\"world!\", style=\"red3\")\n",
"rprint(text)"
]
},
{
"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\">Hello, <span style=\"color: #d70000; text-decoration-color: #d70000\">world!</span>\n",
"</pre>\n"
],
"text/plain": [
"Hello, \u001b[38;5;160mworld!\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich import print as rprint\n",
"from rich.text import Text\n",
"\n",
"text = Text(\"Hello, \")\n",
"highlight = Text(\"world!\", style=\"red3\")\n",
"text.append_text(highlight)\n",
"rprint(text)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"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\">Hello, <span style=\"color: #d70000; text-decoration-color: #d70000; font-weight: bold\">world!</span>\n",
"</pre>\n"
],
"text/plain": [
"Hello, \u001b[1;38;5;160mworld!\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich import print as rprint\n",
"from rich.text import Text\n",
"\n",
"text = Text.assemble(\"Hello, \", Text(\"world!\", style=\"bold red3\"))\n",
"rprint(text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 内容样式"
]
},
{
"cell_type": "code",
"execution_count": 16,
"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\">Hello, <span style=\"color: #d70000; text-decoration-color: #d70000; font-weight: bold\">world!</span>\n",
"</pre>\n"
],
"text/plain": [
"Hello, \u001b[1;38;5;160mworld!\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich import print as rprint\n",
"from rich.text import Text\n",
"from rich.style import Style\n",
"\n",
"style = Style(color=\"red3\", bold=True)\n",
"text = Text.assemble(\"Hello, \", Text(\"world!\", style=style))\n",
"rprint(text)"
]
},
{
"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\">Hello, <span style=\"color: #d70000; text-decoration-color: #d70000; font-weight: bold; text-decoration: underline\">world!</span>\n",
"</pre>\n"
],
"text/plain": [
"Hello, \u001b[1;4;38;5;160mworld!\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich import print as rprint\n",
"from rich.style import Style\n",
"from rich.text import Text\n",
"\n",
"bold_style = Style.parse(\"bold\")\n",
"underline_style = Style(underline=True)\n",
"content_style = bold_style + underline_style + Style(color=\"red3\")\n",
"\n",
"text = Text.assemble(\"Hello, \", Text(\"world!\", style=content_style))\n",
"rprint(text)"
]
},
{
"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\"><span style=\"color: #00ff00; text-decoration-color: #00ff00\">─────────────────────────────────── </span>emphasis<span style=\"color: #00ff00; text-decoration-color: #00ff00\"> ───────────────────────────────────</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[92m─────────────────────────────────── \u001b[0memphasis\u001b[92m ───────────────────────────────────\u001b[0m\n"
]
},
"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\">Hello, <span style=\"color: #d70000; text-decoration-color: #d70000; font-weight: bold\">world!</span>\n",
"</pre>\n"
],
"text/plain": [
"Hello, \u001b[1;38;5;160mworld!\u001b[0m\n"
]
},
"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; text-decoration-color: #00ff00\">───────────────────────────────────── </span>info<span style=\"color: #00ff00; text-decoration-color: #00ff00\"> ─────────────────────────────────────</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[92m───────────────────────────────────── \u001b[0minfo\u001b[92m ─────────────────────────────────────\u001b[0m\n"
]
},
"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: #7fbfbf; text-decoration-color: #7fbfbf; text-decoration: underline\">Testing</span><span style=\"color: #bfbf7f; text-decoration-color: #bfbf7f; text-decoration: underline\">...</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[2;4;36mTesting\u001b[0m\u001b[2;4;33m...\u001b[0m\n"
]
},
"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; text-decoration-color: #00ff00\">──────────────────────────────────── </span>error<span style=\"color: #00ff00; text-decoration-color: #00ff00\"> ─────────────────────────────────────</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[92m──────────────────────────────────── \u001b[0merror\u001b[92m ─────────────────────────────────────\u001b[0m\n"
]
},
"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; text-decoration-color: #c0c0c0; background-color: #000080\">Error!</span>\n",
"</pre>\n"
],
"text/plain": [
"\u001b[37;44mError!\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich.console import Console\n",
"from rich.style import Style\n",
"from rich.text import Text\n",
"from rich.theme import Theme\n",
"\n",
"custom_theme = Theme(\n",
" {\n",
" \"emphasis\": \"red3 bold\",\n",
" \"error\": Style(bgcolor=\"blue\", color=\"white\"),\n",
" \"info\": \"dim cyan underline\",\n",
" }\n",
")\n",
"\n",
"\n",
"console = Console(theme=custom_theme, width=80)\n",
"text = Text.assemble(\"Hello, \", Text(\"world!\", style=\"emphasis\"))\n",
"console.rule(title=\"emphasis\")\n",
"console.print(text)\n",
"console.rule(title=\"info\")\n",
"console.print(\"Testing...\", style=\"info\")\n",
"console.rule(title=\"error\")\n",
"console.print(\"Error!\", style=\"error\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 内容高亮"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"import faker\n",
"from rich.console import Console\n",
"from rich.highlighter import RegexHighlighter\n",
"from rich.theme import Theme\n",
"\n",
"fake = faker.Faker(locale=\"zh-CN\")\n",
"\n",
"\n",
"class TelephoneHighlighter(RegexHighlighter):\n",
" \"\"\"Apply style to anything that looks like an email.\"\"\"\n",
"\n",
" base_style = \"example-\"\n",
" highlights = [\n",
" r\"(?P<telephone>[+][86]{2}[-\\s]{0,1}1[3-9]\\d{9})\",\n",
" r\"(?P<telephone>1[3-9]\\d{9})\",\n",
" ]\n",
"\n",
"\n",
"theme = Theme({\"example-telephone\": \"bold green u\"})\n",
"console = Console(highlighter=TelephoneHighlighter(), theme=theme)"
]
},
{
"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\">Calling to <span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold; text-decoration: underline\">13240988221</span>\n",
"</pre>\n"
],
"text/plain": [
"Calling to \u001b[1;4;32m13240988221\u001b[0m\n"
]
},
"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\">Calling to <span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold; text-decoration: underline\">+86-13522318673</span>\n",
"</pre>\n"
],
"text/plain": [
"Calling to \u001b[1;4;32m+86-\u001b[0m\u001b[1;4;32m13522318673\u001b[0m\n"
]
},
"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\">Calling to <span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold; text-decoration: underline\">+86 15272607659</span>\n",
"</pre>\n"
],
"text/plain": [
"Calling to \u001b[1;4;32m+86 \u001b[0m\u001b[1;4;32m15272607659\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"console.print(f\"Calling to {fake.phone_number()}\")\n",
"console.print(f\"Calling to +86-{fake.phone_number()}\")\n",
"console.print(f\"Calling to +86 {fake.phone_number()}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 高级用法\n",
"### panel 面板"
]
},
{
"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\">╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮\n",
"│ Hello, <span style=\"text-decoration: underline\">100gle!</span> │\n",
"╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
"</pre>\n"
],
"text/plain": [
"╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮\n",
"│ Hello, \u001b[4m100gle!\u001b[0m │\n",
"╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich import print as rprint\n",
"from rich.panel import Panel\n",
"\n",
"panel = Panel(\"Hello, [u]100gle![/]\")\n",
"rprint(panel)"
]
},
{
"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\">╭───────────────────────────────────────────────╮\n",
"│ │\n",
"│ Lorem ipsum dolor sit amet, consectetuer adip │\n",
"│ iscing elit. Maecenas porttitor congue massa. │\n",
"│ Fusce posuere, magna sed pulvinar ultricies, │\n",
"│ purus lectus malesuada libero, sit amet comm │\n",
"│ odo magna eros quis urna. │\n",
"│ Nunc viverra imperdiet enim. Fusce est. Vivam │\n",
"│ us a tellus. │\n",
"│ Pellentesque habitant morbi tristique senectu │\n",
"│ s et netus et malesuada fames ac turpis egest │\n",
"│ as. Proin pharetra nonummy pede. Mauris et or │\n",
"│ ci. │\n",
"│ Aenean nec lorem. In porttitor. Donec laoreet │\n",
"│ nonummy augue. │\n",
"│ Suspendisse dui purus, scelerisque at, vulput │\n",
"│ ate vitae, pretium mattis, nunc. Mauris eget │\n",
"│ neque at sem venenatis eleifend. Ut nonummy. │\n",
"│ │\n",
"╰───────────────────────────────────────────────╯\n",
"</pre>\n"
],
"text/plain": [
"╭───────────────────────────────────────────────╮\n",
"│ │\n",
"│ Lorem ipsum dolor sit amet, consectetuer adip │\n",
"│ iscing elit. Maecenas porttitor congue massa. │\n",
"│ Fusce posuere, magna sed pulvinar ultricies, │\n",
"│ purus lectus malesuada libero, sit amet comm │\n",
"│ odo magna eros quis urna. │\n",
"│ Nunc viverra imperdiet enim. Fusce est. Vivam │\n",
"│ us a tellus. │\n",
"│ Pellentesque habitant morbi tristique senectu │\n",
"│ s et netus et malesuada fames ac turpis egest │\n",
"│ as. Proin pharetra nonummy pede. Mauris et or │\n",
"│ ci. │\n",
"│ Aenean nec lorem. In porttitor. Donec laoreet │\n",
"│ nonummy augue. │\n",
"│ Suspendisse dui purus, scelerisque at, vulput │\n",
"│ ate vitae, pretium mattis, nunc. Mauris eget │\n",
"│ neque at sem venenatis eleifend. Ut nonummy. │\n",
"│ │\n",
"╰───────────────────────────────────────────────╯\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"lorem = \"\"\"\n",
"Lorem ipsum dolor sit amet, consectetuer adip\n",
"iscing elit. Maecenas porttitor congue massa.\n",
" Fusce posuere, magna sed pulvinar ultricies,\n",
" purus lectus malesuada libero, sit amet comm\n",
"odo magna eros quis urna.\n",
"Nunc viverra imperdiet enim. Fusce est. Vivam\n",
"us a tellus.\n",
"Pellentesque habitant morbi tristique senectu\n",
"s et netus et malesuada fames ac turpis egest\n",
"as. Proin pharetra nonummy pede. Mauris et or\n",
"ci.\n",
"Aenean nec lorem. In porttitor. Donec laoreet\n",
" nonummy augue.\n",
"Suspendisse dui purus, scelerisque at, vulput\n",
"ate vitae, pretium mattis, nunc. Mauris eget\n",
"neque at sem venenatis eleifend. Ut nonummy.\n",
"\"\"\"\n",
"\n",
"rprint(Panel.fit(lorem))"
]
},
{
"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\">╭──────────────────── lorem ────────────────────╮\n",
"│ │\n",
"│ Lorem ipsum dolor sit amet, consectetuer adip │\n",
"│ iscing elit. Maecenas porttitor congue massa. │\n",
"│ Fusce posuere, magna sed pulvinar ultricies, │\n",
"│ purus lectus malesuada libero, sit amet comm │\n",
"│ odo magna eros quis urna. │\n",
"│ Nunc viverra imperdiet enim. Fusce est. Vivam │\n",
"│ us a tellus. │\n",
"│ Pellentesque habitant morbi tristique senectu │\n",
"│ s et netus et malesuada fames ac turpis egest │\n",
"│ as. Proin pharetra nonummy pede. Mauris et or │\n",
"│ ci. │\n",
"│ Aenean nec lorem. In porttitor. Donec laoreet │\n",
"│ nonummy augue. │\n",
"│ Suspendisse dui purus, scelerisque at, vulput │\n",
"│ ate vitae, pretium mattis, nunc. Mauris eget │\n",
"│ neque at sem venenatis eleifend. Ut nonummy. │\n",
"│ │\n",
"╰───────────────────────────────────────────────╯\n",
"</pre>\n"
],
"text/plain": [
"╭──────────────────── lorem ────────────────────╮\n",
"│ │\n",
"│ Lorem ipsum dolor sit amet, consectetuer adip │\n",
"│ iscing elit. Maecenas porttitor congue massa. │\n",
"│ Fusce posuere, magna sed pulvinar ultricies, │\n",
"│ purus lectus malesuada libero, sit amet comm │\n",
"│ odo magna eros quis urna. │\n",
"│ Nunc viverra imperdiet enim. Fusce est. Vivam │\n",
"│ us a tellus. │\n",
"│ Pellentesque habitant morbi tristique senectu │\n",
"│ s et netus et malesuada fames ac turpis egest │\n",
"│ as. Proin pharetra nonummy pede. Mauris et or │\n",
"│ ci. │\n",
"│ Aenean nec lorem. In porttitor. Donec laoreet │\n",
"│ nonummy augue. │\n",
"│ Suspendisse dui purus, scelerisque at, vulput │\n",
"│ ate vitae, pretium mattis, nunc. Mauris eget │\n",
"│ neque at sem venenatis eleifend. Ut nonummy. │\n",
"│ │\n",
"╰───────────────────────────────────────────────╯\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"rprint(Panel.fit(lorem, title=\"lorem\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Group 成组渲染"
]
},
{
"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\">╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮\n",
"│ ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │\n",
"│ │ Hello │ │\n",
"│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
"│ ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │\n",
"│ │ <span style=\"text-decoration: underline\">100gle!</span> │ │\n",
"│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
"╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
"</pre>\n"
],
"text/plain": [
"╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮\n",
"│ ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │\n",
"│ │ Hello │ │\n",
"│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
"│ ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │\n",
"│ │ \u001b[4m100gle!\u001b[0m │ │\n",
"│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
"╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich import print as rprint\n",
"from rich.console import Group\n",
"from rich.panel import Panel\n",
"\n",
"panel1 = Panel(\"Hello\")\n",
"panel2 = Panel(\"[u]100gle![/]\")\n",
"group = Group(panel1, panel2)\n",
"\n",
"rprint(Panel(group))"
]
},
{
"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\">╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮\n",
"│ ╭───────────────────────────────────────────────── Section 1 ─────────────────────────────────────────────────╮ │\n",
"│ │ Hello │ │\n",
"│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
"│ ╭───────────────────────────────────────────────── Section 2 ─────────────────────────────────────────────────╮ │\n",
"│ │ <span style=\"text-decoration: underline\">100gle</span> │ │\n",
"│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
"╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
"</pre>\n"
],
"text/plain": [
"╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮\n",
"│ ╭───────────────────────────────────────────────── Section 1 ─────────────────────────────────────────────────╮ │\n",
"│ │ Hello │ │\n",
"│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
"│ ╭───────────────────────────────────────────────── Section 2 ─────────────────────────────────────────────────╮ │\n",
"│ │ \u001b[4m100gle\u001b[0m │ │\n",
"│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
"╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from rich.console import group\n",
"\n",
"@group()\n",
"def panels():\n",
" yield Panel(\"Hello\", title=\"Section 1\")\n",
" yield Panel(\"[u]100gle[/]\", title=\"Section 2\")\n",
"\n",
"\n",
"rprint(Panel(panels()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 进度条"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "9096ec3933b64feda5a334b7b47aeb3a",
"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": []
},
"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": [
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import time\n",
"from rich.progress import track\n",
"\n",
"for i in track(range(10), description=\"Status: \"):\n",
" time.sleep(0.1) # do something here."
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2961fd33ddfd4ae29f409f50c7097258",
"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": []
},
"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": [
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import time\n",
"\n",
"from rich.progress import Progress\n",
"\n",
"status = Progress(expand=True)\n",
"\n",
"with status:\n",
" task = status.add_task(\"downloading...\", total=10)\n",
" while not status.finished:\n",
" status.update(task, advance=1)\n",
" time.sleep(0.2) # do something here."
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "eaec928d01a64b41b67c02d3442e0cc0",
"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": []
},
"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": [
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import pathlib\n",
"\n",
"from rich.progress import open as ropen\n",
"\n",
"fp = pathlib.Path(\"./foo.txt\")\n",
"content = \"\\n\".join([\"Hello, World!\" for _ in range(10)])\n",
"fp.write_text(content)\n",
"\n",
"with ropen(fp, \"r\") as file:\n",
" for line in file:\n",
" data = file.readline()\n",
"\n",
"fp.unlink()"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "9bc4e8ddd3a54c659aa4947dab8be211",
"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": []
},
"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": [
"\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import pathlib\n",
"import time\n",
"\n",
"from rich.progress import wrap_file\n",
"\n",
"fp = pathlib.Path(\"./foo.txt\")\n",
"content = \"\\n\".join([\"Hello, World!\" for _ in range(10)])\n",
"fp.write_text(content)\n",
"\n",
"\n",
"with open(fp, \"r\", newline=\"\\n\") as io:\n",
" with wrap_file(io, total=len(content)) as file:\n",
" for row, line in enumerate(file):\n",
" time.sleep(0.2) # do something here.\n",
"\n",
"fp.unlink()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 数据表格"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"import faker\n",
"from rich.console import Console\n",
"from rich.table import Table\n",
"\n",
"fake = faker.Faker(locale=\"zh-CN\")\n",
"\n",
"table = Table(title=\"User Accounts\")\n",
"\n",
"table.add_column(\"IP\", justify=\"right\", style=\"cyan\", no_wrap=True)\n",
"table.add_column(\"Name\", style=\"blue\")\n",
"table.add_column(\"Email\", justify=\"left\", style=\"green\")\n",
"table.add_column(\"Account\", justify=\"right\", style=\"#812303\")\n",
"\n",
"for _ in range(3):\n",
" table.add_row(\n",
" f\"{fake.ipv4()}\", f\"{fake.name()}\", f\"{fake.email()}\", f\"{fake.pricetag()}\"\n",
" )"
]
},
{
"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=\"font-style: italic\"> User Accounts </span>\n",
"┏━━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓\n",
"┃<span style=\"font-weight: bold\"> IP </span>┃<span style=\"font-weight: bold\"> Name </span>┃<span style=\"font-weight: bold\"> Email </span>┃<span style=\"font-weight: bold\"> Account </span>┃\n",
"┡━━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩\n",
"│<span style=\"color: #008080; text-decoration-color: #008080\"> 42.95.52.205 </span>│<span style=\"color: #000080; text-decoration-color: #000080\"> 虞丹丹 </span>│<span style=\"color: #008000; text-decoration-color: #008000\"> yong55@example.net </span>│<span style=\"color: #812303; text-decoration-color: #812303\"> $21.02 </span>│\n",
"│<span style=\"color: #008080; text-decoration-color: #008080\"> 121.174.78.35 </span>│<span style=\"color: #000080; text-decoration-color: #000080\"> 杨玉兰 </span>│<span style=\"color: #008000; text-decoration-color: #008000\"> guiying89@example.org </span>│<span style=\"color: #812303; text-decoration-color: #812303\"> $1,120.73 </span>│\n",
"│<span style=\"color: #008080; text-decoration-color: #008080\"> 124.12.117.91 </span>│<span style=\"color: #000080; text-decoration-color: #000080\"> 杨桂芝 </span>│<span style=\"color: #008000; text-decoration-color: #008000\"> txia@example.org </span>│<span style=\"color: #812303; text-decoration-color: #812303\"> $457.21 </span>│\n",
"└───────────────┴────────┴───────────────────────┴───────────┘\n",
"</pre>\n"
],
"text/plain": [
"\u001b[3m User Accounts \u001b[0m\n",
"┏━━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓\n",
"┃\u001b[1m \u001b[0m\u001b[1m IP\u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mName \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mEmail \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1m Account\u001b[0m\u001b[1m \u001b[0m┃\n",
"┡━━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩\n",
"│\u001b[36m \u001b[0m\u001b[36m 42.95.52.205\u001b[0m\u001b[36m \u001b[0m│\u001b[34m \u001b[0m\u001b[34m虞丹丹\u001b[0m\u001b[34m \u001b[0m│\u001b[32m \u001b[0m\u001b[32myong55@example.net \u001b[0m\u001b[32m \u001b[0m│\u001b[38;2;129;35;3m \u001b[0m\u001b[38;2;129;35;3m $21.02\u001b[0m\u001b[38;2;129;35;3m \u001b[0m│\n",
"│\u001b[36m \u001b[0m\u001b[36m121.174.78.35\u001b[0m\u001b[36m \u001b[0m│\u001b[34m \u001b[0m\u001b[34m杨玉兰\u001b[0m\u001b[34m \u001b[0m│\u001b[32m \u001b[0m\u001b[32mguiying89@example.org\u001b[0m\u001b[32m \u001b[0m│\u001b[38;2;129;35;3m \u001b[0m\u001b[38;2;129;35;3m$1,120.73\u001b[0m\u001b[38;2;129;35;3m \u001b[0m│\n",
"│\u001b[36m \u001b[0m\u001b[36m124.12.117.91\u001b[0m\u001b[36m \u001b[0m│\u001b[34m \u001b[0m\u001b[34m杨桂芝\u001b[0m\u001b[34m \u001b[0m│\u001b[32m \u001b[0m\u001b[32mtxia@example.org \u001b[0m\u001b[32m \u001b[0m│\u001b[38;2;129;35;3m \u001b[0m\u001b[38;2;129;35;3m $457.21\u001b[0m\u001b[38;2;129;35;3m \u001b[0m│\n",
"└───────────────┴────────┴───────────────────────┴───────────┘\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"console = Console()\n",
"console.print(table)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"import faker\n",
"from rich.console import Console\n",
"from rich.table import Column, Table\n",
"\n",
"fake = faker.Faker(locale=\"zh-CN\")\n",
"\n",
"table = Table(\n",
" Column(\"IP\", justify=\"right\", style=\"cyan\", no_wrap=True),\n",
" Column(\"Name\", style=\"blue\"),\n",
" Column(\"Email\", justify=\"left\", style=\"green\"),\n",
" Column(\"Account\", justify=\"right\", style=\"#812303\"),\n",
" title=\"User Accounts\",\n",
")\n",
"\n",
"for _ in range(3):\n",
" table.add_row(\n",
" f\"{fake.ipv4()}\", f\"{fake.name()}\", f\"{fake.email()}\", f\"{fake.pricetag()}\"\n",
" )"
]
},
{
"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=\"font-style: italic\"> User Accounts </span>\n",
"┏━━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┓\n",
"┃<span style=\"font-weight: bold\"> IP </span>┃<span style=\"font-weight: bold\"> Name </span>┃<span style=\"font-weight: bold\"> Email </span>┃<span style=\"font-weight: bold\"> Account </span>┃\n",
"┡━━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━┩\n",
"│<span style=\"color: #008080; text-decoration-color: #008080\"> 38.74.25.133 </span>│<span style=\"color: #000080; text-decoration-color: #000080\"> 刘秀华 </span>│<span style=\"color: #008000; text-decoration-color: #008000\"> mintian@example.com </span>│<span style=\"color: #812303; text-decoration-color: #812303\"> $66.37 </span>│\n",
"│<span style=\"color: #008080; text-decoration-color: #008080\"> 204.57.139.78 </span>│<span style=\"color: #000080; text-decoration-color: #000080\"> 张红霞 </span>│<span style=\"color: #008000; text-decoration-color: #008000\"> iyin@example.com </span>│<span style=\"color: #812303; text-decoration-color: #812303\"> $2.27 </span>│\n",
"│<span style=\"color: #008080; text-decoration-color: #008080\"> 188.65.166.52 </span>│<span style=\"color: #000080; text-decoration-color: #000080\"> 罗玲 </span>│<span style=\"color: #008000; text-decoration-color: #008000\"> taohan@example.com </span>│<span style=\"color: #812303; text-decoration-color: #812303\"> $255.06 </span>│\n",
"└───────────────┴────────┴─────────────────────┴─────────┘\n",
"</pre>\n"
],
"text/plain": [
"\u001b[3m User Accounts \u001b[0m\n",
"┏━━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┓\n",
"┃\u001b[1m \u001b[0m\u001b[1m IP\u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mName \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mEmail \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mAccount\u001b[0m\u001b[1m \u001b[0m┃\n",
"┡━━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━┩\n",
"│\u001b[36m \u001b[0m\u001b[36m 38.74.25.133\u001b[0m\u001b[36m \u001b[0m│\u001b[34m \u001b[0m\u001b[34m刘秀华\u001b[0m\u001b[34m \u001b[0m│\u001b[32m \u001b[0m\u001b[32mmintian@example.com\u001b[0m\u001b[32m \u001b[0m│\u001b[38;2;129;35;3m \u001b[0m\u001b[38;2;129;35;3m $66.37\u001b[0m\u001b[38;2;129;35;3m \u001b[0m│\n",
"│\u001b[36m \u001b[0m\u001b[36m204.57.139.78\u001b[0m\u001b[36m \u001b[0m│\u001b[34m \u001b[0m\u001b[34m张红霞\u001b[0m\u001b[34m \u001b[0m│\u001b[32m \u001b[0m\u001b[32miyin@example.com \u001b[0m\u001b[32m \u001b[0m│\u001b[38;2;129;35;3m \u001b[0m\u001b[38;2;129;35;3m $2.27\u001b[0m\u001b[38;2;129;35;3m \u001b[0m│\n",
"│\u001b[36m \u001b[0m\u001b[36m188.65.166.52\u001b[0m\u001b[36m \u001b[0m│\u001b[34m \u001b[0m\u001b[34m罗玲 \u001b[0m\u001b[34m \u001b[0m│\u001b[32m \u001b[0m\u001b[32mtaohan@example.com \u001b[0m\u001b[32m \u001b[0m│\u001b[38;2;129;35;3m \u001b[0m\u001b[38;2;129;35;3m$255.06\u001b[0m\u001b[38;2;129;35;3m \u001b[0m│\n",
"└───────────────┴────────┴─────────────────────┴─────────┘\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"console = Console()\n",
"console.print(table)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"# mock data \n",
"import random\n",
"import time\n",
"\n",
"import faker\n",
"from faker.providers import DynamicProvider\n",
"from rich.live import Live\n",
"from rich.table import Column, Table\n",
"\n",
"random.seed(2333)\n",
"fake = faker.Faker(locale=\"zh-CN\")\n",
"ticker = DynamicProvider(\n",
" provider_name=\"ticker\", elements=[\"AAPL\", \"TSLA\", \"GOOG\", \"MSFT\"]\n",
")\n",
"fake.add_provider(ticker)\n",
"\n",
"\n",
"def mock_ticker_table():\n",
"\n",
" table = Table(\n",
" Column(\"Symbol\", justify=\"center\"),\n",
" Column(\"Last Price\", style=\"green\"),\n",
" Column(\"% Change\", justify=\"left\"),\n",
" title=\"Live Ticker Data\",\n",
" )\n",
"\n",
" for _ in range(random.randint(5, 10)):\n",
" value = random.uniform(10, 200)\n",
" pct_value = round(value, 3)\n",
"\n",
" number = random.uniform(-0.5, 0.5)\n",
" pct_number = round(number * 100, 2)\n",
"\n",
" change = (\n",
" f\":green_circle: [green]{pct_number}%[/]\"\n",
" if number > 0\n",
" else f\":red_triangle_pointed_down:[red]{pct_number}%[/]\"\n",
" )\n",
" table.add_row(f\"{fake.ticker()}\", f\"${pct_value}\", f\"{change}\")\n",
"\n",
" return table"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "41aa4daede2b49b9982a2284a41af4cc",
"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": []
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"with Live(mock_ticker_table(), refresh_per_second=4) as live:\n",
" for _ in range(10):\n",
" time.sleep(0.4)\n",
" live.update(mock_ticker_table())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Layout 布局"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"from rich.layout import Layout\n",
"\n",
"# layout\n",
"layout = Layout(name=\"container\")\n",
"layout.split(\n",
" Layout(name=\"header\", size=3),\n",
" Layout(name=\"main\"),\n",
" Layout(name=\"footer\", size=3),\n",
")\n",
"\n",
"layout[\"main\"].split_row(Layout(name=\"aside\"), Layout(name=\"content\"))"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"from rich import box\n",
"from rich.console import Console\n",
"from rich.markdown import Markdown\n",
"from rich.panel import Panel\n",
"from rich.text import Text\n",
"from rich.tree import Tree\n",
"\n",
"# header part\n",
"header = Panel(\n",
" Text(\n",
" \"Rich Layout example\",\n",
" justify=\"center\",\n",
" no_wrap=True,\n",
" style=\"white on blue\",\n",
" )\n",
")\n",
"# aside part\n",
"aside = Tree(\"Table of Contents\")\n",
"chp1 = aside.add(\"Chapter 1\")\n",
"chp2 = aside.add(\"Chapter 2\")\n",
"for chp in [chp1, chp2]:\n",
" chp.add(\"Section 1\")\n",
" chp.add(\"Section 2\")\n",
"\n",
"\n",
"# content part\n",
"content = \"\"\"\n",
"# Chapter 1\n",
"Hello, world!\n",
"\n",
"## Section 1\n",
"\n",
"TODO:\n",
"\n",
"- [ ] List\n",
"- [ ] Tasks\n",
"- [x] Here.\n",
"\n",
"## Section 2\n",
"\n",
" ```python\n",
" from functools import lru_cache\n",
"\n",
"\n",
" @lru_cache(maxsize=128)\n",
" def fibonacci(n: int) -> int:\n",
" if n <= 1:\n",
" return n\n",
" return fibonacci(n - 1) + fibonacci(n - 2)\n",
" ```\n",
"\"\"\"\n",
"\n",
"markdown = Markdown(content)\n",
"\n",
"footer = Panel(\n",
" Text(\"Contact me: \", justify=\"center\").append(\"100gle\", style=\"u green\"),\n",
" box=box.DOUBLE_EDGE,\n",
")"
]
},
{
"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\">╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮\n",
"│ ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │\n",
"│ │ <span style=\"color: #c0c0c0; text-decoration-color: #c0c0c0; background-color: #000080\"> Rich Layout example </span> │ │\n",
"│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">╭─────────────────────────────────────────────────────╮</span><span style=\"color: #800000; text-decoration-color: #800000\">╭──────────────────────────────────────────────────────╮</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> Table of Contents <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> ╔══════════════════════════════════════════════════╗ <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> ├── Chapter 1 <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> ║ <span style=\"font-weight: bold\">Chapter 1</span> ║ <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> │ ├── Section 1 <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> ╚══════════════════════════════════════════════════╝ <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> │ └── Section 2 <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> └── Chapter 2 <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> Hello, world! <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> ├── Section 1 <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> └── Section 2 <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"font-weight: bold; text-decoration: underline\">Section 1</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> TODO: <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\"> • </span>[ ] List <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\"> • </span>[ ] Tasks <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\"> • </span>[x] Here. <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"font-weight: bold; text-decoration: underline\">Section 2</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822\">```python</span><span style=\"background-color: #272822\"> </span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822\">from functools import lru_cache</span><span style=\"background-color: #272822\"> </span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"background-color: #272822\"> </span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"background-color: #272822\"> </span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822\">@lru_cache(maxsize=128)</span><span style=\"background-color: #272822\"> </span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822\">def fibonacci(n: int) -&gt; int:</span><span style=\"background-color: #272822\"> </span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822\"> if n &lt;= 1:</span><span style=\"background-color: #272822\"> </span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822\"> return n</span><span style=\"background-color: #272822\"> </span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822\"> return fibonacci(n - 1) + fibonacci(n - 2)</span><span style=\"background-color: #272822\"> </span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #f8f8f2; text-decoration-color: #f8f8f2; background-color: #272822\">```</span><span style=\"background-color: #272822\"> </span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">│</span> <span style=\"color: #008080; text-decoration-color: #008080\">│</span><span style=\"color: #800000; text-decoration-color: #800000\">│</span> <span style=\"color: #800000; text-decoration-color: #800000\">│</span> │\n",
"│ <span style=\"color: #008080; text-decoration-color: #008080\">╰─────────────────────────────────────────────────────╯</span><span style=\"color: #800000; text-decoration-color: #800000\">╰──────────────────────────────────────────────────────╯</span> │\n",
"│ ╔═════════════════════════════════════════════════════════════════════════════════════════════════════════════╗ │\n",
"│ ║ Contact me: <span style=\"color: #008000; text-decoration-color: #008000; text-decoration: underline\">100gle</span> ║ │\n",
"│ ╚═════════════════════════════════════════════════════════════════════════════════════════════════════════════╝ │\n",
"╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
"</pre>\n"
],
"text/plain": [
"╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮\n",
"│ ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │\n",
"│ │ \u001b[37;44m Rich Layout example \u001b[0m │ │\n",
"│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
"│ \u001b[36m╭─────────────────────────────────────────────────────╮\u001b[0m\u001b[31m╭──────────────────────────────────────────────────────╮\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m Table of Contents \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m ╔══════════════════════════════════════════════════╗ \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m ├── Chapter 1 \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m ║ \u001b[1mChapter 1\u001b[0m ║ \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m │ ├── Section 1 \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m ╚══════════════════════════════════════════════════╝ \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m │ └── Section 2 \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m └── Chapter 2 \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m Hello, world! \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m ├── Section 1 \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m └── Section 2 \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[1;4mSection 1\u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m TODO: \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[1;33m • \u001b[0m[ ] List \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[1;33m • \u001b[0m[ ] Tasks \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[1;33m • \u001b[0m[x] Here. \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[1;4mSection 2\u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[38;2;248;248;242;48;2;39;40;34m```python\u001b[0m\u001b[48;2;39;40;34m \u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[38;2;248;248;242;48;2;39;40;34mfrom functools import lru_cache\u001b[0m\u001b[48;2;39;40;34m \u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[48;2;39;40;34m \u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[48;2;39;40;34m \u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[38;2;248;248;242;48;2;39;40;34m@lru_cache(maxsize=128)\u001b[0m\u001b[48;2;39;40;34m \u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[38;2;248;248;242;48;2;39;40;34mdef fibonacci(n: int) -> int:\u001b[0m\u001b[48;2;39;40;34m \u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[38;2;248;248;242;48;2;39;40;34m if n <= 1:\u001b[0m\u001b[48;2;39;40;34m \u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[38;2;248;248;242;48;2;39;40;34m return n\u001b[0m\u001b[48;2;39;40;34m \u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[38;2;248;248;242;48;2;39;40;34m return fibonacci(n - 1) + fibonacci(n - 2)\u001b[0m\u001b[48;2;39;40;34m \u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[38;2;248;248;242;48;2;39;40;34m```\u001b[0m\u001b[48;2;39;40;34m \u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m│\u001b[0m \u001b[36m│\u001b[0m\u001b[31m│\u001b[0m \u001b[31m│\u001b[0m │\n",
"│ \u001b[36m╰─────────────────────────────────────────────────────╯\u001b[0m\u001b[31m╰──────────────────────────────────────────────────────╯\u001b[0m │\n",
"│ ╔═════════════════════════════════════════════════════════════════════════════════════════════════════════════╗ │\n",
"│ ║ Contact me: \u001b[4;32m100gle\u001b[0m ║ │\n",
"│ ╚═════════════════════════════════════════════════════════════════════════════════════════════════════════════╝ │\n",
"╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"layout[\"header\"].update(header)\n",
"layout[\"aside\"].update(Panel(aside, border_style=\"cyan\"))\n",
"layout[\"content\"].update(Panel(markdown, border_style=\"red\"))\n",
"layout[\"footer\"].update(footer)\n",
"\n",
"console = Console()\n",
"console.print(Panel(layout, height=40))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.0 ('pandas-startup')",
"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.9.0"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "13977d4cc82dee5f9d9535ceb495bd0ab12a43c33c664e5f0d53c24cf634b67f"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}