YouTip LogoYouTip

Flask Func 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 current active Flask application instance. Available within the application context | * * * ## URL and Redirection | Function | Signature | Description | | --- | --- | --- | | url_for | (endpoint, *, _anchor, _method, _scheme, _external, **values) | Generates a URL based on the view function name. _external=True generates an absolute URL | | redirect | (location, code=303, Response=None) | Creates a redirect response. code defaults to 303 (See Other) | * * * ## Request Abortion and Response Building | Function | Signature | Description | | --- | --- | --- | | abort | (code, *args, **kwargs) | Immediately aborts the request and returns an HTTP error. code is the status code, description is the description | | make_response | (*args) | Converts the view return value into a Response object. When multiple arguments are passed, they are treated as a tuple | | after_this_request | (func) | Registers a function to be executed only once after the current request. Used to dynamically add after_request | * * * ## Context Checks | Function | Description | | --- | --- | | has_request_context() | Returns True if there is an active request context currently | | has_app_context() | Returns True if there is an active application context currently | | copy_current_request_context(func) | Decorator. Copies the current request context so that the decorated function can still access request, g, etc. when running outside the context | * * * ## File Sending | Function | Signature | Description | | --- | --- | --- | | send_file | (path_or_file, mimetype, as_attachment, download_name, conditional, etag, last_modified, max_age) | Sends file content. as_attachment=True triggers a download. Supports conditional requests and ETag | | send_from_directory | (directory, path, **kwargs) | Safely sends a file from the specified directory. Uses safe_join to prevent path traversal attacks | * * * ## Code Examples ## 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 URL login_url = url_for("login") # Generate absolute URL (including domain) full_url = url_for("profile", username="tutorial", _external=True) return f'Login' @app.route("/redirect-me") def redirect_me(): # Redirect to homepage return redirect(url_for("index")) @app.route("/profile/") def profile(username): return f"Profile: {username}" @app.route("/admin") def admin(): # Unauthorized access, return 403 abort(403, description="Access denied") @app.route("/download") def download(): # Send file (trigger browser download) return send_from_directory("uploads","report.pdf", as_attachment=True, download_name="tutorial_report.pdf") @app.route("/custom-response") def custom_response(): # Manually build response object resp = make_response("

Custom

") resp.headers="TUTORIAL" @after_this_request def log_response(response): app.logger.info(f"Request to {request.path} done, status={response.status_code}") return response return resp
← Flask JsonFlask Test Cli Runner Api β†’