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, replace decorators from
@app.route(...)
to@auto_route(...)
; - Create a temp script to generate server stub file;
- Copy generated stub (folder) to client side;
- Client imports stub files and use them like native functions.
Example for guide through:
demo
|= server
|- main.py
|= client
|- main.py
-
server/main.py
source code:from flask_native_stubs import app, auto_route @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.run('localhost', 5000)
-
Create a temp script like this:
In 'server/temp_stubgen.py':
import os from flask_native_stubs import enable_stubgen from flask_native_stubs import generate_stub_files enable_stubgen(project_root_path=os.getcwd()) # will search `@auto_route` decorators only under '~/server'. # explicit import modules which contains routed functions. from main import hello ... os.mkdir(d := 'server_stubs') generate_stub_files(dir_o=d)
-
Run this script.
-
It will generate 'server/server_stubs' folder (contains a stub file named 'main.py').
The stub file looks like:
""" Auto-generated stub file by [flask-native-stubs][1]. [1]: https://github.com/likianta/flask-native-stubs """ from flask_native_stubs import add_route from typing import Any def hello(a: int, b: str, c: float) -> list: ... [add_route(x) for x in (hello,)]
-
-
Copy the generated stub to client side:
Now the directories are:
demo |= server |- main.py |- temp_stubgen.py |= server_stubs # 1. copy from |- main.py |= client |- main.py |= server_stubs # 2. copy to |- main.py
-
Write your client side implementations with server stubs:
'client/main.py':
from flask_native_stubs import setup from . import server_stubs as stubs setup(host='localhost', port=5000) # note: you should ask server admin to get the host and port. def main(): result = stubs.hello(a=123, b='456', c=7.89) # the ide works well with type hints :) print(result) # -> [123, '456', 7.89] ... if __name__ == '__main__': main()
How it works
This project is inspired by swagger codegen.
I want to get both type hints support and the convenience of native functions calling style. So let the mechanism become truth 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 @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 Distribution
Built Distribution
Hashes for flask_native_stubs-0.3.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e215481661e4214bf7ba062a4ed82064385d371ec6bff6fd32c81a27bb8c26e |
|
MD5 | 3342eaadfb2410fdb26e1c562f46e6d2 |
|
BLAKE2b-256 | 3332e9273655ea8452787dd3b2ee4754e8d05d423eaf7b3a6a07550513ac05ec |