class documentation

class SharedDataMiddleware:

View In Hierarchy

A WSGI middleware which provides static content for development environments or simple server setups. Its usage is quite simple:

import os
from werkzeug.middleware.shared_data import SharedDataMiddleware

app = SharedDataMiddleware(app, {
    '/shared': os.path.join(os.path.dirname(__file__), 'shared')
})

The contents of the folder ./shared will now be available on http://example.com/shared/. This is pretty useful during development because a standalone media server is not required. Files can also be mounted on the root folder and still continue to use the application because the shared data middleware forwards all unhandled requests to the application, even if the requests are below one of the shared folders.

If pkg_resources is available you can also tell the middleware to serve files from package data:

app = SharedDataMiddleware(app, {
    '/static': ('myapplication', 'static')
})

This will then serve the static folder in the myapplication Python package.

The optional disallow parameter can be a list of ~fnmatch.fnmatch rules for files that are not accessible from the web. If cache is set to False no caching headers are sent.

Currently the middleware does not support non-ASCII filenames. If the encoding on the file system happens to match the encoding of the URI it may work but this could also be by accident. We strongly suggest using ASCII only file names for static files.

The middleware will guess the mimetype using the Python mimetype module. If it's unable to figure out the charset it will fall back to fallback_mimetype.

Changed in version 1.0: The default fallback_mimetype is application/octet-stream. If a filename looks like a text mimetype, the utf-8 charset is added to it.
New in version 0.6: Added fallback_mimetype.
Changed in version 0.5: Added cache_timeout.
Parameters
appthe application to wrap. If you don't want to wrap an application you can pass it NotFound.
exportsa list or dict of exported files and folders.
disallowa list of ~fnmatch.fnmatch rules.
cacheenable or disable caching headers.
cache​_timeoutthe cache timeout in seconds for the headers.
fallback​_mimetypeThe fallback mimetype for unknown files.
Method __call__ Undocumented
Method __init__ Undocumented
Method generate​_etag Undocumented
Method get​_directory​_loader Undocumented
Method get​_file​_loader Undocumented
Method get​_package​_loader Undocumented
Method is​_allowed Subclasses can override this method to disallow the access to certain files. However by providing disallow in the constructor this method is overwritten.
Instance Variable app Undocumented
Instance Variable cache Undocumented
Instance Variable cache​_timeout Undocumented
Instance Variable exports Undocumented
Instance Variable fallback​_mimetype Undocumented
Method ​_opener Undocumented
def __call__(self, environ, start_response):

Undocumented

Parameters
environ:WSGIEnvironmentUndocumented
start​_response:StartResponseUndocumented
Returns
t.Iterable[bytes]Undocumented
def __init__(self, app, exports, disallow=None, cache=True, cache_timeout=(60*60)*12, fallback_mimetype='application/octet-stream'):

Undocumented

Parameters
app:WSGIApplicationUndocumented
exports:t.Union[t.Dict[str, t.Union[str, t.Tuple[str, str]]], t.Iterable[t.Tuple[str, t.Union[str, t.Tuple[str, str]]]]]Undocumented
disallow:NoneUndocumented
cache:boolUndocumented
cache​_timeout:intUndocumented
fallback​_mimetype:strUndocumented
def generate_etag(self, mtime, file_size, real_filename):

Undocumented

Parameters
mtime:datetimeUndocumented
file​_size:intUndocumented
real​_filename:strUndocumented
Returns
strUndocumented
def get_directory_loader(self, directory):

Undocumented

Parameters
directory:strUndocumented
Returns
_TLoaderUndocumented
def get_file_loader(self, filename):

Undocumented

Parameters
filename:strUndocumented
Returns
_TLoaderUndocumented
def get_package_loader(self, package, package_path):

Undocumented

Parameters
package:strUndocumented
package​_path:strUndocumented
Returns
_TLoaderUndocumented
def is_allowed(self, filename):
Subclasses can override this method to disallow the access to certain files. However by providing disallow in the constructor this method is overwritten.
Parameters
filename:strUndocumented
Returns
boolUndocumented
app =

Undocumented

cache =

Undocumented

cache_timeout =

Undocumented

exports: t.List[t.Tuple[str, _TLoader]] =

Undocumented

fallback_mimetype =

Undocumented

def _opener(self, filename):

Undocumented

Parameters
filename:strUndocumented
Returns
_TOpenerUndocumented