Skip to main content

A simple Elm like web application framework

Project description

Alfort-DOM

Build Version Downloads Contributors Issues Codecov Apache License 2.0 License

Alfort-DOM is Elm-like web application framework for Python. It uses Alfort to handle Vritual DOM and Pyodide to run its codes on browser.

Installation

$ pip install alfort-dom

Example

from dataclasses import dataclass
from typing import TypeAlias

from alfort import Effect
from alfort.vdom import VDom, el

from alfort_dom import AlfortDom


@dataclass(frozen=True)
class CountUp:
    value: int = 1


@dataclass(frozen=True)
class CountDown:
    value: int = 1


Msg: TypeAlias = CountUp | CountDown


def title(text: str) -> VDom:
    return el("h1", {}, [text])


def count(cnt: int) -> VDom:
    return el("div", {"style": {"margin": "8px"}}, [str(cnt)])


def buttons() -> VDom:
    button_style = {"margin": "4px", "width": "50px"}
    return el(
        "div",
        {},
        [
            el("button", {"style": button_style, "onclick": CountDown(10)}, ["-10"]),
            el("button", {"style": button_style, "onclick": CountDown()}, ["-"]),
            el("button", {"style": button_style, "onclick": CountUp()}, ["+"]),
            el("button", {"style": button_style, "onclick": CountUp(10)}, ["+10"]),
        ],
    )


def view(state: dict[str, int]) -> VDom:
    return el(
        "div",
        {
            "style": {
                "display": "flex",
                "justify-content": "center",
                "align-items": "center",
                "flex-flow": "column",
            }
        },
        [title("Simple Counter"), count(state["count"]), buttons()],
    )


def init() -> tuple[dict[str, int], list[Effect[Msg]]]:
    return ({"count": 0}, [])


def update(msg: Msg, state: dict[str, int]) -> tuple[dict[str, int], list[Effect[Msg]]]:
    match msg:
        case CountUp(value):
            return ({**state, "count": state["count"] + value}, [])
        case CountDown(value):
            return ({**state, "count": state["count"] - value}, [])


app = AlfortDom[dict[str, int], Msg](
    init=init,
    view=view,
    update=update,
)
app.main(root="root")

simple counter

If you need more exmplaes, please check the examples.

For development

Install Poery plugins

$ poetry self add "poethepoet[poetry_plugin]"

Run tests

$ poetry poe test

Run linter and formatter

$ poetry poe check

Run examples

$ poetry poe run-example

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_dom-0.1.3.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

alfort_dom-0.1.3-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file alfort_dom-0.1.3.tar.gz.

File metadata

  • Download URL: alfort_dom-0.1.3.tar.gz
  • Upload date:
  • Size: 10.5 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

Hashes for alfort_dom-0.1.3.tar.gz
Algorithm Hash digest
SHA256 1c4173a6d0a26f5010206833801c074c3fbb3b0612c5957817911b9bc791f3bb
MD5 e096cdcbed5b3e0734e9b4201520bcdc
BLAKE2b-256 53691176d9471d130c1aecd4f42d16086df4ee05e6a507afee0bb669e5d80ed1

See more details on using hashes here.

File details

Details for the file alfort_dom-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: alfort_dom-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 10.9 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

Hashes for alfort_dom-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 74f4e9cfb58a7fb9732402c884d9587768b5ecad4e11107e4225f98d8b95f834
MD5 e82bc9a2b6d9ed10c768e6ebf6f26f2c
BLAKE2b-256 b7f026ad81e12aeb0d4def1962207b763aed76946298f69304522388f81a875a

See more details on using hashes here.

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