跳转到主要内容

将Python对象格式化输出到Buche日志器。

项目描述

一个辅助包,用于使用Buche日志器格式化和交互Python对象。

该包包括一个repl和一个调试器。

需要Python >= 3.6

用法

您必须首先安装Buche,无论是从发布还是通过npm(npm install -g buche)。此包有助于与Buche交互,但它本身不是应用程序。一旦您编写了脚本,就可以按以下方式使用它

python -m buche yourscript.py

或者,直接使用buche

buche python -u yourscript.py

显示

以下是一个Buche可以做到的示例。您可以使用buche python3 examples/demo.py运行此代码。

from buche import buche, 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 HTML
buche.html.h3('Welcome!')

# Display objects
buche(1234)
buche([x * x for x in range(100)])
buche.dict(avocado="green", banana="yellow", cherry="red")

# Open automatically creates an address for an element
div1 = buche.open.div(style="border: 3px solid red")

# You can also set an address explicitly
buche.html.div(address='/two', style="border: 3px solid blue")

# Get a printer for the given address
div2 = buche['/two']

# These objects will go in the divs
div1('One')
div2('Two')
div1('One again')

# Handy tabs component
grocery_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 class
class Color:
    def __init__(self, r, g, b):
        self.r = r
        self.g = g
        self.b = b

    def __hrepr__(self, H, hrepr):
        sz = hrepr.config.swatch_size or 20
        return H.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 recursively
buche(Color(0, 0, 255), swatch_size=50)

# You can evaluate JavaScript on elements
button = buche.open.button("Tickle me")
button.command_eval(expression="this.onclick = () => alert('Hihihihi!')")

Repl

您可以非常容易地启动一个交互式评估器

# repl.py
from buche import repl
repl.start()

运行buche python3 repl.py,您将获得一个空窗口和底部的一个输入框。您可以在输入框中评估Python表达式,并获得非常漂亮的输出,您还可以单击对象的表示并将其放入临时变量中。

注意:start是非阻塞的。对于阻塞版本,您可以这样做

from buche import repl
repl.start(synchronous=True)
repl.query()  # Processes a single command, blocking

调试器

通过设置环境变量 PYTHONBREAKPOINTbuche.breakpoint,内置函数 breakpoint() 的调用将使用Buche的repl进行调试。您可以基本上以与 pdb 相同的方式使用它,但会获得漂亮的HTML打印效果。

PYTHONBREAKPOINT=buche.breakpoint buche python3 mycode.py

当您使用 python -m buche 运行脚本时,此变量会自动设置。

项目详情


下载文件

下载适合您平台的文件。如果您不确定该选择哪个,请了解更多关于 安装包 的信息。

源代码分发

buche-0.2.0.tar.gz (16.1 kB 查看哈希值)

上传时间 源代码

构建分发

buche-0.2.0-py3-none-any.whl (15.7 kB 查看哈希值)

上传时间 Python 3

由以下支持