An flask extension to make your code more elegant
Project description
flask-seek
An flask extension to make your code more elegant.
Automatically discover and register Blueprint and decorators (such as before_request).
Requirements
- Python 3.6+
- Flask 1.1.0+
Installation
$ pip install flask-seek
A Simple Example
- Project structure and content
project
hello.py
main.py
# main.py
from flask import Flask
from flask_seek import seek
app = Flask(__name__)
seek(app, blueprint_modules=["hello"])
if __name__ == "__main__":
app.run()
# hello.py
from flask import Blueprint
hello_bp = Blueprint("hello", __name__)
@hello_bp.route("/")
def hello():
return {"msg": "Hello"}
- start
$ python main.py
* Serving Flask app 'main' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
$ curl -s http://127.0.0.1:5000/
{"msg":"Hello"}
Example upgrade
project
common
__init__.py
error_handler.py
middleware.py
controller
__init__.py
hello.py
main.py
# main.py
from flask import Flask
from flask_seek import seek
app = Flask(__name__)
seek(app, blueprint_deep_modules=["controller"], decorator_modules=["common"])
if __name__ == "__main__":
app.run()
# hello.py
from flask import Blueprint
hello_bp = Blueprint("hello", __name__)
@hello_bp.route("/")
def hello():
print("hello")
return {"msg": "Hello"}
@hello_bp.route("/error")
def error():
a = 1 / 0
return {"msg": "Hello"}
# error_handler.py
from flask_seek import ff
@ff.errorhandler(Exception)
def err(e):
return {"msg": "Server Error"}
# middlerware.py
from flask_seek import df
@df.before_request
def before():
print("before_request")
@df.after_request
def after(resp):
print("after_request")
return resp
- start
$ python main.py
* Serving Flask app 'main' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
- Blueprint registered automatically
$ curl -s http://127.0.0.1:5000/
{"msg":"Hello"}
- before_request, after_request take effect
$ python main.py
* Serving Flask app 'main' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
before_request
hello
after_request
127.0.0.1 - - [11/Jun/2021 00:06:13] "GET / HTTP/1.1" 200 -
- errorhandler take effect
$ curl -s http://127.0.0.1:5000/error
{"msg":"Server Error"}
Guide
seek
-
parameters
-
instance - flask or buleprint instance
-
blueprint_modules - List of blueprint modules path such as
["common", "common.demo"]
-
blueprint_deep_modules - It will recursively query all blueprint modules of the package
-
decorator_modules - List of flask decorator modules path
-
decorator_deep_modules - It will recursively query all decorator modules of the package
-
-
example
project
common
__init__.py
error_handler.py
middleware.py
demo
__init__.py
a.py
main.py
# main.py
from flask import Flask
from flask_seek import seek
app = Flask(__name__)
seek(app, decorator_modules=["common"]) # will search error_handler.py, middleware.py
seek(app, decorator_modules=["common.middleware"]) # will search middleware.py
seek(app, decorator_deep_modules=["common"]) # will search error_handler.py, middleware.py, a.py
seek(app, decorator_modules=["common.demo"]) # will search a.py
df
decorator without parameters
from flask_seek import df
@df.before_request
def before():
print("before_request")
ff
decorator with parameters
from flask_seek import ff
@ff.errorhandler(Exception)
def err(e):
return {"msg": "Server Error"}
License
This project is licensed under the terms of the MIT license.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flask-seek-0.2.1.tar.gz
.
File metadata
- Download URL: flask-seek-0.2.1.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63c17494ddb4f61973bd55462e2c284e69064f659f39eae7b9a21ede730f5dd0 |
|
MD5 | 08ecdaf2918352509d4261f12cedf46d |
|
BLAKE2b-256 | fe414491cf703b2b58307b8332cfaed3e1c75443d4fd719e872a366081d6328c |
File details
Details for the file flask_seek-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: flask_seek-0.2.1-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11b27664388de6d6e53f41d55e29805c41e20e58ab5a2336fcfde0471b27d2c7 |
|
MD5 | a2155b73a62d6b8fffb8dc6e192bf8e5 |
|
BLAKE2b-256 | 1f54b41290799fd73ae1e9ee21819c899e54e526bcbc3d63e450bd66ba2fd057 |