python - 我要做的就是在 Panel 小部件中显示一个 asci 字符串并保留其格式/颜色
问题描述
我要做的就是在 Panel 小部件中显示一个 asci 字符串并保留其格式/颜色。
我想要的是:
#here's the string after pygments formatting:
test = '\x1b[38;2;0;68;221mTraceback (most recent call last):\x1b[39m\n File \x1b[38;2;0;128;0m"<ipython-input-3-39b4fbfd5961>"\x1b[39m, line \x1b[38;2;102;102;102m17\x1b[39m, in <module>\n myfunc(\x1b[38;2;102;102;102m3\x1b[39m)\n File \x1b[38;2;0;128;0m"<ipython-input-3-39b4fbfd5961>"\x1b[39m, line \x1b[38;2;102;102;102m6\x1b[39m, in myfunc\n \x1b[38;2;0;128;0;01mreturn\x1b[39;00m myfunc(x\x1b[38;2;102;102;102m-\x1b[39m\x1b[38;2;102;102;102m1\x1b[39m)\n File \x1b[38;2;0;128;0m"<ipython-input-3-39b4fbfd5961>"\x1b[39m, line \x1b[38;2;102;102;102m6\x1b[39m, in myfunc\n \x1b[38;2;0;128;0;01mreturn\x1b[39;00m myfunc(x\x1b[38;2;102;102;102m-\x1b[39m\x1b[38;2;102;102;102m1\x1b[39m)\n File \x1b[38;2;0;128;0m"<ipython-input-3-39b4fbfd5961>"\x1b[39m, line \x1b[38;2;102;102;102m6\x1b[39m, in myfunc\n \x1b[38;2;0;128;0;01mreturn\x1b[39;00m myfunc(x\x1b[38;2;102;102;102m-\x1b[39m\x1b[38;2;102;102;102m1\x1b[39m)\n File \x1b[38;2;0;128;0m"<ipython-input-3-39b4fbfd5961>"\x1b[39m, line \x1b[38;2;102;102;102m5\x1b[39m, in myfunc\n \x1b[38;2;0;128;0;01massert\x1b[39;00m x \x1b[38;2;102;102;102m>\x1b[39m \x1b[38;2;102;102;102m0\x1b[39m, \x1b[38;2;186;33;33m"\x1b[39m\x1b[38;2;186;33;33moh no\x1b[39m\x1b[38;2;186;33;33m"\x1b[39m\n\x1b[38;2;255;0;0mAssertionError\x1b[39m: oh no\n'
#I want it to look like this in Panel:
print(test)
我有的:
#The closest I can get is:
import panel as pn
string_b4_pygments = 'Traceback (most recent call last):\n File "<ipython-input-3-39b4fbfd5961>", line 17, in <module>\n myfunc(3)\n File "<ipython-input-3-39b4fbfd5961>", line 6, in myfunc\n return myfunc(x-1)\n File "<ipython-input-3-39b4fbfd5961>", line 6, in myfunc\n return myfunc(x-1)\n File "<ipython-input-3-39b4fbfd5961>", line 6, in myfunc\n return myfunc(x-1)\n File "<ipython-input-3-39b4fbfd5961>", line 5, in myfunc\n assert x > 0, "oh no"\nAssertionError: oh no\n'
pn.pane.Str(string_b4_pygments)
解决方案
我发现捕获错误并将其格式化为 html 而不是 asci 可以解决问题。例如
import panel as pn
import traceback as tb
from pygments import highlight
from pygments.lexers import Python3TracebackLexer
from pygments.formatters import HtmlFormatter
pn.extension()
def myfunc(x):
assert x > 0, "oh no"
return myfunc(x-1)
def extract_format_tb_html(e):
traceback_str = ''.join(tb.format_exception(None, e, e.__traceback__))
return highlight(traceback_str, Python3TracebackLexer(), HtmlFormatter(noclasses=True))
try:
myfunc(3)
except Exception as e:
test = extract_format_tb_html(e)
pn.pane.Str(test)
推荐阅读
- hyperledger-fabric - 关于超级账本 Fabric 网络的参与者
- r - rJava导入在气流中不起作用
- javascript - React - 第二次点击后调用另一个函数
- ios - 在使用 Objective-C 制作的 App 中使用 Swift 代码
- python - 从货币符号中提取数字并格式化
- oracle - 在 Oracle 中扩展父子层次结构记录以显示父记录的根记录
- heroku - 将 Heroku Postgres 从标准降级为爱好
- angular - 角度 6:错误 TS2315:类型“ModuleWithProviders”不是通用的
- android - 无法打开 HAXM 设备:ERROR_FILE_NOT_FOUND
- coq - 以 Prop inhabitant 作为参数的定点