Skip to main content

A lightweight toolkit that simplifies the Flask development process, helping you quickly build Python web applications.

Project description

Lazy-Flask

CI codecov PyPI License

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lazy_flask-0.1.2.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lazy_flask-0.1.2-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file lazy_flask-0.1.2.tar.gz.

File metadata

  • Download URL: lazy_flask-0.1.2.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lazy_flask-0.1.2.tar.gz
Algorithm Hash digest
SHA256 8adc5bedcdb499df6d7b7956f33491eda7cf894d57a0919b715440f1549d6634
MD5 6457f88f796f03c1a9f63867bd261506
BLAKE2b-256 d07c898a454148e268783e5070b90e1089f96e9a68579d1de465d6a3576a6011

See more details on using hashes here.

Provenance

The following attestation bundles were made for lazy_flask-0.1.2.tar.gz:

Publisher: ci.yml on wrl96/lazy-flask

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lazy_flask-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: lazy_flask-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lazy_flask-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2b57c7dfb948970acd351bc53494660b2b62b34f65e2a303cf2e4999eaf55f46
MD5 7aba68e25dd552b5aef78df8feffce5f
BLAKE2b-256 55080629a75075e2f9bfdf837a006a7a3314af7b3adbc66c3dd2e449b1b481ee

See more details on using hashes here.

Provenance

The following attestation bundles were made for lazy_flask-0.1.2-py3-none-any.whl:

Publisher: ci.yml on wrl96/lazy-flask

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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