# 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: ```bash pip install cylinder waitress ``` Cylinder works with any WSGI server. These pages use [Waitress](https://pypi.org/project/waitress/) because it runs on every platform. ## Step 1: a page Create this layout: ```text 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: ```python 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 /`: ```python def main(response): response.data = "Hello World!" return response ``` Start it from the `my_project` directory: ```bash python cylinder_main.py ``` Open . 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: ```text my_project |-- cylinder_main.py |-- /my_webapps |-- webapp1.page.get.py |-- /webapp1 |-- /foo |-- bar.page.get.py ``` `bar.page.get.py`: ```python def main(response): response.data = "Hello Bar!" return response ``` Open . 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](operations.md#restarting-automatically-during-development) 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: ```text |-- /foo |-- bar.page.get.py |-- bar.page.post.py ``` `bar.page.post.py`: ```python 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](https://werkzeug.palletsprojects.com/en/stable/wrappers/#werkzeug.wrappers.Request). Try it: ```bash 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: ```text |-- /my_webapps |-- webapp1.page.get.py |-- webapp1.hook.late.py ``` `webapp1.hook.late.py`: ```python 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 . 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](routing.md#dynamic-paths) shows how. To use your own `404` page, add an error handler file named after the status code: ```text |-- /my_webapps |-- webapp1.404.py |-- webapp1.page.get.py |-- webapp1.hook.late.py ``` `webapp1.404.py`: ```python 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 ```text 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](routing.md) explains exactly how a URL is matched to a file, including URLs with dynamic parts such as `/users/123`. - [Static files](static-files.md) shows how to serve images, stylesheets, and scripts. - [Handlers](handlers.md) covers the request and response objects and everything a handler can ask for. - [Hooks](hooks.md) shows how to put authentication in one file. - [Composing an application](composition.md) adds templates, a database, and sessions. - [Building a JSON API](json-apis.md) puts the pieces together for an API.