推荐用 rich.table 渲染表格:先创建 Table 实例,再 add_column() 定义列(支持 style/justify/no_wrap),最后 add_row() 填数据,注意行列数严格匹配;rich.progress 适合多任务进度条,需 add_task() 注册后 update() 推进;Jupyter/VS Code 中需显式设 force_jupyter=True 或 color_system="truecolor";样式优先用 style 参数或 Style 对象,避免过度嵌套 Text。

用 rich.table 渲染带边框/对齐/颜色的表格,别直接拼字符串
手动用空格、| 和 - 拼表格既难维护又容易错位,rich.table 能自动处理列宽、文本对齐、颜色和边框样式。关键点是:先创建 Table 实例,再用 add_column() 定义列(支持 style、justify、no_wrap),最后用 add_row() 填数据——注意行数据类型要和列数严格匹配,否则会报 ValueError: Row has 3 cells but table has 4 columns。
常见错误包括:在 add_column() 中传入 style="red" 却忘了给单元格内容加 Text 或 Panel 包装,导致颜色不生效;或用 add_row(*row_list) 展开嵌套列表时维度出错。示例:
from rich.table import Table
from rich.console import Console
console = Console()
table = Table(show_header=True, header_style="bold magenta")
table.add_column("ID", style="dim", width=4)
table.add_column("Name", style="cyan", no_wrap=True)
table.add_column("Status", justify="right")
table.add_row("1", "Alice", "[green]✅ Active")
table.add_row("2", "Bob", "[red]❌ Inactive")
console.print(table)
用 rich.progress 实现多任务进度条,避免阻塞主线程
Progress 默认同步更新,适合单任务;但多个耗时操作并行时,得用 add_task() 分别注册,再配合 update() 手动推进。容易踩的坑是:忘记调用 start()(虽多数情况自动触发,但在某些上下文如异步协程中会卡住);或在循环里反复 add_task() 而没复用 task ID,造成进度条堆叠。
参数上,total 必须明确指定(不能为 None 否则显示为“?”),completed 初始值默认 0;若需动态更新描述文字,用 update(task_id, description="...")。示例:
from rich.progress import Progress
from time import sleep
with Progress() as progress:
task1 = progress.add_task("[cyan]Processing files...", total=100)
task2 = progress.add_task("[green]Uploading...", total=50)
for _ in range(100):
progress.update(task1, advance=1)
sleep(0.02)
for _ in range(50):
progress.update(task2, advance=1)
sleep(0.03)
在 Jupyter 或 VS Code 终端里 rich 不渲染?检查 Console 初始化方式
Jupyter 内核和部分 IDE 终端不完全兼容 ANSI 序列,rich 可能自动降级为纯文本输出。此时不能只写 print(...),必须显式创建 Console 并设 force_terminal=True 或 force_jupyter=True:
Nodriver是Undetected-Chromedriver的官方继承者,提供异步浏览器自动化功能,可直接与Chrome DevTools Protocol通信,无需Selenium或WebDriver依赖。专为规避反机器人系统(包括Cloudflare和Imperva)的隐蔽网络自动化而构建。
- 在 Jupyter 中:用
Console(force_jupyter=True),否则颜色和进度条可能消失 - 在 VS Code 的 Python Terminal 中:有时需加
Console(color_system="truecolor")强制启用 24-bit 色彩 - 导出日志到文件时:
Console(file=open("log.txt", "w"))会自动禁用所有格式化,这是预期行为,不是 bug
另外,rich 对 Windows CMD 支持有限,Win10 1809+ 推荐启用「Windows Terminal」并确保启用了「ANSI 转义序列」(注册表键 VirtualTerminalLevel 为 1)。
自定义样式时别滥用 Text 嵌套,优先用 Style 参数和主题
很多人想高亮某段文字就一层层套 Text.from_markup("[red][bold]xxx[/bold][/red]"),结果可读性差、性能低。其实 Table、Progress、Panel 等组件都支持直接传 style 字符串(如 "bold red on yellow"),甚至可以预定义 Style 对象复用:
from rich.style import Style
error_style = Style(color="red", bold=True, underline=True)
console.print("File not found", style=error_style)
更进一步,可用 Console(style="dim") 设置全局默认样式,或通过 console.push_styles(...) / pop_styles() 临时覆盖。过度嵌套 Text 还可能导致换行逻辑异常,尤其在窄终端中。
复杂交互场景下,rich 的 Live + Group 组合比硬编码刷新更可靠,但要注意 refresh_per_second 别设太高(>20 容易卡顿),也别在非主线程里直接调用 console.print() ——得用 console.print(..., soft_wrap=True) 配合线程安全锁,或者改用 console.record 缓存后统一输出。
Python免费学习笔记(深入):立即使用
在学习笔记中,你将探索 Python 的核心概念和高级技巧!










