Skip to main content

Buche

Helper package to pretty-print and interact with Python objects using the Buche logger.

The package includes a repl and a debugger.

Requires Python >= 3.6

Usage

You must install Buche first, either from the release or through npm (npm install -g buche). This package helps interact with Buche, but it is not the application itself. Once you have written your script, use it as follows:

python -m buche yourscript.py

Alternatively, using buche directly:

buche python -u yourscript.py

Display

Here’s an example of what you can do with Buche. You can run this code using 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

You can start an interactive evaluator very easily:

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

Run buche python3 repl.py and you will get an empty window and an input box at the bottom. You can evaluate Python expressions in the input box and get very pretty output, and you can also click on the representations of the objects in order to put them in temporary variables.

Note: start is non-blocking. For a blocking version you can do this:

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

Debugger

By setting the environment variable PYTHONBREAKPOINT to buche.breakpoint, calls to the builtin breakpoint() will use Buche’s repl for debugging. You can use it essentially the same way as pdb, but you get pretty HTML printing.

PYTHONBREAKPOINT=buche.breakpoint buche python3 mycode.py

This variable is automatically set when you run a script with python -m buche.

Metadata

Release files for buche 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for buche 0.2.0
File Size Uploaded
buche-0.2.0.tar.gz 16.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for buche 0.2.0
File Interpreter ABI Platform
buche-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 31.8 kB

Release files / buche-0.2.0.tar.gz

Download URL buche-0.2.0.tar.gz
Size 16.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3c63ba6c885b6fd4e15103dd5bed660443e7efdaeb92ad43a1e0657a4e5c1a65
BLAKE2b-256 checksum
How to use checksums
28368d98a5b7dafbceb8f1c62001ec8e1ec1c4f8709996e341f26406a2b90ce3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0

Release files / buche-0.2.0-py3-none-any.whl

Download URL buche-0.2.0-py3-none-any.whl
Size 15.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ea963eb89cbe96545fe3a033e7bb97cfe7ffe169a56c3e3def7baf52bbe5eb0c
BLAKE2b-256 checksum
How to use checksums
91e74c27ed4b02b6903100438b008f70cbf46766da73a5e76a1c6e470a60f919
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page