Skip to main content

Alfort

Build Version Downloads Contributors Issues Codecov Apache License 2.0 License

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

Apache-2.0

Metadata

Release files for alfort 0.1.9

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

Source distribution (sdist)

Source distribution for alfort 0.1.9
File Size Uploaded Provenance Actions
alfort-0.1.9.tar.gz 11.2 kB Without provenance Details for alfort-0.1.9.tar.gz

Built distribution (wheel)

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

Total release size: 22.2 kB

Release files / alfort-0.1.9.tar.gz

Download URL alfort-0.1.9.tar.gz
Size 11.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b9ead5d2d300a09ff6844fcbb9e7d583032f33bf42c03fbf80593f1d1e906ae6
BLAKE2b-256 checksum
How to use checksums
650f04b8c0b30763669b9a549aa33b269274f9f99496ba66efc93653dd790239
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.10.7 Linux/5.15.0-1020-azure

Release files / alfort-0.1.9-py3-none-any.whl

Download URL alfort-0.1.9-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
182d858c56eb45aa23b5d3bd4b51394ae094eeb8de4daf7aac9d4d63dc2d548b
BLAKE2b-256 checksum
How to use checksums
1dc36cb8f53268ffa02c504387020c3eb9f18125c12c9925fc55014694055907
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.10.7 Linux/5.15.0-1020-azure

Security for alfort 0.1.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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