Skip to main content

Elm-like interactive application framework for Python

Project description

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

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

alfort-0.1.9.tar.gz (11.2 kB view hashes)

Uploaded Source

Built Distribution

alfort-0.1.9-py3-none-any.whl (11.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page