Skip to main content

A simple web framework for Python.

Project description

BetterWeb

A simple web framework for Python.

Installation

pip install betterweb
uv add betterweb

Diagram

Better Web Excalidraw

Limitations

  • Only SSR, no logic is executed on the client
  • Only can edit the children of the <body> tag in a normal route. No changing the <head> or <html> tag
  • For non-static routes, the server must always be running
  • 99% async
  • Currently no dev mode / hot reload

Example

[!NOTE] See the example for a more complete example. Uploading Better Web Excalidraw.svg…

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 an APIRoute object.
  • websocket_routes: A dictionary of websocket routes. The key is the route path, and the value is a WSRoute object.
  • routes: A dictionary of routes. The key is the route path, and the value is a Route object.
  • static_routes: A dictionary of static routes. The key is the route path, and the value is a StaticRoute object.
  • 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 return None but accept a Request and ResponseConstructor object.

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.

State

use_state(name: str, initial: T | Callable[[], T] = None) -> Tuple[T, Callable[[T], None]]

Creates a stateful value.

  • name: The name of the state. Has no effect on the state, but is used to identify it.
  • initial: The initial value of the state.

use_memo(func: Callable[[], None | Callable[[], None]], deps: list[Any] = None)

Creates a memoized value. Is run when the dependencies change.

  • func: The function to memoize. The name must be unique.
  • deps: The dependencies of the memoized value. Optional: defaults to an empty list.

Returns:

  • None
  • Cleanup function - Called immediately before the next render

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

betterweb-0.1.3.tar.gz (32.8 kB view details)

Uploaded Source

Built Distribution

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

betterweb-0.1.3-py3-none-any.whl (25.7 kB view details)

Uploaded Python 3

File details

Details for the file betterweb-0.1.3.tar.gz.

File metadata

  • Download URL: betterweb-0.1.3.tar.gz
  • Upload date:
  • Size: 32.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for betterweb-0.1.3.tar.gz
Algorithm Hash digest
SHA256 7773268038f858a18bbc31a18ff714abf59b0121a17a135f2b10569bc0aec302
MD5 a2089d2c5380ee6d3d20bf7a738c8108
BLAKE2b-256 359da219939c4c4fedf25dacb4817c7b08ac438ed2147affc53468cfee2294bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for betterweb-0.1.3.tar.gz:

Publisher: publish.yml on R5dan/betterweb

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file betterweb-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: betterweb-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 25.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for betterweb-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 90313c5f6defd124361938cfad6993305683d453a5a1c25253421d32b46fb284
MD5 e57042b864e3c76dc33448c263cdb97d
BLAKE2b-256 4780dc38568b3b33643a18672860705852da738f4a94727574e3f72517da8f60

See more details on using hashes here.

Provenance

The following attestation bundles were made for betterweb-0.1.3-py3-none-any.whl:

Publisher: publish.yml on R5dan/betterweb

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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