A Python package for creating and manipulating HTML components. It is working similar to React.js, but in Python
Project description
Seamless
Seamless is a Python package for creating and manipulating HTML components. It is working similar to React.js, but in Python.
Installation
pip install python-seamless
Usage
from seamless import Div, H1, P, Component, StyleObject
class MyComponent(Component):
def render(self):
root_style = StyleObject(color="#33343c")
return Div(style=div_style)(
H1(
"Hello, World!",
),
P(
"Welcome to Seamless!"
),
)
from .components import MyComponent
from seamless.renderer import render
@app.get("/")
def hello_world():
return render(MyComponent())
Server actions
It's possible to pass a python function as component props.
The current version works with ASGIApp
.
class Person(Component):
def __init__(self, name: str, age: float):
self.age = age
self.name = name
def render(self):
return Form(on_submit=self.save_age)(
Div(f"Update the age for {name}"),
Label(html_for="age")(
"Age: "
),
Input(
type="text",
on_change=self.set_age
),
Button(type="submit")(
"Submit Age"
),
)
def set_age(self, event_data: ChangeEvent):
self.age = event_data.value
def save_age(self, event_data: SubmitEvent):
user = get_user()
user.age = self.age
save(user)
To call a function on the server include this script in your file
<script src="/socket.io/static/main.js"></script>
Import the middleware and mount it to your app
from fastapi import FastAPI
from seamless.middlewares import ASGIMiddleware as SeamlessMiddleware
app = FastAPI()
app.add_middleware(SeamlessMiddleware)
You can pass the following config to the middleware to change the socket path of all seamless endpoints.
app.add_middleware(
SeamlessMiddleware,
socket_path="/my/custom/path"
)
Actions use socket.io to communicate between server and client.
TODO
- Add detailed documentation
- Add more tests
- Add support for http actions
Contributing
Feel free to open an issue or a pull request.
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
Hashes for python_seamless-0.8.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72ada6de1fb6419edf679ee0e3b4d927f14cd836be47d8c5bac30da4a174fa50 |
|
MD5 | fd5235ffebd1670b222c1e6e1dc591a1 |
|
BLAKE2b-256 | 51c8c1b946b53acdce4086d0a1d6af500cec77cbed86ffa53e1f566ebd11de6d |