{ "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": [ "
Hello, 100gle!\n",
"\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": [
"[1, 2, 3, 4, 5]\n", "\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": [ "
{\n", "│ '__name__': '__main__',\n", "│ '__doc__': 'Automatically created module for IPython interactive environment',\n", "│ '__package__': None,\n", "│ '__loader__': None,\n", "│ '__spec__': None,\n", "│ '__builtin__': <module 'builtins' (built-in)>,\n", "│ '__builtins__': <module 'builtins' (built-in)>,\n", "│ '_ih': [\n", "│ │ '',\n", "│ │ 'print(\"Hello, [bold red]100gle[/bold red]!\")\\n\\nfrom rich import print as rprint\\nrprint(\"Hello, [bold red]100gle[/bold red]!\")',\n", "│ │ 'from rich.pretty import pprint\\nnumbers = [1, 2, 3, 4, 5]\\npprint(numbers)\\npprint(locals())'\n", "│ ],\n", "│ '_oh': {},\n", "│ '_dh': [PosixPath('/Users/Bobot/Repositories/Python-Courses/pandas-startup/code/newsletters/N14')],\n", "│ 'In': [\n", "│ │ '',\n", "│ │ 'print(\"Hello, [bold red]100gle[/bold red]!\")\\n\\nfrom rich import print as rprint\\nrprint(\"Hello, [bold red]100gle[/bold red]!\")',\n", "│ │ 'from rich.pretty import pprint\\nnumbers = [1, 2, 3, 4, 5]\\npprint(numbers)\\npprint(locals())'\n", "│ ],\n", "│ 'Out': {},\n", "│ 'get_ipython': <bound method InteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x7f88a8732190>>,\n", "│ 'exit': <IPython.core.autocall.ZMQExitAutocall object at 0x7f88d838d280>,\n", "│ 'quit': <IPython.core.autocall.ZMQExitAutocall object at 0x7f88d838d280>,\n", "│ '_': '',\n", "│ '__': '',\n", "│ '___': '',\n", "│ 'sys': <module 'sys' (built-in)>,\n", "│ 'os': <module 'os' from '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py'>,\n", "│ '__vsc_ipynb_file__': '/Users/Bobot/Repositories/Python-Courses/pandas-startup/code/newsletters/N14/rich.ipynb',\n", "│ '_i': 'print(\"Hello, [bold red]100gle[/bold red]!\")\\n\\nfrom rich import print as rprint\\nrprint(\"Hello, [bold red]100gle[/bold red]!\")',\n", "│ '_ii': '',\n", "│ '_iii': '',\n", "│ '_i1': 'print(\"Hello, [bold red]100gle[/bold red]!\")\\n\\nfrom rich import print as rprint\\nrprint(\"Hello, [bold red]100gle[/bold red]!\")',\n", "│ 'rprint': <function print at 0x7f88987e3a60>,\n", "│ '_i2': 'from rich.pretty import pprint\\nnumbers = [1, 2, 3, 4, 5]\\npprint(numbers)\\npprint(locals())',\n", "│ 'pprint': <function pprint at 0x7f88d8816e50>,\n", "│ 'numbers': [1, 2, 3, 4, 5]\n", "}\n", "\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
Hello, 100gle!\n",
"\n"
],
"text/plain": [
"Hello, \u001b[1m100gle\u001b[0m!\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"Hello, 100gle!\n",
"\n"
],
"text/plain": [
"Hello, \u001b[3m100gle\u001b[0m!\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"Hello, 100gle\n",
"\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": [
"Hello, 100gle!\n",
"\n"
],
"text/plain": [
"Hello, \u001b[1m100gle\u001b[0m!\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"Hello, 100gle!\n",
"\n"
],
"text/plain": [
"Hello, \u001b[3m100gle\u001b[0m!\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"Hello, 100gle\n",
"\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": [
"[info] Hello, world!\n", "\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": [ "
Hello, SSPAI! 🤪\n",
"\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": [
"Hello, SSPAI!\n",
"\n"
],
"text/plain": [
"Hello, \u001b[1mSSPAI\u001b[0m!\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"👍\n",
"\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": [
"[11:17:10] Hey, here! 4013596842.py:1\n", "\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": [ "
────────────────────────────────────────────────────── Good ──────────────────────────────────────────────────────\n", "\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": [ "
👍\n",
"\n"
],
"text/plain": [
"👍\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"👍\n",
"\n"
],
"text/plain": [
"👍\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"────────────────────────────────────────────────────── Bad ───────────────────────────────────────────────────────\n", "\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": [ "
👎\n",
"\n"
],
"text/plain": [
"👎\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"👎\n",
"\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": [
"───────────────────────────────────────────────────────────────────────────────────────────────────────────── Good\n", "\n" ], "text/plain": [ "\u001b[92m─────────────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m \u001b[1;31m Good\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
👍\n",
"\n"
],
"text/plain": [
"👍\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"👍\n",
"\n"
],
"text/plain": [
"👍\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"Bad ───────────────────────────────────────────────────────────────────────────────────────────────────────────────\n",
"\n"
],
"text/plain": [
"Bad \u001b[1;34m───────────────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"👎\n",
"\n"
],
"text/plain": [
"👎\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"👎\n",
"\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": [
"──── fold ────\n", "\n" ], "text/plain": [ "\u001b[92m──── \u001b[0mfold\u001b[92m ────\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
supercalifragi\n", "listicexpialid\n", "ocious\n", "\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": [ "
\n",
"\n"
],
"text/plain": [
"\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"──── crop ────\n", "\n" ], "text/plain": [ "\u001b[92m──── \u001b[0mcrop\u001b[92m ────\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
supercalifragi\n",
"\n"
],
"text/plain": [
"\u001b[1;34msupercalifragi\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
"\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"── ellipsis ──\n", "\n" ], "text/plain": [ "\u001b[92m── \u001b[0mellipsis\u001b[92m ──\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
supercalifrag…\n",
"\n"
],
"text/plain": [
"\u001b[1;34msupercalifrag…\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\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": [
"Hello, world!\n",
"\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": [
"Hello, world!\n",
"\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": [
"Hello, world!\n",
"\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": [
"Hello, world!\n",
"\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": [
"Hello, world!\n",
"\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": [
"Hello, world!\n",
"\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": [
"─────────────────────────────────── emphasis ───────────────────────────────────\n", "\n" ], "text/plain": [ "\u001b[92m─────────────────────────────────── \u001b[0memphasis\u001b[92m ───────────────────────────────────\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Hello, world!\n",
"\n"
],
"text/plain": [
"Hello, \u001b[1;38;5;160mworld!\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"───────────────────────────────────── info ─────────────────────────────────────\n", "\n" ], "text/plain": [ "\u001b[92m───────────────────────────────────── \u001b[0minfo\u001b[92m ─────────────────────────────────────\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Testing...\n", "\n" ], "text/plain": [ "\u001b[2;4;36mTesting\u001b[0m\u001b[2;4;33m...\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
──────────────────────────────────── error ─────────────────────────────────────\n", "\n" ], "text/plain": [ "\u001b[92m──────────────────────────────────── \u001b[0merror\u001b[92m ─────────────────────────────────────\u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Error!\n",
"\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\"(?PCalling to 13240988221\n",
"\n"
],
"text/plain": [
"Calling to \u001b[1;4;32m13240988221\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"Calling to +86-13522318673\n",
"\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": [
"Calling to +86 15272607659\n",
"\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": [
"╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮\n",
"│ Hello, 100gle! │\n",
"╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
"\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": [
"╭───────────────────────────────────────────────╮\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",
"\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": [
"╭──────────────────── 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",
"\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": [
"╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮\n",
"│ ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │\n",
"│ │ Hello │ │\n",
"│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
"│ ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │\n",
"│ │ 100gle! │ │\n",
"│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
"╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
"\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": [
"╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮\n",
"│ ╭───────────────────────────────────────────────── Section 1 ─────────────────────────────────────────────────╮ │\n",
"│ │ Hello │ │\n",
"│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
"│ ╭───────────────────────────────────────────────── Section 2 ─────────────────────────────────────────────────╮ │\n",
"│ │ 100gle │ │\n",
"│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
"╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
"\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": [
"\n"
],
"text/plain": []
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\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": [
"\n"
],
"text/plain": []
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\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": [
"\n"
],
"text/plain": []
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\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": [
"\n"
],
"text/plain": []
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"\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": [
"User Accounts \n", "┏━━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓\n", "┃ IP ┃ Name ┃ Email ┃ Account ┃\n", "┡━━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩\n", "│ 42.95.52.205 │ 虞丹丹 │ yong55@example.net │ $21.02 │\n", "│ 121.174.78.35 │ 杨玉兰 │ guiying89@example.org │ $1,120.73 │\n", "│ 124.12.117.91 │ 杨桂芝 │ txia@example.org │ $457.21 │\n", "└───────────────┴────────┴───────────────────────┴───────────┘\n", "\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": [ "
User Accounts \n", "┏━━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┓\n", "┃ IP ┃ Name ┃ Email ┃ Account ┃\n", "┡━━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━┩\n", "│ 38.74.25.133 │ 刘秀华 │ mintian@example.com │ $66.37 │\n", "│ 204.57.139.78 │ 张红霞 │ iyin@example.com │ $2.27 │\n", "│ 188.65.166.52 │ 罗玲 │ taohan@example.com │ $255.06 │\n", "└───────────────┴────────┴─────────────────────┴─────────┘\n", "\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": [ "\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": [ "
╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮\n",
"│ ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │\n",
"│ │ Rich Layout example │ │\n",
"│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │\n",
"│ ╭─────────────────────────────────────────────────────╮╭──────────────────────────────────────────────────────╮ │\n",
"│ │ Table of Contents ││ ╔══════════════════════════════════════════════════╗ │ │\n",
"│ │ ├── Chapter 1 ││ ║ Chapter 1 ║ │ │\n",
"│ │ │ ├── Section 1 ││ ╚══════════════════════════════════════════════════╝ │ │\n",
"│ │ │ └── Section 2 ││ │ │\n",
"│ │ └── Chapter 2 ││ Hello, world! │ │\n",
"│ │ ├── Section 1 ││ │ │\n",
"│ │ └── Section 2 ││ │ │\n",
"│ │ ││ Section 1 │ │\n",
"│ │ ││ │ │\n",
"│ │ ││ TODO: │ │\n",
"│ │ ││ │ │\n",
"│ │ ││ • [ ] List │ │\n",
"│ │ ││ • [ ] Tasks │ │\n",
"│ │ ││ • [x] Here. │ │\n",
"│ │ ││ │ │\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",
"│ ╰─────────────────────────────────────────────────────╯╰──────────────────────────────────────────────────────╯ │\n",
"│ ╔═════════════════════════════════════════════════════════════════════════════════════════════════════════════╗ │\n",
"│ ║ Contact me: 100gle ║ │\n",
"│ ╚═════════════════════════════════════════════════════════════════════════════════════════════════════════════╝ │\n",
"╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
"\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
}