Python 我只想在面板小部件中显示一个asci字符串,并保留其格式/颜色

Python 我只想在面板小部件中显示一个asci字符串,并保留其格式/颜色,python,widget,ascii,panel,pygments,Python,Widget,Ascii,Panel,Pygments,我只想在面板小部件中显示一个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[3

我只想在面板小部件中显示一个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)
#以下是pygments格式化后的字符串:
(最近一次的最后一次电话最后一次的最后一次的最后一次的最后一次的最后一次的最后一次的最后一次的最后一次的最后一次的最后一次的最后一次的最后一次的电话):\x1b[39 m\n文件\n\n文件\x1b[38;38;2;2;0;0;128;128;0;128;0;0;128;0;0;0;128;0;0;0;0;0;128;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;关键词关键词关键词关键词;0;0;0;0;0;0;0;0;0;0;0;米米米米米米米米米米;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0米米米米;x1b10;102;102;102;102;102;102;102;102;102;102;102;102;102;102;102;102;102;102;102;102;102;10;102;10;102;10;102;10;102;10;10;102;10;10;102;10;102;10;102;102;102;102;102;102;102;102;10;102;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10;10[38;2;102;102;102m6\x1b39;00米米米米我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家我家102;102;102m0\x1b[39米,x1b[38;2;186;33;33米“\x1b[39m\x1b[38;2;186;33;33moh编号\x1b[39m\x1b[38;2;186;33;33m”\x1b[39m\n\x1b[38;2;255;0;0mAssertionError\x1b[39m:噢,不\n'
#我希望它在面板中看起来像这样:
打印(测试)
我所拥有的:

#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)
#我能得到的最接近的是:
作为pn导入面板
字符串\u b4\u pygments='回溯(最近一次调用):\n File“”,第17行,在\n myfunc(3)\n File“”中,第6行,在myfunc\n return myfunc(x-1)\n File“”中,第6行,在myfunc\n return myfunc(x-1)\n File“”中,第6行,在myfunc\n return myfunc(x-1)\n File“”中,第5行,在myfunc\n assert x>0中,“噢no”\n错误:噢no\n
pn.pane.Str(字符串)

我发现捕获错误并将其格式化为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)