Reactive user interfaces
Project description
Collagraph 📓
Reactive user interfaces.
The word Collagraphy is derived from the Greek word koll or kolla, meaning glue, and graph, meaning the activity of drawing.
Inspired by Vue and React.
Features
Write your Python interfaces in a declarative manner with plain render functions, component classes or even single-file components using Vue-like syntax, but with Python!
- Reactivity (made possible by leveraging observ)
- Function components
- Class components with local state and life-cycle methods/hooks
- Single-file components with Vue-like template syntax (
.cgx
files) - Custom renderers
Here is an example that shows a counter, made with a component with Vue-like syntax:
Contents of counter.cgx
:
<template>
<widget>
<label
:text="f'Count: {count}'"
/>
<button
text="bump"
@clicked="bump"
/>
</widget>
</template>
<script>
import collagraph as cg
class Counter(cg.Component):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.state["count"] = 0
def bump(self):
self.state["count"] += 1
</script>
Contents of app.py
:
from PySide6 import QtWidgets
import collagraph as cg
# After importing collagraph, it's possible to import
# components directly from .cgx files
from counter import Counter
# Create a Collagraph instance with a PySide renderer
# and register with the Qt event loop
gui = cg.Collagraph(renderer=cg.PySideRenderer())
# Render the component into a container
# (in this case the app but can be another widget)
app = QtWidgets.QApplication()
gui.render(cg.h(Counter), app)
app.exec()
Which looks something like this:
Instead of using a python file as an entry point to run components, you can run them directly using the collagraph CLI:
poetry run collagraph examples/pyside/counter.cgx
For more examples, please take a look at the examples folder.
Currently there are three renderers:
- PysideRenderer: for rendering PySide6 applications
- PygfxRenderer: for rendering 3D graphic scenes with Pygfx
- DomRenderer: for rendering to browser DOM through PyScript (or rather Pyodide)
It is possible to create a custom Renderer using the Renderer interface, to render to other UI frameworks, for instance wxPython.
Development
To try out Collagraph or start development, run:
# Basic dev setup (no pygfx or pyside)
poetry install
# Full dev setup
poetry install --with pyside --extras pyside --extras pygfx
# Run example:
poetry run python examples/pyside/layout-example.py
# Run test suite:
poetry run pytest
# Install git pre-commit hooks to make sure tests/linting passes before committing
poetry run pre-commit install
Syntax Highlighting
Syntax highlighting for single-file components (.cgx
) is supported for VSCode and Sublime Text:
Formatting and linting
Linting cgx files is possible with a flake8 plugin: flake8-cgx. Formatting the contents of the script tag can be done with black-cgx.
Project details
Release history Release notifications | RSS feed
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 collagraph-0.7.0.tar.gz
.
File metadata
- Download URL: collagraph-0.7.0.tar.gz
- Upload date:
- Size: 33.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e86c5385239bd7e7c8e543b05514c7b547d40d73a147c9f3c6b3e9d1a6248205 |
|
MD5 | e8f85bd238b3127de322c613052671bf |
|
BLAKE2b-256 | b082acad1c119d2f6309df21201729990bc6ed84c52f3b0413d4eac48c02c2e7 |
File details
Details for the file collagraph-0.7.0-py3-none-any.whl
.
File metadata
- Download URL: collagraph-0.7.0-py3-none-any.whl
- Upload date:
- Size: 44.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad3cb7c575b941078a7c8742e2133f440365a9c41885fdabbc4bff6683c8ceff |
|
MD5 | dcb91b97a6913fe26b37fb04cdb2ab33 |
|
BLAKE2b-256 | 816f8201445a30cc27e3349d8d2b5d886e7973c283b5882fb9a8f459c3b53ac3 |