A simple web framework for Python.
Project description
BetterWeb
A simple web framework for Python.
Installation
pip install betterweb
uv add betterweb
Example
[!NOTE] See the example for a more complete example.
from betterweb import (
App,
APIRoute,
WSRoute,
ResponseConstructor,
Websocket,
Route,
Console,
DOM,
StaticRoute,
)
from starlette.requests import Request
import time
import asyncio
async def get(request: Request, response: ResponseConstructor):
await response.json({"method": "GET"})
async def post(request: Request, response: ResponseConstructor):
await response.json({"method": "POST"})
async def stream(request: Request, response: ResponseConstructor):
stream = await response.stream()
print("Sending")
for i in range(10):
await stream.send(f"Hello World {i}\n".encode())
await asyncio.sleep(1)
print("Closing")
await stream.close()
async def onclick():
await Console.log("Clicked") # Logs it to the client only, to see check the browser console
print("Clicked")
# See [#Routes] for more information
async def page():
async def client():
await Console.log("Hello World")
return DOM.create("div", {}, [
DOM.create("h1", {}, ["Hello World"]),
DOM.create("button", {
"onclick": onclick # A python function that will be called when the button is clicked
}, ["Click Me"]),
])
return client
async def ws(ws: Websocket):
await ws.accept()
while True:
msg = await ws.receive()
if msg["text"]:
print(msg["text"])
await ws.sendText(msg["text"])
app = App(
api_routes={
"/get": APIRoute("/get", ["GET"], get),
"/post": APIRoute("/post", ["POST"], post),
"/stream": APIRoute("/stream", ["GET"], stream),
},
websocket_routes={"/ws": WSRoute("/ws", ws)},
routes={"/": Route("/", page)},
static_routes={
"/static": StaticRoute.from_file("static/index.html", "text/html")
},
)
app.run()
Documentation
App
The App class is the main class of the betterweb package. It is used to create an instance of the app and run it.
App(api_routes: dict[str, APIRoute], websocket_routes: dict[str, WSRoute], routes: dict[str, Route], static_routes: dict[str, StaticRoute], on_startup: Callable[[], None] = None, on_shutdown: Callable[[], None] = None)
Creates a new instance of the App class.
api_routes: A dictionary of API routes. The key is the route path, and the value is anAPIRouteobject.websocket_routes: A dictionary of websocket routes. The key is the route path, and the value is aWSRouteobject.routes: A dictionary of routes. The key is the route path, and the value is aRouteobject.static_routes: A dictionary of static routes. The key is the route path, and the value is aStaticRouteobject.on_startup: A function to be called when the app starts.on_shutdown: A function to be called when the app stops.
run(host: str = "127.0.0.1", port: int = 8000)
Runs the app.
host: The host to run the app on.port: The port to run the app on.
APIRoute
The APIRoute class is used to define an API route.
APIRoute(path: str, methods: list[str], handler: Callable[[Request, ResponseConstructor], Awaitable[None]])
Creates a new instance of the APIRoute class.
path: The path of the route.methods: A list of HTTP methods that the route supports.handler: The handler function for the route. Should returnNonebut accept aRequestandResponseConstructorobject.
ResponseConstructor
The ResponseConstructor class is used to construct a response.
ResponseConstructor.__call__(body: Optional[bytes] = None, options: Optional[OPTIONS] = None)
Creates a new instance of the ResponseConstructor class.
body: The body of the response.options: The options of the response.
ResponseConstructor.error()
ResponseConstructor.redirect()
ResponseConstructor.json(data: dict, options: Optional[OPTIONS] = None)
ResponseConstructor.stream(options: Optional[OPTIONS] = None)
Returns a StreamResponse object.
StreamResponse.send(data: bytes)
Sends data to the stream.
StreamResponse.close()
Closes the stream.
WSRoute
The WSRoute class is used to define a websocket route.
WSRoute(path: str, handler: Callable[[Websocket], Awaitable[None]], close: bool = True)
Creates a new instance of the WSRoute class.
path: The path of the route.handler: The handler function for the route.close: Whether to close the websocket connection after the handler function is called.
Route
The Route class is used to define a route.
Route(path: str, handler: Callable[[], Awaitable[Callable[[], Awaitable[DOMNode]]]])
Creates a new instance of the Route class.
path: The path of the route.handler: The handler function for the route.
The handler function should return an async function The returned function is called the client function The handler function is called the server function
The client function should return a DOM node
It will be called whenever the state changes
Client APIs like Console can only be called from the client function
The server function should return the client function
It will be called exactly once before any response is sent
Client APIs like Console can not be called from the server function as the response has not been sent yet
DOM
The DOM class is used to create DOM nodes.
DOM.create(tag: str, properies: dict, children: list[DOMNode | str])
Creates a new DOM node.
tag: The tag of the DOM node.properies: The properties of the DOM node.children: The children of the DOM node.
StaticRoute
The StaticRoute class is used to define a static route.
StaticRoute(path: str, data: bytes, mime: str)
Creates a new instance of the StaticRoute class.
path: The path of the route.data: The data of the route.mime: The MIME type of the route.
StaticRoute.from_file(path: str, mime: str)
Creates a new instance of the StaticRoute class from a file.
path: The path of the file.mime: The MIME type of the file.
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 betterweb-0.1.0.tar.gz.
File metadata
- Download URL: betterweb-0.1.0.tar.gz
- Upload date:
- Size: 24.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abeaa6ef0d91e009e8a1c76600c0b133a8d1533697a2801c2889329fa462ccee
|
|
| MD5 |
1da3579f4321b3b1b6b4b943927daf92
|
|
| BLAKE2b-256 |
18d9faf7bdee01cc078ce2c2fa30cf0256fc84baa2313f6654afe1223ff2f6e8
|
Provenance
The following attestation bundles were made for betterweb-0.1.0.tar.gz:
Publisher:
publish.yml on R5dan/betterweb
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
betterweb-0.1.0.tar.gz -
Subject digest:
abeaa6ef0d91e009e8a1c76600c0b133a8d1533697a2801c2889329fa462ccee - Sigstore transparency entry: 258348295
- Sigstore integration time:
-
Permalink:
R5dan/betterweb@3b438f574cb3a732271e86389810f37548f8d6b8 -
Branch / Tag:
refs/tags/V0.1.0 - Owner: https://github.com/R5dan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3b438f574cb3a732271e86389810f37548f8d6b8 -
Trigger Event:
release
-
Statement type:
File details
Details for the file betterweb-0.1.0-py3-none-any.whl.
File metadata
- Download URL: betterweb-0.1.0-py3-none-any.whl
- Upload date:
- Size: 15.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa9e52dc937c49d43022082fcef08da3064de5ab897ee2a4f19cf403ac552358
|
|
| MD5 |
2be4f44cbf010f170377b3e68b3f1d2f
|
|
| BLAKE2b-256 |
180ff2e888670135c136a72b0b2055adc8dd002d4e53372b5648afba2107c20f
|
Provenance
The following attestation bundles were made for betterweb-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on R5dan/betterweb
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
betterweb-0.1.0-py3-none-any.whl -
Subject digest:
fa9e52dc937c49d43022082fcef08da3064de5ab897ee2a4f19cf403ac552358 - Sigstore transparency entry: 258348326
- Sigstore integration time:
-
Permalink:
R5dan/betterweb@3b438f574cb3a732271e86389810f37548f8d6b8 -
Branch / Tag:
refs/tags/V0.1.0 - Owner: https://github.com/R5dan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3b438f574cb3a732271e86389810f37548f8d6b8 -
Trigger Event:
release
-
Statement type: