[英]pretty print python code from jupyter notebook
如果您要打印的代碼在單獨的腳本中,您可以使用 CLI pygments
https://pygments.org/docs/cmdline/
為了漂亮地打印代碼,您只需要使用以下內容
!pygmentize your_filename.py
因此,對於 Python 3.5+(編碼為 3.6+),它將是:
def foo():
print(42)
import inspect
from subprocess import check_output
from IPython.core.display import HTML
output = check_output(["pygmentize","-f","html","-O","full,style=emacs","-l","python"],
input=inspect.getsource(foo), encoding='ascii')
HTML(output)
請參閱如何將字符串傳遞到 subprocess.Popen(使用 stdin 參數)? 對於其他 python 版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.