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/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://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://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/10434599/get-the-data-received-in-a-flask-request
Patterns
- https://github.com/uwcirg/script-fhir-facade/blob/develop/script_facade/app.py
- https://github.com/xiaobing88/Python-little-code/blob/master/23_flask/flask_blog/Lib/site-packages/pip/_vendor/requests/models.py#L265
pytest-flask
- https://stackoverflow.com/questions/45703591/how-to-send-post-data-to-flask-using-pytest-flask
- https://stackoverflow.com/questions/30249082/python-flask-test-client-doesnt-have-request-authorization-with-pytest
- https://stackoverflow.com/questions/46846762/flask-jwt-extended-fake-authorization-header-during-testing-pytest
- https://stackoverflow.com/questions/38545913/accessing-flask-test-client-session-in-pytest-test-when-using-an-app-factory
- https://stackoverflow.com/questions/56939383/flaskpytestsqlalchemy-cant-create-and-drop-tables-when-run-pytest-using-flas
- https://stackoverflow.com/questions/57312603/how-to-completely-teardown-flask-app-after-each-test-in-pytest