Cylinder

A small WSGI web framework where the filesystem is the routing table.

Cylinder is built on Werkzeug and adds one opinion: a URL is handled by the file whose path matches it. To find the code behind /foo/bar, open foo/bar. There is no route table to maintain, no decorators to hunt for, and no application object to learn.

my_webapps
    |-- webapp1.page.get.py            GET /
    |-- /webapp1
        |-- /foo
            |-- bar.page.get.py        GET /foo/bar
            |-- bar.page.post.py       POST /foo/bar
        |-- assets.tree.get.py         GET /assets/logo.png, and every file beside it
        |-- /assets
            |-- logo.png
# bar.page.get.py
def main(request, response):
    response.data = f"Hello from {request.path}"
    return response

That is a working page. Save the file and reload the browser; there is nothing to restart.

New here? Start with the Quickstart. It takes about five minutes.

Why Cylinder

Flask and FastAPI attach a URL to a function with a decorator, and Django lists its URLs in code. Either way, you find the code behind a URL by searching for it. Cylinder has nothing to register: the directory tree is the list of routes.

Most Python web frameworks also ask you to choose. Microframeworks stay out of your way and leave every team to invent its own project layout. Full stack frameworks give you a layout, along with layers and conventions you may not want. Cylinder sits between them.

  • Your project tree is your site map. Anyone can see which URLs exist, and where their code lives, from a directory listing. A new teammate can find any page without asking, and without searching.

  • No magic. A handler is a plain function that receives Werkzeug’s own request and response. There is no application context, no global request to import, and nothing to subclass. What a handler depends on is written in its signature.

  • No restart while you work. Handler files are loaded when a request arrives, so an edit takes effect on the next request, with no file watcher or development mode. For the modules they import, a reloading server restarts the app for you.

  • Shared logic lives in the tree too. Put an authentication check or a set of headers in one hook file and it applies to every page in that directory. Error pages work the same way.

  • Small enough to read. The framework is a single file with less than 450 lines of code. When you want to know what it does, you can read it.

  • Nothing to outgrow. Templates, databases, and sessions come from the libraries you already use, and you pass them to your handlers explicitly. Cylinder does not wrap them.

It grows by adding files

“Small” describes the framework, not what you can build with it. The model is the one PHP made familiar: a URL maps to the file that answers it. Very large applications have been built that way. Cylinder keeps the model and passes every dependency in explicitly, where PHP reached for globals.

What makes a large codebase hard is needing to understand all of it to change any of it. Cylinder keeps each page a separate, small problem:

  • You can work out where any page’s code is. The handler for /store/orders is store/orders.page.get.py, in a project with ten pages or ten thousand. There are no decorators or URL configuration to trace.

  • A change touches one small file. A handler is one function, and its signature lists everything it uses. Reviewing a change to a page means reading that page.

  • Nothing central grows. Adding a page is adding a file. There is no route table or registration module that every feature edits and every merge collides in.

  • Routing cost does not grow with the site. Matching a request depends on how deep the URL is, not on how many pages exist.

Large applications shows how to lay out a big project.

These properties help people, and they help AI coding assistants even more, because an assistant works with limited context. See AI assistants.

Compared with other file-based routers

File-based routing is best known from JavaScript, in Next.js, SvelteKit, and Nuxt, and from PHP before them. For Python it mostly comes as a plugin that adds a directory of routes to Flask, FastAPI, or Django. Cylinder is a framework that is built on the idea, and it differs in these ways:

  • There is no build step. No route manifest is compiled and no code is generated. Cylinder reads the directory when a request arrives, so a file that you add is a URL on the next request.

  • A file is named for the path and the method that it serves. payments.page.post.py is the code for a POST to /payments, where Next.js has a page.tsx in every directory and SvelteKit a +page.svelte. A listing shows which methods a URL answers, and any other method gets a 405 with no code. See Why there are no index files.

  • Shared logic is layered, and a page cannot skip it. A hook covers everything beneath it, and every hook between the site root and the URL runs. A login check at /admin cannot be switched off by a file deeper in the tree. Many routers have one middleware file for the whole application, and it has to match paths itself.

  • An error page is a file in the tree too. webapp1.404.py builds every 404 on the site, a file deeper in the tree takes over for one section, and any error status can have one.

  • Static files follow the same rules as pages. A file is sent only where a handler sends it, where PHP made every file under the web root public. The hooks above a file run before it is sent, so the login check that covers the pages of a section covers its downloads too.

  • A file answers to one URL. Cylinder looks each name up in a listing of its directory, and never asks the filesystem for it. So where the filesystem ignores letter case, as on Windows and macOS, /ADMIN/report does not reach a page past the hook that was written for /admin. Nothing is found through a symbolic link either.

  • A handler file keeps nothing between requests. It is run again for each request, so a variable at the top of one is never shared between two visitors.

When Cylinder is a good fit

Cylinder suits anything that answers HTTP requests with pages or data: server-rendered sites, internal tools, JSON APIs, and large applications made of all three. One process can serve several sites, each in its own directory.

A tour of the documentation

Each part of the documentation is a short page. This tour says what is in each one.

Routing basics

How a URL finds its file. Read Routing after the Quickstart.

Method files. bar.page.get.py answers GET, bar.page.post.py answers POST, and bar.page.default.py answers everything else. HEAD works without any code.

Static files. A file is sent only where a handler sends it, and Cylinder comes with that handler: a one-line assets.tree.get.py serves every file under /assets. A file that lands anywhere else in the tree by mistake is not public.

Paths with dynamic parts. A name in braces stands for any value, so users/{user_id}.page.get.py answers /users/123 and is passed user_id. A .tree. handler answers for a whole section. Every other handler serves its own path only, so a URL nobody wrote a page for is a 404.

Handlers

What a handler is given, and what it returns. See Handlers for the request and response objects, the parameters a handler can ask for, and the two rules Cylinder enforces.

Hooks

Code that runs before or after every handler in a directory. See Hooks for early and late hooks, and for a worked authentication example.

Error handling

How a request stops early, and what the client sees. See Errors and redirects.

abort(). Stop a request with any HTTP status.

Redirects. abort(302, "/login") sends the client elsewhere.

Custom error pages. A file named webapp1.404.py builds every 404 response for the site, and a file deeper in the tree can override it for one section.

Application composition

How to give your handlers the things they need. See Composing an application.

Jinja2 templates. Create the environment once and hand a render_template function to your handlers.

SQLAlchemy. Pass a session factory in the same way.

Sessions. A complete server-side session in about thirty lines.

Configuration. Configuration is ordinary Python, including serving different sites for different hostnames.

There is also a page on building a JSON API, which puts routing, hooks, and error handlers together, and one on large applications.

Operations and development

Running, testing, and deploying. See Testing, Operations, and Deployment.

Testing. Every app has a test client, and it works with pytest.

Logging. Every log record carries the request ID.

Streaming. Send or receive a body in pieces.

Thread safety. Share state between requests safely.

Deployment. WSGI and ASGI servers, reverse proxies, more threads for requests that wait, and a process for each core.

Reference and project pages

  • Reference: every filename pattern, parameter, and option on one page

  • Security: what to check before putting an app on the internet

  • Changelog: what changed in each release

  • Design notes: why the framework itself is written the way it is

  • Contributing: how to work on Cylinder itself

Using Cylinder with an AI coding assistant

Cylinder publishes a skill file that teaches an AI coding assistant how to write Cylinder apps. AI assistants says how to install it.

About the project

Plasma is the nonprofit software foundation that supports the ongoing development of Cylinder. The Plasma Foundation Inc. is a 501(c)(3) founded by Tier2 Technologies to support software that benefits the world at large. Cylinder is released under the Apache License 2.0.