Call flask decorated functions like native ones.
Project description
Flask Native Stubs
Call flask decorated functions like native ones.
Overall workflow:
- Create server side functions, use
@app.auto_route(...)
instead of@app.route(...)
. - In server side, finally call
app.generate_stubs(...)
instead ofapp.run(...)
. -- This creates a "stubs" folder that contains all magic routes. - Copy generated stubs (folder) to client side.
- Client imports stub files and use them like native functions.
Installation
pip install flask-native-stubs
The latest version is 0.4.2+.
Quick Start
Example for guide through:
demo
|= server
|- main.py
|= client
|- main.py
-
server/main.py
source code:from flask_native_stubs import app @app.auto_route() def hello(a: int, b: str, c: float) -> list: # type annotations are optional, but recommended. return [a, b, c] if __name__ == '__main__': app.generate_stubs( dir_i='.', dir_o='./stubs', ) # app.run('localhost', 5000)
-
Run
server/main.py
and get "stubs" folder like below:demo |= server |= stubs # <- generated stubs |- __init__.py |- main.py # <- a `hello` function is defined in it. |- main.py |= client |- main.py
-
Move "stubs" folder to client side:
demo |= server |- main.py |= client |= stubs |- __init__.py |- main.py # <- a `hello` function is defined in it. |- main.py
-
Write your client side code:
'client/main.py':
from flask_native_stubs import setup_client from .stubs import main as server_stubs # note: you may ask the server admin to get the ip address. setup_client('localhost', 5000) def main(): response = server_stubs.hello(a=123, b='456', c=7.89) # the IDE works well with type hints. print(response) # -> [123, '456', 7.89] if __name__ == '__main__': main()
More examples
Please go to examples
folder, follow examples/readme.md
to run them.
How it works
This project is inspired by swagger codegen.
I want to get both typing annotations support and the convenience of native functions calling style. So let the mechanism works by some delegate hooks underlay.
Here are a few comparisons between swagger and flask-native-stubs:
- Workflow:
- Swagger:
- Draw a blueprint by configuration files;
- Generate template code;
- Override templates to implement server and client logics.
- Flask-native-stubs:
- We have implemented a flask app;
- Now generate stub files;
- Let the client side interact with stubs.
- Swagger:
- TODO:More...
TODO:ExplainFlaskNativeStubsImplementations
Cautions
-
Do not use
*args
and**kwargs
in decorated functions:# don't @app.auto_route() def xxx(a, b, c, *args, d=1, e=None, **kwargs): ...
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 Distributions
Built Distribution
File details
Details for the file flask_native_stubs-0.4.4-py3-none-any.whl
.
File metadata
- Download URL: flask_native_stubs-0.4.4-py3-none-any.whl
- Upload date:
- Size: 16.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.11.1 Darwin/21.2.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88a8afb42b2b15a54e743adce5a545914911cde2d1e695fa94f4f96ad35bf185 |
|
MD5 | 82b8ca9e92d676ae8db5238a3e935656 |
|
BLAKE2b-256 | 775b5380f9b4cdfea37d5bbc384ad2f33919a1bc9cf5b9b17e23ed079adba41a |