Skip to main content

增强print函数,使其输出内容能被点击并直接跳转到源代码位置

Project description

nb_print

pypi versions license

nb_print:让你的 print() "活" 起来,一键跳转到代码位置。

在大型项目或遗留代码中进行调试时,你是否曾被满屏的 print 输出所困扰,苦苦寻找 "这行输出究竟是哪里打印的?" nb_print 正是为解决这一痛点而生。

它通过极简的方式,增强了 Python 内置的 print 函数,让你的每一次打印输出都附带源码位置的超链接。


✨ 核心功能

  • 🚀 无感植入: 只需在项目入口 import nb_print 一次,无需修改任何现有 print 代码,即可全局生效。
  • 🖱️ 点击跳转: 在 PyCharm, VSCode 等现代 IDE 的控制台中,点击输出前缀即可直接跳转到发起 print 的确切代码行。
  • 🎨 智能美化: 自动美化打印内容,提升复杂数据结构(如 dict, list)在控制台中的可读性。
  • 🎯 解决痛点: 彻底告别大海捞针式的 print 调试,极大提升开发和排错效率。

📦 安装

pip install nb-print

🚀 使用方法

在你的项目主入口文件(例如 main.pyapp.py)的顶部,加入一行导入语句即可。

# a_module.py
def some_function():
    user_data = {'id': 1001, 'name': 'Alice', 'roles': ['admin', 'editor']}
    print(user_data)

# main.py
import nb_print  # 只需要在程序入口导入一次

import requests
from a_module import some_function

print("程序开始运行...")
some_function()
print(requests.get('https://www.baidu.com/'))
print("程序运行结束。")

效果演示

使用前,你的控制台输出是这样的,无法快速定位来源:

程序开始运行...
{'id': 1001, 'name': 'Alice', 'roles': ['admin', 'editor']}
<Response [200]>
程序运行结束。

使用 nb_print,控制台输出会附带可点击的文件路径和行号:

main.py:10 - 程序开始运行...
a_module.py:4 - {'id': 1001, 'name': 'Alice', 'roles': ['admin', 'editor']}
main.py:12 - <Response [200]>
main.py:13 - 程序运行结束。

在 PyCharm 或 VSCode 中,main.py:10 这部分文本会变成一个超链接,点击即可跳转。

🛠️ 工作原理

nb_print 的实现非常轻量,其核心原理是 "猴子补丁" (Monkey Patching)

  1. import nb_print 时,它会用一个自定义的函数替换掉 Python builtins 模块中的原生 print 函数。
  2. 这个新的 print 函数在执行打印操作前,会使用 inspect 模块回溯调用堆栈 (inspect.stack())。
  3. 通过分析堆栈信息,它可以精确地获取到调用者的文件名和行号。
  4. 最后,它将文件名和行号格式化后,与原始的打印内容一并输出到控制台。现代 IDE 会自动识别 文件名:行号 这种格式并将其渲染为可点击的链接。

🤝 贡献

欢迎通过提交 Issues 和 Pull Requests 来贡献代码、报告问题或提出功能建议。

📄 许可证

本项目基于 MIT License 开源。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

very-nb-print-1.9.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

very_nb_print-1.9-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file very-nb-print-1.9.tar.gz.

File metadata

  • Download URL: very-nb-print-1.9.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.9

File hashes

Hashes for very-nb-print-1.9.tar.gz
Algorithm Hash digest
SHA256 19164e32788b750d4fdad56044535e09d4be2b7fb80973990dedbcc43e9ef570
MD5 4765e240d9332b723d5f0170b90a7198
BLAKE2b-256 fa430b9d8019710312a9e1a74212413a6f1daff7449684dd700f43a8a1225584

See more details on using hashes here.

File details

Details for the file very_nb_print-1.9-py3-none-any.whl.

File metadata

  • Download URL: very_nb_print-1.9-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.9

File hashes

Hashes for very_nb_print-1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 6eaf68401ae9d49973d58fdb554a0678ebd19cb5b2e30e521130e322a5fa301d
MD5 0baeb9bf42bb0ab0e7bd24845c1387e5
BLAKE2b-256 6ef08ecabfd9c68000f708402ac8f0f11e4ef27311746a7755b85d83ee93040b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page