The static route helper provides a more Pylons-like way to serve static files compared to Pyramid’s standard static view. In Pylons, the static app is an overlay on “/”, so that it looks first for a file under the static directory, and if no file exists, it falls back to the dynamic application. This static route helper works the same way: it works as an overlay on “/”, so the route matches only if the file exists, otherwise Pyramid falls through to the next route. The difference is that Pylons’ static app is a WSGI middleware, while the static route helper registers an ordinary route and a view. By convention you put the static route last in the route list, but it can go anywhere in the list.
The static route helper does have some disadvantages compared to Pyramid’s static view. (1) There’s no spiffy method to generate URLs to them. (2) You can’t switch to a static media server and configure the nonexistent spiffy method to generate external URLs to it. (3) You can’t override assets.
For completeness, we’ll mention that if you’re using Pyramid’s static view, there are a couple workarounds for serving top-level URLs or disjoint URLs. (1) Use an ordinary route and view to serve a static file. (2) Use the “pyramid_assetviews” package on PyPI to serve top-level files. So you can weigh these alternatives against the static route helper. I (the static route author) am now undecided, so I can’t definitively say which way is better. The demo app uses it mainly so that you can see it in action.
# In main(). config.include("akhet.static") config.add_static_route("myapp", "static")
Register a route and view to serve any URL if a corresponding file exists under the static directory. If the file doesn’t exist, the route will fail and Pyramid will continue down the route list.
The API is from Pyramid’s early days, so it makes an asset spec out of package and subdir for you and doesn’t allow you to supply your own. It also searches only a single directory rather than a search path. These limitations may be relaxed in a future version.
The include module is now “akhet.static”; in version 1 it was “akhet”. A backward compatibility shim is in place.