Getting Started
Installâ
pip install beaverweb
This installs Jinja2 as a dependency. No other third-party packages are required.
Your first applicationâ
Create a file called app.py:
from beaver import App, Response
app = App()
@app.get("/")
def home(req):
return Response("Hello, BeaverWeb!")
if __name__ == "__main__":
app.run()
Run it:
python app.py
The process reports the listening address:
Listening on 127.0.0.1:5000
In another terminal:
curl http://127.0.0.1:5000/
# â Hello, BeaverWeb!
Reading the requestâ
Every handler receives a Request object with the following attributes:
req.methodâ the HTTP method ("GET","POST", etc.)req.pathâ the request path (e.g."/users/42")req.headersâ a dict of headers with lowercased keysreq.query_paramsâ aMultiDictof query-string values (supports?tag=a&tag=b)req.path_paramsâ a dict of captured path variablesreq.bodyâ the raw request body as bytesreq.json()â parses the body as JSON whenContent-Type: application/json
Example combining query parameters and headers:
@app.get("/hello")
def hello(req):
name = req.query_params.get("name", "stranger")
ua = req.headers.get("user-agent", "unknown")
return Response(f"Hello, {name}! You're using {ua}.")