Immediate-mode web framework
Project description
FlyWeb
FlyWeb is a smol live immediate-mode web framework for Python:
- smol: minimal dependencies (no frontend build required).
- live: updates are pushed to the browser in real time.
- immediate-mode: you write a function that emits HTML elements for the whole page, immediate mode GUI-style.
- web framework: FlyWeb takes care of sending the web page contents to the browser, and calling your Python event handlers magically.
- Python: 3.10+ and an ASGI server required.
Examples
Here's a minimal program that renders a counter and an [INCREMENT] button:
import asyncio
import flyweb
class Counter:
def __init__(self):
self._count = 0
def render(self, w: flyweb.FlyWeb) -> None:
with w.div():
w.text(f"count is {self._count}")
with w.div():
w.button("INCREMENT", onclick=self._increment)
def _increment(self, _) -> None:
self._count += 1
async def main():
counter = Counter()
await flyweb.Server(counter.render, port=8000).run()
if __name__ == "__main__":
asyncio.run(main())
There are a couple more examples under flyweb/examples.
Try it out
$ pip install 'flyweb[examples] @ git+https://github.com/girtsf/flyweb'
$ python -m flyweb.examples.todo
Then go to http://localhost:8000/.
Design
Behind the scenes, FlyWeb works like this:
- Your
renderfunction builds up a virtual DOM. - This virtual DOM gets serialized to JSON and sent to the frontend over socket.io. Any event handlers get converted to magic strings that say "hey frontend, please send me a message if this event happens".
- Frontend turns the JSON structure into a VDOM that Maquette then turns into a real DOM.
- As you interact with the web page, events are sent to the backend,
decoded, and matching event handlers are called. Your
renderfunction is called again, and the results are sent to the frontend. Maquette diffs the VDOMs and updates the real DOM with any changes that happened.
FlyWeb uses anyio library (thus should
work with either built-in asyncio or
trio libraries).
Limitations
FlyWeb is mostly intended as a quick way of adding simple web interfaces to internal tools without having to do a bunch of scaffolding or frontend builds. It probably won't be suitable for handling complex pages or many users.
Currently it assumes that you want to share the state between all the webpage users. Implementing per-user state would not be too difficult.
Security: currently there is none, and it's probably pretty easy to crash the backend if you try.
Status
It works, at least for simple pages. The API is definitely not stable and will likely change as it evolves.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flyweb_framework-0.3.2.tar.gz.
File metadata
- Download URL: flyweb_framework-0.3.2.tar.gz
- Upload date:
- Size: 42.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77a57425ddd59b83ef6e94c42359b0553e7227702c798effa8e620e4c40e5231
|
|
| MD5 |
d39d60a33add550e6dbda3f1b64aac3d
|
|
| BLAKE2b-256 |
b243d05e3079936e7b9cbf66267838e0e410cd2378bbc56cc083a1d906e2dba9
|
File details
Details for the file flyweb_framework-0.3.2-py3-none-any.whl.
File metadata
- Download URL: flyweb_framework-0.3.2-py3-none-any.whl
- Upload date:
- Size: 44.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d8db2b0c8d0c371c7a1902b1dc202eb2d1ddecd69d6f4cc1b6c9f2f90c9db8a
|
|
| MD5 |
1bc8b691cfef1d13637a7977ae19fc20
|
|
| BLAKE2b-256 |
97028c0f006a05316d5c25715f87b31ec2cac0f41b942f0ef5edbada2ecca966
|