漂亮地将python对象打印到buche logger。
buche的Python项目详细描述
helper包,使用Buchelogger打印并与python对象交互。
这个包包括一个repl和一个调试器
需要Python>;=3.6
用法
必须首先安装Buche,可以从release安装,也可以通过npm(npm install -g buche)安装。这个包有助于与buche交互,但它不是应用程序本身。编写脚本后,请按如下方式使用:
python -m buche yourscript.py
或者,直接使用buche:
buche python -u yourscript.py
显示
这里有一个你可以用布希做的例子。您可以使用buche python3 examples/demo.py运行此代码。
frombucheimportbuche,H# You don't have to set a template, but if you do, it must be the# very first command you emit, before any printing.# You can also give `src=<path-to-file>` instead of `content=...`buche.command_template(content=H.div['my-template'](address="/"))# Use this command to add styles, stylesheets, scripts, etc.buche.command_resource(content=H.style(""" .my-template { background-color: #eee; padding: 5px; display: flex; flex-direction: column; align-items: start; } """))# Display simple HTMLbuche.html.h3('Welcome!')# Display objectsbuche(1234)buche([x*xforxinrange(100)])buche.dict(avocado="green",banana="yellow",cherry="red")# Open automatically creates an address for an elementdiv1=buche.open.div(style="border: 3px solid red")# You can also set an address explicitlybuche.html.div(address='/two',style="border: 3px solid blue")# Get a printer for the given addressdiv2=buche['/two']# These objects will go in the divsdiv1('One')div2('Two')div1('One again')# Handy tabs componentgrocery_list=buche.open.boxTabs()fruit=grocery_list.open.tabEntry(label='Fruits',active=True,)vegetable=grocery_list.open.tabEntry(label='Veggies',)fruit.html.div(H.s("Pineapple"))fruit.html.div("Raspberry")fruit.html.div("Grape")vegetable.html.div("Carrot")vegetable.html.div("Potato")vegetable.html.div("Yam")# Customize the representation of a classclassColor:def__init__(self,r,g,b):self.r=rself.g=gself.b=bdef__hrepr__(self,H,hrepr):sz=hrepr.config.swatch_sizeor20returnH.div(style=f'display:inline-block;width:{sz}px;height:{sz}px;margin:2px;'f'background-color:rgb({self.r},{self.g},{self.b});')# This will call __hrepr__buche(Color(255,0,0))# Configuration values can be anything and are propagated recursivelybuche(Color(0,0,255),swatch_size=50)# You can evaluate JavaScript on elementsbutton=buche.open.button("Tickle me")button.command_eval(expression="this.onclick = () => alert('Hihihihi!')")
回复
您可以很容易地启动交互式计算器:
# repl.pyfrombucheimportreplrepl.start()
运行buche python3 repl.py,您将得到一个空窗口和底部的一个输入框。您可以在输入框中计算python表达式并获得非常漂亮的输出,还可以单击对象的表示,以便将它们放入临时变量中。
注意:start是非阻塞的对于阻止版本,可以执行以下操作:
frombucheimportreplrepl.start(synchronous=True)repl.query()# Processes a single command, blocking
调试器
通过将环境变量PYTHONBREAKPOINT设置为buche.breakpoint,对内置breakpoint()的调用将使用buche的repl进行调试。您可以用与pdb基本相同的方式使用它,但是您可以得到漂亮的html打印。
PYTHONBREAKPOINT=buche.breakpoint buche python3 mycode.py
使用python -m buche运行脚本时会自动设置此变量。