Pretty-print Python objects to Buche logger.
Project description
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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file buche-0.2.0.tar.gz
.
File metadata
- Download URL: buche-0.2.0.tar.gz
- Upload date:
- Size: 16.1 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c63ba6c885b6fd4e15103dd5bed660443e7efdaeb92ad43a1e0657a4e5c1a65 |
|
MD5 | 94c116971a6698ecb463915e60a69f4a |
|
BLAKE2b-256 | 28368d98a5b7dafbceb8f1c62001ec8e1ec1c4f8709996e341f26406a2b90ce3 |
File details
Details for the file buche-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: buche-0.2.0-py3-none-any.whl
- Upload date:
- Size: 15.7 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea963eb89cbe96545fe3a033e7bb97cfe7ffe169a56c3e3def7baf52bbe5eb0c |
|
MD5 | 3cdd3b3f707b8c016fd83450f99a9175 |
|
BLAKE2b-256 | 91e74c27ed4b02b6903100438b008f70cbf46766da73a5e76a1c6e470a60f919 |