Skip to main content

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

flask-seek-0.1.0.tar.gz (4.8 kB view hashes)

Uploaded Source

Built Distribution

flask_seek-0.1.0-py3-none-any.whl (5.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page