Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Flask Utility Functions and Classes API

Flask Utility Functions and Classes API

Flask provides a series of globally available functions for common operations such as generating URLs, aborting requests, and building responses.

All are imported via from flask import ...


Global Proxy Objects

object Type Description
current_app Proxy → Flask Points to the currently active Flask application instance. Available within the application context

URLs and Redirects

function Signing Description
url_for (endpoint, *, _anchor, _method, _scheme, _external, **values) Generate a URL based on a view function name. _external=True generates an absolute URL
redirect (location, code=303, Response=None) Create a redirect response. code defaults to 303 (See Other)

Request Abortion and Response Building

function Signing Description
abort (code, *args, **kwargs) Abort the request immediately and return an HTTP error. code is the status code, description is the description
make_response (*args) Convert a view's return value into a Response object. When multiple arguments are passed, they are treated as a tuple
after_this_request (func) Register a function to be executed only once after the current request. Used to dynamically add an after_request

Context Checks

function Description
has_request_context() Returns True if there is currently an active request context
has_app_context() Returns True if there is currently an active application context
copy_current_request_context(func) Decorator. Copies the current request context so that the decorated function can still access request, g, etc. when run outside the context

File Sending

function Signing Description
send_file (path_or_file, mimetype, as_attachment, download_name, conditional, etag, last_modified, max_age) Send file contents. as_attachment=True triggers a download. Supports conditional requests and ETags
send_from_directory (directory, path, **kwargs) Safely send a file from a specified directory. Uses safe_join to prevent path traversal attacks

Code Example

Example

from

flask

import

(

Flask

,

current_app

,

url_for

,

redirect

,

                   abort

,

make_response

,

after_this_request

,

                   has_request_context

,

send_file

,

send_from_directory

)

app

=

Flask

(

name

)

@

app.

route

(

"/"

)

def

index

(

)

:

   

url_for Generates URLs

    login_url

=

url_for

(

"login"

)

   

Generate an absolute URL (including the domain)

    full_url

=

url_for

(

"profile"

,

username

=

"runoob"

,

_external

=

True

)

   

return

f

'Login'

@

app.

route

(

"/redirect-me"

)

def

redirect_me

(

)

:

   

Redirect to the homepage

   

return

redirect

(

url_for

(

"index"

)

)

@

app.

route

(

"/profile/"

)

def

profile

(

username

)

:

   

return

f

"Profile: {username}"

@

app.

route

(

"/admin"

)

def

admin

(

)

:

   

Unauthorized access, returns 403

    abort

(

403

,

description

=

"Access denied"

)

@

app.

route

(

"/download"

)

def

download

(

)

:

   

Send a file (triggers browser download)

   

return

send_from_directory

(

"uploads"

,

"report.pdf"

,

                               as_attachment

=

True

,

                               download_name

=

"runoob_report.pdf"

)

@

app.

route

(

"/custom-response"

)

def

custom_response

(

)

:

   

Manually build a Response object

    resp

=

make_response

(

"

Custom

"

)

    resp.

headers

[

"X-Powered-By"

]

=

"RUNOOB"

   

@

after_this_request

   

def

log_response

(

response

)

:

        app.

logger

.

info

(

f

"Request to {request.path} done, status={response.status_code}"

)

       

return

response

   

return

resp