Undocumented
Class | ArgumentValidationError |
Raised if validate_arguments fails to validate |
Class | cached_property |
No summary |
Class | environ_property |
Maps request attributes to environment variables. This works not only for the Werkzeug request object, but also any other class with an environ attribute: |
Class | header_property |
Like environ_property but for headers. |
Class | HTMLBuilder |
Helper object for HTML generation. |
Class | ImportStringError |
Provides information about a failed import_string attempt. |
Function | append_slash_redirect |
Redirects to the same URL but with a slash appended. The behavior of this function is undefined if the path ends with a slash already. |
Function | bind_arguments |
No summary |
Function | detect_utf_encoding |
Detect which UTF encoding was used to encode the given bytes. |
Function | escape |
Replace &, <, >, ", and ' with HTML-safe sequences. |
Function | find_modules |
No summary |
Function | format_string |
String-template format a string: |
Function | get_content_type |
Returns the full content type string with charset for a mimetype. |
Function | import_string |
No summary |
Function | invalidate_cached_property |
Invalidates the cache for a cached_property : |
Function | redirect |
No summary |
Function | secure_filename |
No summary |
Function | send_file |
Send the contents of a file to the client. |
Function | send_from_directory |
Send a file from within a directory using send_file . |
Function | unescape |
The reverse of escape . This unescapes all the HTML entities, not only those inserted by escape. |
Function | validate_arguments |
No summary |
Variable | html |
Undocumented |
Variable | xhtml |
Undocumented |
Constant | _T |
Undocumented |
Variable | _charset_mimetypes |
Undocumented |
Variable | _entity_re |
Undocumented |
Variable | _filename_ascii_strip_re |
Undocumented |
Variable | _windows_device_files |
Undocumented |
Parameters | |
environ:WSGIEnvironment | the WSGI environment for the request that triggers the redirect. |
code:int | the status code for the redirect. |
Returns | |
Response | Undocumented |
Bind the arguments provided into a dict. When passed a function,
a tuple of arguments and a dict of keyword arguments bind_arguments
returns a dict of names as the function would see it. This can be useful
to implement a cache decorator that uses the function arguments to build
the cache key based on the values of the arguments.
Signature.bind
instead.Parameters | |
func | the function the arguments should be bound for. |
args | tuple of positional arguments. |
kwargs | a dict of keyword arguments. |
Returns | |
a dict of bound keyword arguments. |
Detect which UTF encoding was used to encode the given bytes.
The latest JSON standard (RFC 8259) suggests that only UTF-8 is accepted. Older documents allowed 8, 16, or 32. 16 and 32 can be big or little endian. Some editors or libraries may prepend a BOM.
json.loads
.Parameters | |
data:bytes | Bytes in unknown UTF encoding. |
Returns | |
str | UTF encoding name |
Unknown Field: internal | |
Replace &, <, >, ", and ' with HTML-safe sequences.
None is escaped to an empty string.
Parameters | |
s:t.Any | Undocumented |
Returns | |
str | Undocumented |
Finds all the modules below a package. This can be useful to automatically import all views / controllers so that their metaclasses / function decorators have a chance to register themselves on the application.
Packages are not returned unless include_packages
is True
. This can
also recursively list modules but in that case it will import all the
packages to get the correct load path of that module.
Parameters | |
import_path:str | the dotted name for the package to find child modules. |
include_packages:bool | set to True if packages should be returned, too. |
recursive:bool | set to True if recursion should happen. |
Returns | |
t.Iterator[ | generator |
String-template format a string:
>>> format_string('$foo and ${foo}s', dict(foo=42)) '42 and 42s'
This does not do any attribute lookup.
string.Template
instead.Parameters | |
string:str | the format string. |
context:t.Mapping[ | a dict with the variables to insert. |
Returns | |
str | Undocumented |
Returns the full content type string with charset for a mimetype.
If the mimetype represents text, the charset parameter will be appended, otherwise the mimetype is returned unchanged.
Parameters | |
mimetype:str | The mimetype to be used as content type. |
charset:str | The charset to be appended for text mimetypes. |
Returns | |
str | The content type. |
Imports an object based on a string. This is useful if you want to use import paths as endpoints or something similar. An import path can be specified either in dotted notation (xml.sax.saxutils.escape) or with a colon as object delimiter (xml.sax.saxutils:escape).
If silent
is True the return value will be None
if the import fails.
Parameters | |
import_name:str | the dotted name for the object to import. |
silent:bool | if set to True import errors are ignored and
None is returned instead. |
Returns | |
t.Any | imported object |
Invalidates the cache for a cached_property
:
>>> class Test(object): ... @cached_property ... def magic_number(self): ... print("recalculating...") ... return 42 ... >>> var = Test() >>> var.magic_number recalculating... 42 >>> var.magic_number 42 >>> invalidate_cached_property(var, "magic_number") >>> var.magic_number recalculating... 42
You must pass the name of the cached property as the second argument.
Parameters | |
obj:object | Undocumented |
name:str | Undocumented |
Returns a response object (a WSGI application) that, if called, redirects the client to the target location. Supported codes are 301, 302, 303, 305, 307, and 308. 300 is not supported because it's not a real redirect and 304 because it's the answer for a request with a request with defined If-Modified-Since headers.
iri_to_uri
function.Parameters | |
location:str | the location the response should redirect to. |
code:int | the redirect status code. defaults to 302. |
Response:t.Optional[ | Undocumented |
class Response | a Response class to use when instantiating a
response. The default is werkzeug.wrappers.Response if
unspecified. |
Returns | |
Response | Undocumented |
Pass it a filename and it will return a secure version of it. This
filename can then safely be stored on a regular file system and passed
to os.path.join
. The filename returned is an ASCII only string
for maximum portability.
On windows systems the function also makes sure that the file is not named after one of the special device files.
>>> secure_filename("My cool movie.mov") 'My_cool_movie.mov' >>> secure_filename("../../../etc/passwd") 'etc_passwd' >>> secure_filename('i contain cool \xfcml\xe4uts.txt') 'i_contain_cool_umlauts.txt'
The function might return an empty filename. It's your responsibility to ensure that the filename is unique and that you abort or generate a random filename if the function returned an empty one.
Parameters | |
filename:str | the filename to secure |
Returns | |
str | Undocumented |
Send the contents of a file to the client.
The first argument can be a file path or a file-like object. Paths
are preferred in most cases because Werkzeug can manage the file and
get extra information from the path. Passing a file-like object
requires that the file is opened in binary mode, and is mostly
useful when building a file in memory with io.BytesIO
.
Never pass file paths provided by a user. The path is assumed to be trusted, so a user could craft a path to access a file you didn't intend.
If the WSGI server sets a file_wrapper in environ, it is used, otherwise Werkzeug's built-in wrapper is used. Alternatively, if the HTTP server supports X-Sendfile, use_x_sendfile=True will tell the server to send the given path, which is much more efficient than reading it in Python.
Parameters | |
path_or_file:t.Union[ | The path to the file to send, relative to the current working directory if a relative path is given. Alternatively, a file-like object opened in binary mode. Make sure the file pointer is seeked to the start of the data. |
environ:WSGIEnvironment | The WSGI environ for the current request. |
mimetype:t.Optional[ | The MIME type to send for the file. If not provided, it will try to detect it from the file name. |
as_attachment:bool | Indicate to a browser that it should offer to save the file instead of displaying it. |
download_name:t.Optional[ | The default name browsers will use when saving the file. Defaults to the passed file name. |
conditional:bool | Enable conditional and range responses based on request headers. Requires passing a file path and environ. |
etag:t.Union[ | Calculate an ETag for the file, which requires passing a file path. Can also be a string to use instead. |
last_modified:t.Optional[ | The last modified time to send for the file, in seconds. If not provided, it will try to detect it from the file path. |
max_age:t.Optional[ | How long the client should cache the file, in seconds. If set, Cache-Control will be public, otherwise it will be no-cache to prefer conditional caching. |
use_x_sendfile:bool | Set the X-Sendfile header to let the server to efficiently send the file. Requires support from the HTTP server. Requires passing a file path. |
response_class:t.Optional[ | Build the response using this class. Defaults
to ~werkzeug.wrappers.Response . |
_root_path:t.Optional[ | Do not use. For internal use only. Use
send_from_directory to safely send files under a path. |
Returns | |
Response | Undocumented |
Send a file from within a directory using send_file
.
This is a secure way to serve files from a folder, such as static
files or uploads. Uses ~werkzeug.security.safe_join
to
ensure the path coming from the client is not maliciously crafted to
point outside the specified directory.
If the final path does not point to an existing regular file,
returns a 404 ~werkzeug.exceptions.NotFound
error.
Parameters | |
directory:t.Union[ | The directory that path must be located under. |
path:t.Union[ | The path to the file to send, relative to directory. |
environ:WSGIEnvironment | The WSGI environ for the current request. |
**kwargs:t.Any | Arguments to pass to send_file . |
Returns | |
Response | Undocumented |
The reverse of escape
. This unescapes all the HTML
entities, not only those inserted by escape.
Parameters | |
s:str | Undocumented |
Returns | |
str | Undocumented |
Checks if the function accepts the arguments and keyword arguments.
Returns a new (args, kwargs) tuple that can safely be passed to
the function without causing a TypeError
because the function signature
is incompatible. If drop_extra
is set to True
(which is the default)
any extra positional or keyword arguments are dropped automatically.
The exception raised provides three attributes:
missing
extra
extra_positional
This can be useful for decorators that forward user submitted data to a view function:
from werkzeug.utils import ArgumentValidationError, validate_arguments def sanitize(f): def proxy(request): data = request.values.to_dict() try: args, kwargs = validate_arguments(f, (request,), data) except ArgumentValidationError: raise BadRequest('The browser failed to transmit all ' 'the data expected.') return f(*args, **kwargs) return proxy
inspect.signature
instead.Parameters | |
func | the function the validation is performed against. |
args | a tuple of positional arguments. |
kwargs | a dict of keyword arguments. |
drop_extra | set to False if you don't want extra arguments
to be silently dropped. |
Returns | |
tuple in the form (args, kwargs). |