Quickstart
This page takes you from an empty directory to a small site with two pages, a form handler, a shared header, and a custom error page. Each step adds one file.
Install
Cylinder needs Python 3.11 or newer. Install it together with a WSGI server:
pip install cylinder waitress
Cylinder works with any WSGI server. These pages use Waitress because it runs on every platform.
Step 1: a page
Create this layout:
my_project
|-- cylinder_main.py
|-- /my_webapps
|-- webapp1.page.get.py
cylinder_main.py creates the app and tells Cylinder which webapp should handle each request:
import cylinder
import waitress
def main():
app = cylinder.get_app(app_map)
waitress.serve(app, host="127.0.0.1", port=8080)
def app_map(request):
# app_map() returns:
# 1) the directory that holds your webapps
# 2) the name of the webapp that should handle this request
return "my_webapps", "webapp1"
if __name__ == "__main__":
main()
webapp1.page.get.py handles GET /:
def main(response):
response.data = "Hello World!"
return response
Start it from the my_project directory:
python cylinder_main.py
Open http://127.0.0.1:8080. You should see Hello World!.
The filename did the routing. webapp1 is the name of the webapp, .page marks the file as a page
handler, and .get is the HTTP method it answers.
Step 2: a second page
Add a file whose path matches the URL you want, in a directory named after the webapp:
my_project
|-- cylinder_main.py
|-- /my_webapps
|-- webapp1.page.get.py
|-- /webapp1
|-- /foo
|-- bar.page.get.py
bar.page.get.py:
def main(response):
response.data = "Hello Bar!"
return response
Open http://127.0.0.1:8080/foo/bar. You did not restart anything: Cylinder loads handler files when a request arrives.
That covers handler files. Once handlers import modules of your own, a change to one of those needs a restart, and a reloading server does it for you.
Step 3: another HTTP method
To accept a POST at the same URL, add a file with .post in its name:
|-- /foo
|-- bar.page.get.py
|-- bar.page.post.py
bar.page.post.py:
def main(request, response):
name = request.form.get("name", "stranger")
response.data = f"Thanks, {name}"
return response
This handler asks for request as well as response. A handler names the things it needs as parameters,
in any order, and Cylinder passes them in. request is a normal
Werkzeug Request.
Try it:
curl -d "name=Ada" http://127.0.0.1:8080/foo/bar
Step 4: a header on every page
A hook is a file that runs before or after the handlers in its part of the tree. Add a late hook at the root of the site:
|-- /my_webapps
|-- webapp1.page.get.py
|-- webapp1.hook.late.py
webapp1.hook.late.py:
def main(response):
response.headers["X-Frame-Options"] = "DENY"
return response
.hook.late marks a late hook. A hook is not named for an HTTP method, and runs for every one. Every
response from the site now carries that header, including the pages you wrote before the hook existed.
Step 5: a custom 404 page
Try http://127.0.0.1:8080/no/such/page. You get Werkzeug’s plain 404 page. Nobody wrote a file for that
URL, and a handler answers for its own path only, so webapp1.page.get.py is not run for it.
A URL with a dynamic part, such as /users/123, gets a file whose name stands for any value:
users/{user_id}.page.get.py. Routing shows how.
To use your own 404 page, add an error handler file named after the status code:
|-- /my_webapps
|-- webapp1.404.py
|-- webapp1.page.get.py
|-- webapp1.hook.late.py
webapp1.404.py:
from markupsafe import escape
def main(request, response):
response.data = f"Nothing lives at {escape(request.path)}"
return response
The response of an error is HTML, and the path is text that the client chose, so the handler escapes it.
markupsafe is installed with Werkzeug.
The response still has the 404 status. The file only decides what it says.
What you built
my_project
|-- cylinder_main.py
|-- /my_webapps
|-- webapp1.404.py every 404 on the site
|-- webapp1.page.get.py GET /
|-- webapp1.hook.late.py runs after every request
|-- /webapp1
|-- /foo
|-- bar.page.get.py GET /foo/bar
|-- bar.page.post.py POST /foo/bar
Six files, and the listing tells you what the site does. That stays true as the site grows.
Where to go next
Routing explains exactly how a URL is matched to a file, including URLs with dynamic parts such as
/users/123.Static files shows how to serve images, stylesheets, and scripts.
Handlers covers the request and response objects and everything a handler can ask for.
Hooks shows how to put authentication in one file.
Composing an application adds templates, a database, and sessions.
Building a JSON API puts the pieces together for an API.