Reference
Everything on one page, for looking things up. Each entry links to the page that explains it.
What is promised
From 1.0, Cylinder follows semantic versioning. What this page lists is what it
promises: a release that changes any of it in a way that an app could notice has a new major number. Every
other name in cylinder.py is part of how Cylinder works, and can change in any release. So can the words
of Cylinder’s own log lines and error pages.
Adding a parameter that Cylinder passes is a change too. No extra or placeholder may have the name of one, so a new parameter could take a name that an app already uses. A parameter is added only in a release with a new major number.
The request and response that a handler is passed are Werkzeug’s own, and what they offer is Werkzeug’s
to change. A Werkzeug with a new minor number can take away what an earlier one deprecated. A release of
Cylinder that allows such a Werkzeug has a new minor number, and its changelog says so. Werkzeug warns
about a call for a release or more before it takes the call away. See Requirements.
A release that needs a newer Python or a newer Werkzeug than the one before it has a new minor number too, and its changelog says so. pip does not install a release where what it needs is missing, so no app that is installed stops working for it.
Filenames
A webapp named webapp1 in the directory my_webapps consists of the directory my_webapps/webapp1 and
the files beside it whose names start with webapp1.
Filename pattern |
Example |
What it is |
|---|---|---|
|
|
Page handler for one HTTP method |
|
|
Page handler for any method without a file of its own |
|
|
Page handler for its own path and every path beneath |
|
|
The same, for any method without a file of its own |
|
|
Early hook: runs before the page handler |
|
|
Late hook: runs after the page handler |
|
|
Error handler for a status |
anything else |
|
A file, sent only where a handler serves it |
<name>is the last segment of the URL path, or the webapp’s name for files at the root of the site.A
<name>in braces, such as{user_id}, is a placeholder: the file or directory answers for any one segment, and the segment is passed in as the parameteruser_id. What is in the braces must be a Python identifier, and no other parameter may have its name.<method>is the HTTP method in lowercase. It must be alphabetic.defaultis reserved.A
.page.handler namedfooserves/fooand nothing beneath it. A.tree.page handler namedfooserves/fooand every path beneath it that has no closer match.A hook named
foocovers/fooand every path beneath it, together with the hooks above and below it. It is not named for a method, and runs for every one.Every page handler and hook ends in three parts, of which the middle one says what the file is:
page,tree, orhook. An error handler ends in its status. So a filename can be read in one way only.An error handler named
foocovers errors raised for/fooand every path beneath it.
How a request is matched
Rule |
Details |
|---|---|
A deeper path wins over a shallower one |
|
At the same path, an exact method wins over |
|
For |
|
A |
|
At the same path and method, |
|
A name wins over a placeholder, and one placeholder is allowed per directory |
|
A file is sent only where a page handler serves files |
|
Hidden paths, handler files, and copies of handler files are never sent |
|
Nothing is found through a symbolic link or a Windows junction |
|
Matching is case-sensitive on every operating system |
|
A URL is served in one spelling: no stray slash, |
|
Every hook from the site root to the URL runs, one of each kind per path |
|
The most specific error handler for the status code runs |
|
No page at a URL gives |
The order a request runs in
app_map()chooses the webapp and supplies extra parameters.The early hooks run, from the site root down to the URL.
The page handler runs. If no page handler serves the request, a
404,405, or501is raised here.If anything raised an HTTP error, the matching error handler runs. An uncaught exception is a
500, and its traceback is logged first. A redirect andabort(response)have no error handler.The late hooks run, from the URL back up to the site root.
The response is sent.
Functions registered with
response.call_on_close()run.
A file is run from its first line when its turn comes, and not before. So none of the code in a page handler’s file runs for a request that an early hook turned away. Each request runs a handler file as a new module, so nothing at the top of one is shared between requests: see What belongs at the top of a handler file.
Handler parameters
A handler, hook, or error handler is a function named main(). It receives the parameters it names.
Parameter |
Value |
|---|---|
|
The Werkzeug |
|
The Werkzeug |
|
A logger whose records carry the request ID |
|
|
|
List of URL path segments after the page handler’s own path |
|
The exception being handled, or |
|
The ID of the request that its log records carry, a |
placeholder |
The URL segment that a placeholder on the path stands for, as a |
extras |
Any key of the dictionary returned as the third value from |
Parameters are matched by name, in any order. None is required. A parameter after a
*is passed by name, and*argsand**kwargsare passed nothing.abort(response)ends the request with the response as it is: no error handler runs for it.abort()takes a status that Werkzeug has a class for, a redirect, or a status added withabort_extra. Anything else is aLookupError, which becomes a500.The argument after the status is the URL of a redirect, and the description of any other status, whatever its class takes first. Anything else that a class takes, such as the
valid_methodsof a405, is passed by name.An error or a redirect keeps the headers and cookies that were set before it, except the ones that describe the body that it replaces: see What survives an abort.
No parameter affects routing.
remainderis[]for a.page.handler.Asking for a name that is not available raises
TypeError, unless the parameter has a default value.app_map()can ask forrequest,response,logger,abort, andrequest_id.Only a page handler can read the request body.
app_map(), a hook, or an error handler has to setrequest.shallow = Falsefirst.
See Handlers.
Return values
A handler returns |
Result |
|---|---|
The |
Sent as it is |
A |
Becomes the body of the |
Anything else |
|
app_map() returns (site_dir, site_name) or (site_dir, site_name, extras), as a tuple or a list.
Anything else is a ValueError, which becomes a 500. Its line in the log says what app_map() must
return, and the types of what it returned.
site_diris the directory that holds the webapps: an absolute path, or one relative to the working directory. Astror apathlib.Path. One that is not a directory is aValueError, which becomes a500, and its line in the log names the path.site_nameis the name of the webapp: one name, and not a path. It is looked up insite_diras it is. One that names nothing there, such as a misspelled name or a path, is aValueError, which becomes a500, and its line in the log names it. The directories on the way to the webapp go intosite_dir.extrasis a dictionary of extra parameters, orNone.
get_app options
cylinder.get_app(
app_map,
*,
log_level=logging.DEBUG,
log_handler=None,
request_id_header=None,
abort_extra=None,
)
Option |
Default |
Purpose |
|---|---|---|
|
required |
Function that chooses the webapp for each request |
|
|
Level of the app’s logger |
|
stream to stderr |
Any |
|
|
Header that carries the request ID |
|
|
Extra status codes for |
Every option but app_map is passed by name.
Every app logs through the logger named cylinder. Its records carry two fields beside the standard ones,
which a formatter can use: request_id and timestamp. See Where logs go.
get_app() returns the WSGI application. It has these attributes:
Attribute |
Purpose |
|---|---|
|
Returns a Werkzeug test client |
|
The app’s logger |
The file server
cylinder.static_file(request, response, abort) is a page handler that sends the file at the path of the
URL, or aborts with a 404 where there is none to send. A handler file uses it as its main(), with the
one line from cylinder import static_file as main, or calls it from a main() of its own. See
Static files.
Status codes Cylinder produces itself
Status |
When |
|---|---|
|
An uncaught exception, an invalid return value, or an unavailable parameter |
|
Two placeholders in one directory, or one with the name of a parameter |
|
An extra from |
|
A |
|
A |
|
|
|
A response that cannot be sent |
|
The URL has no page handler for any method, or |
|
The URL has one, but not for the method of the request |
|
An HTTP method that is not alphabetic, or the reserved method |
|
|
others |
Whatever a handler passes to |
Redirect statuses supported by abort(): 301, 302, 303, 307, 308. Each needs the URL to send the
client to, as a str: without one, with one that Werkzeug cannot read, such as //[, or with a line break
in it, the redirect is a 500.
Requirements
Python 3.11 or newer
Werkzeug 3.1.7 or newer, and older than 3.2, which is installed with Cylinder
A WSGI server of your choice