Elm-like interactive application framework for Python
Project description
Alfort
Alfort is simple and plain ELM-like interactive applicaiton framework for Python. Alfort is motivated to provide declaretive UI framework independent from any backends.
Alfort is developping now. So there will be breaking changes.
Features
- Rendering with Virtual DOM (this feature is truely inspired from hyperapp)
- Elm-like Movel-View-Update architecture
- Independent from Real DOM
- Simple implementation (under 1k loc)
Installation
$ pip install alfort
Example
Code
from typing import Callable
from enum import Enum, auto
from click import prompt
from alfort import Alfort, Dispatch, Effect
from alfort.vdom import Node, Patch, PatchText, Props, VDOM
handlers: dict[str, Callable[[], None]] = {}
class Msg(Enum):
Up = auto()
Down = auto()
class TextNode(Node):
def __init__(self, text: str) -> None:
print(text)
def apply(self, patch: Patch) -> None:
match patch:
case PatchText(new_text):
print(new_text)
case _:
raise ValueError(f"Invalid patch: {patch}")
class AlfortSimpleCounter(Alfort[int, Msg, TextNode]):
def create_text(
self,
text: str,
dispatch: Dispatch[Msg],
) -> TextNode:
handlers["u"] = lambda: dispatch(Msg.Up)
handlers["d"] = lambda: dispatch(Msg.Down)
return TextNode(text)
def create_element(
self,
tag: str,
props: Props,
children: list[TextNode],
dispatch: Dispatch[Msg],
) -> TextNode:
raise ValueError("create_element should not be called")
def main(
self,
) -> None:
self._main()
while True:
c = prompt("press u or d")
if handle := handlers.get(c):
handle()
def main() -> None:
def view(state: int) -> VDOM:
return f"Count: {state}"
def init() -> tuple[int, list[Effect[Msg]]]:
return (0, [])
def update(msg: Msg, state: int) -> tuple[int, list[Effect[Msg]]]:
match msg:
case Msg.Up:
return (state + 1, [])
case Msg.Down:
return (state - 1, [])
app = AlfortSimpleCounter(init=init, view=view, update=update)
app.main()
if __name__ == "__main__":
main()
Output
Count: 0
press u or d: u
Count: 1
press u or d: u
Count: 2
press u or d: u
Count: 3
press u or d: d
Count: 2
press u or d: d
Count: 1
If you need more exmplaes, please check the examples.
Concept
Alfort is inspired by TEA(The Elm Architecture). So Alfort makes you create an interactive application with View
, Model
and Update
. If you need more specification about TEA, please see this documentation.
Therefore, Alfort doesn't support Command. So Alfort uses functions whose type is Callable[[Callable[[Msg], None]], Coroutine[None, None, Any]]
to achieve side effect.
You can run some tasks which have side effects in this function. And, if you need, you can pass the result of side effect as Message to dicpatch
which is given as an argument.
This idea is inspired by hyperapp.
For now, Alfort doesn't support the following features.
- Event subscription
- Virtual DOM comparison by key
- Port to the outside of runtime.
Alfort doesn't provide Real DOM or other Widgets manupulation.
But there is an iterface between your concrete target and Alfort's Virtual DOM.
It is Patche
. So you have to implement some codes to handle some patches.
alfort-dom is an implementation for manupulation DOM.
For development
Install Poery plugins
$ poetry self add "poethepoet[poetry_plugin]"
Run tests
$ poetry poe test
Run linter and formatter
$ poetry poe check
See Also
License
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 alfort-0.1.9.tar.gz
.
File metadata
- Download URL: alfort-0.1.9.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.10.7 Linux/5.15.0-1020-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9ead5d2d300a09ff6844fcbb9e7d583032f33bf42c03fbf80593f1d1e906ae6 |
|
MD5 | 5d5508ad2eed94aff224c5667042dcd7 |
|
BLAKE2b-256 | 650f04b8c0b30763669b9a549aa33b269274f9f99496ba66efc93653dd790239 |
File details
Details for the file alfort-0.1.9-py3-none-any.whl
.
File metadata
- Download URL: alfort-0.1.9-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.10.7 Linux/5.15.0-1020-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 182d858c56eb45aa23b5d3bd4b51394ae094eeb8de4daf7aac9d4d63dc2d548b |
|
MD5 | 4fabdbd025cfb8facb4e31b04f40be8c |
|
BLAKE2b-256 | 1dc36cb8f53268ffa02c504387020c3eb9f18125c12c9925fc55014694055907 |