Skip to main content

Python frontend framework which works on Browser

Project description

WebComPy

What is WebComPy

WebComPy is Python frontend framework on Browser (powered by Brython, thank you very much!), which has following features.

  • Component-based declarative rendering
  • Automatic DOM refreshing
  • Built-in router
  • Built-in server / Static Site Generation

Get started

mkdir webcompy-project
cd webcompy-project
pip install webcompy
python -m webcompy init
python -m webcompy start --dev

then access http://127.0.0.1:8080/WebComPy/

Documents and Demos

Sample Code

from webcompy.reactive import Reactive, computed_property, computed
from webcompy.elements import html, repeat, switch, DOMEvent
from webcompy.router import RouterContext
from webcompy.components import (
    define_component,
    ComponentContext,
    TypedComponentBase,
    component_class,
    on_before_rendering,
    component_template,
)


@define_component
def FizzbuzzList(context: ComponentContext[Reactive[int]]):
    @computed
    def fizzbuzz():
        li: list[str] = []
        for n in range(1, context.props.value + 1):
            if n % 15 == 0:
                li.append("FizzBuzz")
            elif n % 5 == 0:
                li.append("Fizz")
            elif n % 3 == 0:
                li.append("Buzz")
            else:
                li.append(str(n))
        return li

    return html.DIV(
        {},
        html.UL(
            {},
            repeat(fizzbuzz, lambda s: html.LI({}, s)),
        ),
    )


FizzbuzzList.scoped_style = {
    "ul": {
        "border": "dashed 2px #668ad8",
        "background": "#f1f8ff",
        "padding": "0.5em 0.5em 0.5em 2em",
    },
    "ul > li:nth-child(3n)": {
        "color": "red",
    },
    "ul > li:nth-child(5n)": {
        "color": "blue",
    },
    "ul > li:nth-child(15n)": {
        "color": "purple",
    },
}


@component_class
class Fizzbuzz(TypedComponentBase(props_type=RouterContext)):
    def __init__(self) -> None:
        self.opened = Reactive(True)
        self.count = Reactive(10)

    @computed_property
    def toggle_button_text(self):
        return "Hide" if self.opened.value else "Open"

    @on_before_rendering
    def on_before_rendering(self):
        self.count.value = 10

    def add(self, ev: DOMEvent):
        self.count.value += 1

    def pop(self, ev: DOMEvent):
        if self.count.value > 0:
            self.count.value -= 1

    def toggle(self, ev: DOMEvent):
        self.opened.value = not self.opened.value

    @component_template
    def template(self):
        return html.DIV(
            {},
            html.H3(
                {},
                "FizzBuzz",
            ),
            html.P(
                {},
                html.BUTTON(
                    {"@click": self.toggle},
                    self.toggle_button_text,
                ),
                html.BUTTON(
                    {"@click": self.add},
                    "Add",
                ),
                html.BUTTON(
                    {"@click": self.pop},
                    "Pop",
                ),
            ),
            html.P(
                {},
                "Count: ",
                self.count,
            ),
            switch(
                {
                    "case": self.opened,
                    "generator": lambda: FizzbuzzList(props=self.count),
                },
                default=lambda: html.H5(
                    {},
                    "FizzBuzz Hidden",
                ),
            ),
        )

ToDo

  • Add PyScript support (Github Repo)
  • Add provide/inject (DI)
  • Add Plugin System

Lisence

This project is licensed under the MIT License, see the LICENSE.txt file for details.

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

webcompy-0.0.3.tar.gz (45.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

webcompy-0.0.3-py3-none-any.whl (72.2 kB view details)

Uploaded Python 3

File details

Details for the file webcompy-0.0.3.tar.gz.

File metadata

  • Download URL: webcompy-0.0.3.tar.gz
  • Upload date:
  • Size: 45.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.4

File hashes

Hashes for webcompy-0.0.3.tar.gz
Algorithm Hash digest
SHA256 f05418d97795ad8364dff7c6c609f297810db9e4d4f9b18077a18b7eda92d4c6
MD5 05e431524a2c3334ac214766aea381b8
BLAKE2b-256 6dc1d322fe92749a60a35b75f55b7a5cf0fbc1755f932f61af4fdd7e05195c88

See more details on using hashes here.

File details

Details for the file webcompy-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: webcompy-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 72.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.4

File hashes

Hashes for webcompy-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 eb53d0159d76782f0ddd70fc3cc2d11ec34c74d82daeffb6d3532c7d2061f28d
MD5 09abc3b2f135f074534dea5709663e7e
BLAKE2b-256 2769f67e037458f0a7f08767bdfc78f4358f5d544bd3d48dd9a7c8a1358fe86d

See more details on using hashes here.

Supported by

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