A lightweight toolkit that simplifies the Flask development process, helping you quickly build Python web applications.
Project description
Lazy-Flask
English | 中文文档 | Contributing Guide
Introduction
Lazy-Flask is a lightweight toolkit that simplifies the Flask development process, helping you quickly build Python web applications.
Features:
- Based on Flask, fully compatible with all Flask features
- Very small codebase (<200 lines)
- Supports custom middleware
Installation
pip install lazy-flask
Quick Start
Initialize
from lazy_flask import App
app = App(name='lazy_flask', endpoint='/query')
| Parameter | Type | Default | Description |
|---|---|---|---|
| name | str | lazy_flask | Flask app name |
| endpoint | str | /query | Interaction endpoint |
Register Module
from lazy_flask import App, Module
module = Module('module')
app.register_module(module)
Module
| Parameter | Type | Default | Description |
|---|---|---|---|
| name | str | Module name, must be unique |
register_module
| Parameter | Type | Default | Description |
|---|---|---|---|
| module | Module | Register the module to App |
Register Function
from lazy_flask import App, Module, APIResponse, APIError
@module.function('hello')
def hello(name: str = '') -> APIResponse:
return APIResponse(data={'hello': name})
@module.function('oh')
def oh() -> APIResponse:
return APIResponse(error=APIError(code=1, message='This is an error.'))
function
| Parameter | Type | Default | Description |
|---|---|---|---|
| name | str | Function name, must be unique in the same module |
APIResponse
| Parameter | Type | Default | Description |
|---|---|---|---|
| data | dict | {} | Returned data |
| error | APIError | APIError() | Error information |
APIError
| Parameter | Type | Default | Description |
|---|---|---|---|
| code | int | 0 | Error code |
| message | str | Error message |
Make a Request
import requests
url = "http://{host}/query"
data = {
"module": "module",
"function": "hello",
"args": {
"name": "world"
}
}
response = requests.request("POST", url, json=data)
print(response.text) # {"data": {"hello": "world"}, "error": {"code": 0, "msg": ""}}
Advanced Usage
Middleware
Middleware can intercept requests and responses. For example, you can do permission checks or modify responses before returning.
Just define a function that accepts a Request or Response. If you modify it, return the new object. If not, Lazy-Flask will continue with the original.
# Permission check
import time
from lazy_flask import *
from flask import request as flask_request
info = Module('info')
app.register_module(info)
# Check request, only executed when tag contains 'login'
def login_middleware(request: APIRequest):
user_id = request.args.get('id', None)
if user_id is None:
raise APIError(code=1, message='User id is none')
token = flask_request.headers.get('Authorization', None)
if token is None:
raise APIError(code=2, message='Token is none')
info.register_middleware(Middleware(login_middleware, tag='login', priority=1, m_type=MiddlewareType.Request))
# Modify response, executed globally
def add_timestamp_middleware(response: APIResponse) -> APIResponse:
response['timestamp'] = int(time.time() * 1000)
return response
info.register_middleware(Middleware(add_timestamp_middleware, m_type=MiddlewareType.Response))
@info.function('info', tags=['login'])
def info():
return APIResponse(data={'result': 'ok'})
Middleware
| Parameter | Type | Default | Description |
|---|---|---|---|
| function | Callable | Middleware handler, must accept Request or Response | |
| tag | str | If set, only runs when decorator specifies the same tag, otherwise global | |
| priority | int | 1 | Priority, larger runs earlier |
| m_type | MiddlewareType | MiddlewareType.Request | Middleware type (Request or Response) |
Demo
pip3 install lazy-flask
pip3 install requests
python3 example/app.py
python3 example/req.py # run in another terminal
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lazy_flask-0.1.3.tar.gz.
File metadata
- Download URL: lazy_flask-0.1.3.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d03dcf4f04f7d797d2defaed6857e98479c52bf1788a7ca5a38824143c58fd9
|
|
| MD5 |
b55998043cf935680404b570815e3559
|
|
| BLAKE2b-256 |
8f9a580da2875a4eaff284f2a3884dee1314ce5e7c288629c3b5e61ee593d723
|
Provenance
The following attestation bundles were made for lazy_flask-0.1.3.tar.gz:
Publisher:
ci.yml on wrl96/lazy-flask
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lazy_flask-0.1.3.tar.gz -
Subject digest:
4d03dcf4f04f7d797d2defaed6857e98479c52bf1788a7ca5a38824143c58fd9 - Sigstore transparency entry: 656057773
- Sigstore integration time:
-
Permalink:
wrl96/lazy-flask@a3ee00efbc842c1d8ee88b354cb8604835991fda -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/wrl96
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@a3ee00efbc842c1d8ee88b354cb8604835991fda -
Trigger Event:
push
-
Statement type:
File details
Details for the file lazy_flask-0.1.3-py3-none-any.whl.
File metadata
- Download URL: lazy_flask-0.1.3-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff28842aee99c2efdf057b000f1c95038e0608639b834a27ad58958e44c5c1a7
|
|
| MD5 |
a00255a66bed861085e371971a36cca7
|
|
| BLAKE2b-256 |
8179874851b4ebfb9217350a5b45e65d24ca62341ea4edf1e41236f282c743b7
|
Provenance
The following attestation bundles were made for lazy_flask-0.1.3-py3-none-any.whl:
Publisher:
ci.yml on wrl96/lazy-flask
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lazy_flask-0.1.3-py3-none-any.whl -
Subject digest:
ff28842aee99c2efdf057b000f1c95038e0608639b834a27ad58958e44c5c1a7 - Sigstore transparency entry: 656057876
- Sigstore integration time:
-
Permalink:
wrl96/lazy-flask@a3ee00efbc842c1d8ee88b354cb8604835991fda -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/wrl96
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@a3ee00efbc842c1d8ee88b354cb8604835991fda -
Trigger Event:
push
-
Statement type: