notpushkin / pyrract

A React-esque library for Python
The Unlicense
13 stars 0 forks source link
component-tree preact react toga vdom widget-toolkit

Pyrract

Pyrract is a React-esque library for Python. It allows you to build component trees with a simple DSL.

Our primary target is currently Toga, an emerging widget toolkit for desktop (Windows, Linux and macOS) and mobile (Android and iOS). (It is still in development, but already usable — see the platform support page).

Let's run a quick demo.

class Root(Component):
    def __init__(self, **props):
        super().__init__(**props)
        self.state = {"count": 0}

    def do_count(self, widget):
        self.set_state(lambda s: {"count": s["count"] + 1})

    def render(self):
        return TogaBox() [
            TogaButton(label="Needs some pudding, huh"),
            # [TN: pudding means padding, see https://github.com/pybee/toga/issues/139]
            TogaButton(label="Clicked %d times" % self.state["count"],
                       on_press=self.do_count),
        ]

app = toga.App(name="TEM COUNTER!!!", app_id="rocks.ale.TemCounter",
               startup=lambda app: render(Root()).base)

If you've seen React, you'll instantly recognize what's going on here.

To try out this code, run:

pip install toga
git clone https://github.com/iamale/pyrract && cd pyrract
python -m pyrract.demo

Now click that button. C'mon. That's all I've got.

TEM COUNTER!

Caveats

Prior art

Pyrract is heavily inspired, loosely based on Preact, a fast, lightweight React-compatible JavaScript library. It is totally awesome.

License

MIT

Final thoughts

Fish can't understand what the actual fuck am I doing