Flask - sgml/signature GitHub Wiki

Flask itself just bridges to Werkzeug to implement a proper WSGI application and to Jinja2 to handle templating.

A view function is a python method decorated by app.route

The flask object implements a WSGI application

Blueprints record operations to execute before or after route changes. Flask associates view functions with blueprints for reusability.

flask.g is the global object used to store user-defined data across requests

Flask uses thread-local objects internally so that you don’t have to pass objects around from function to function

Context Checking

"""
This modulino demonstrates safe usage of flask.g
within a request context, while logging the context state.

We import Flask and its context helpers:
- Flask: the application object
- g: the context-local storage
- has_app_context / has_request_context: booleans to check context state

We also configure logging to observe context state before using g.
"""

from flask import Flask, g, has_app_context, has_request_context
import logging

# Create the Flask application
app = Flask(__name__)

# Configure logging to INFO level
logging.basicConfig(level=logging.INFO)

"""
Define a route handler for "/".
This function demonstrates:
1. Logging whether app/request contexts are active.
2. Safely storing a value in g when inside a request.
3. Returning the stored value.
"""

@app.route("/")
def index():
    # Step 1: Log context state before touching g
    logging.info("Has app context? %s", has_app_context())
    logging.info("Has request context? %s", has_request_context())

    # Step 2: Safe to use g here because request context is active
    g.value = "Hello from g"

    # Step 3: Return the stored value
    return f"g.value = {g.value}"

"""
Modulino pattern:
If run directly, start the Flask development server.
If imported, expose 'app' for use in a larger application.
"""

if __name__ == "__main__":
    app.run(debug=True)

References

https://flask.palletsprojects.com/en/1.1.x/design/

https://flask.palletsprojects.com/en/3.0.x/shell/

https://github.com/pallets-eco/blinker/blob/main/tests/test_signals.py

https://pythonbasics.org/flask-rest-api/

https://sodocumentation.net/flask/topic/1789/working-with-json

http://www2.lawrence.edu/fast/GREGGJ/CMSC210/flask/flask.html

https://stackoverflow.com/questions/104983/what-is-thread-local-storage-in-python-and-why-do-i-need-it

https://stackoverflow.com/questions/20036520/what-is-the-purpose-of-flasks-context-stacks

https://github.com/pallets/flask/issues/1151

https://www.cvedetails.com/cve/CVE-2018-1000656/

https://www.cvedetails.com/vendor/17201/Palletsprojects.html

https://pythonise.com/series/learning-flask/python-before-after-request

https://stackoverflow.com/questions/8896473/exception-for-non-existing-parameter-in-flask

https://flask.palletsprojects.com/en/1.1.x/api/#flask.Flask.trap_http_exception

https://werkzeug.palletsprojects.com/en/master/exceptions/

https://programming.vip/docs/exception-handling-of-flask-development-skills.html

https://apps.dtic.mil/sti/pdfs/AD1038470.pdf

https://github.com/corydolphin/flask-cors/blob/master/tests/core/helper_tests.py

https://tech-habit.info/posts/https-cert-based-auth-with-flask-and-gunicorn

https://stackoverflow.com/questions/29458548/can-you-add-https-functionality-to-a-python-flask-web-server

https://github.com/pallets/flask/blob/master/tests/test_cli.py

https://flask.palletsprojects.com/en/1.1.x/api/#flask.Flask

https://flask.palletsprojects.com/en/1.1.x/patterns/deferredcallbacks/

https://flask.palletsprojects.com/en/1.1.x/testing/

https://github.com/aws/aws-xray-sdk-python/blob/master/aws_xray_sdk/ext/flask/middleware.py

https://philchen.com/2019/07/15/scaling-a-python-flask-app-with-nginx-using-multiple-containers-with-docker-compose

https://xplordat.com/2020/02/16/a-flask-full-of-whiskey-wsgi/

https://flask-jwt-extended.readthedocs.io/en/stable/basic_usage/

https://github.com/vimalloc/flask-jwt-extended/issues/204

https://overiq.com/flask-101/custom-response-and-hook-points-in-flask/

https://chase-seibert.github.io/blog/2016/03/31/flask-sqlalchemy-sessionless.html

https://fabianlee.org/2019/11/18/python-using-flask-to-stream-chunked-dynamic-content-to-end-users/

https://gist.github.com/ivanlmj/dbf29670761cbaed4c5c787d9c9c006b

https://coddyschool.com/upload/Flask_Web_Development_Developing.pdf

https://stackoverflow.com/questions/10999990/get-raw-post-body-in-python-flask-regardless-of-content-type-header?rq=1

https://stackoverflow.com/questions/10434599/get-the-data-received-in-a-flask-request

Patterns

pytest-flask