Pyjoyment.Static - Serve static files¶
from Pyjoyment.Static import Static
static = Static()
static.classes.append('MyApp.Controller.Foo')
static.paths.append('/home/sri/public')
# Or import the module, as in Google Python Style Guide
import Pyjoyment.Static
static = Pyjoyment.Static.new()
static.classes.append('MyApp.Controller.Foo')
static.paths.append('/home/sri/public')
Pyjoyment.Static is a static file server with Range,
If-Modified-Since and If-None-Match support, based on
RFC 7232 and
RFC 7233.
A file of paths with a precompressed variant next to it, as
app.css.gz of gzip -k, is served as the variant to the clients whose
Accept-Encoding takes its encoding, with the encodings and the extensions
of encodings. A variant older than its file is ignored, and a
Range request gets the file.
See also¶
Pyjoyment, https://mojolicious.org.
Ported from Mojolicious::Static of Mojolicious 9.49.
- class Pyjoyment.Static.Pyjoyment_Static[source]¶
Pyjoyment.Staticinherits all methods fromPyjo.Baseand implements the following new ones.- asset_dir¶
dir = static.asset_dir static.asset_dir = 'assets'
Subdirectory used for all static assets, defaults to
assets.
- cache_control: str | Callable[[Pyjoyment.Controller.object, str], str | None] | None¶
cache_control = static.cache_control static.cache_control = 'public, max-age=60' static.cache_control = lambda c, rel: 'no-cache' if rel.endswith('.html') else 'public, max-age=60'
Cache-Controlof the files that are served, also with304, unless the response has one already, or a function that gets the controller and the path of the file, relative topathsand withoutprefix, and returns it, orNonefor none, defaults toNone. The files ofimmutableget theirs.# Revalidate every file after a minute static.cache_control = 'public, max-age=60'
- classes¶
classes = static.classes static.classes = ['__main__']
Classes to use for finding files in
DATAsections withPyjo.Loader, first one has the highest precedence, defaults to__main__. Only files with exactly one extension will be used, likeindex.html. Note that for files to be detected, these classes need to have already been loaded and added beforewarmup()is called, which usually happens automatically during application startup.# Add another class with static files in DATA section static.classes.append('Pyjoyment.Plugin.Fun') # Add another class with static files in DATA section and higher precedence static.classes.insert(0, 'Pyjoyment.Plugin.MoreFun')
- dispatch(c: Pyjoyment.Controller.object) bool | None[source]¶
boolean = static.dispatch(Pyjoyment.Controller.new())
Serve static file for
Pyjoyment.Controllerobject.
- encodings¶
encodings = static.encodings static.encodings = {'gzip': '.gz'}
Encodings of the precompressed variants of the files of
pathsand the extensions of their files, in their order of preference, defaults to{'br': '.br', 'zstd': '.zst', 'gzip': '.gz'}, asbrotli -k,zstd -kandgzip -kwrite them. A request whoseAccept-Encodingtakes an encoding gets the variant with itsContent-Encoding, and theContent-Typeof the file.# Serve the files as they are only static.encodings = {}
- extra¶
extra = static.extra static.extra = {'foo/bar.txt': '/home/sri/myapp/bar.txt'}
Paths for extra files to be served from locations other than
paths, such as the images used by the built-in exception and not found pages. Note that extra files are only served if no better alternative could be found inpathsandclasses.# Remove built-in favicon del static.extra['favicon.ico']
- file(rel: str) Pyjo_Asset_File | Pyjo_Asset_Memory | None[source]¶
asset = static.file('images/logo.png') asset = static.file('../lib/MyApp.py')
Build
Pyjo.Asset.FileorPyjo.Asset.Memoryobject for a file, relative topathsor fromclasses, or returnNoneif it doesn’t exist. Note that this method uses a relative path, but does not protect from traversing to parent directories.content = static.file('foo/bar.html').slurp()
- file_path(file: str) str[source]¶
path = static.file_path('/index.html')
Get static file path with
prefixif it has been configured.
- immutable: str | re.Pattern[str] | None¶
immutable = static.immutable static.immutable = r'\.[0-9a-f]{12}\.[^/.]+$'
Regular expression of the paths of the files, relative to
pathsand withoutprefix, whose names have a hash of their content, as the ones of a bundler, and never change, so that they are served withCache-Control: public, max-age=31536000, immutablein place ofcache_control, defaults toNone. The assets of thedevelopmentmode are served withCache-Control: no-cachestill.# The names of ManifestStaticFilesStorage of Django static.immutable = re.compile(r'\.[0-9a-f]{12}\.[^/.]+$')
- is_fresh(c: Pyjoyment.Controller.object, *, etag: str | None = None, last_modified: float | None = None) bool[source]¶
boolean = static.is_fresh(Pyjoyment.Controller.new(), etag='abc') boolean = static.is_fresh(Pyjoyment.Controller.new(), etag='W/"def"')
Check freshness of request by comparing the
If-None-MatchandIf-Modified-Sincerequest headers to theETagandLast-Modifiedresponse headers.These options are currently available:
etag
etag='abc' etag='W/"abc"'
Add
ETagheader before comparing.last_modified
last_modified=epoch
Add
Last-Modifiedheader before comparing.
- paths: list[str]¶
paths = static.paths static.paths = ['/home/sri/public']
Directories to serve static files from, first one has the highest precedence.
# Add another "public" directory static.paths.append('/home/sri/public') # Add another "public" directory with higher precedence static.paths.insert(0, '/home/sri/themes/blue/public')
- prefix: str | None¶
prefix = static.prefix static.prefix = '/static'
Prefix to use for all static files, defaults to
None. This can be very useful for production deployments where the reverse proxy server should take over serving static files.
- serve(c: Pyjoyment.Controller.object, rel: str) bool | None[source]¶
boolean = static.serve(Pyjoyment.Controller.new(), 'images/logo.png') boolean = static.serve(Pyjoyment.Controller.new(), '../lib/MyApp.py')
Serve a specific file, relative to
pathsor fromclasses. Note that this method uses a relative path, but does not protect from traversing to parent directories.
- serve_asset(c: Pyjoyment.Controller.object, asset: Pyjo_Asset) Any[source]¶
static.serve_asset(Pyjoyment.Controller.new(), Pyjo.Asset.File.new())
Serve a
Pyjo.Asset.FileorPyjo.Asset.Memoryobject withRange,If-Modified-SinceandIf-None-Matchsupport.