当前位置:网站首页>Automatic generation of client code from flask server code -- Introduction to flask native stubs Library
Automatic generation of client code from flask server code -- Introduction to flask native stubs Library
2022-07-03 15:27:00 【Likianta Me】
flask-native-stubs Project inspiration swagger Of codegen Tools . It is used to automatically generate the corresponding client interface function from the server's routing function .
You will experience something like “ Local function call ” As fast and natural . As follows :
Server code :
from flask_native_stubs import app, auto_route @auto_route() def hello_world(a: int, b: bool, c: str) -> str: print('received from client side:', a, b, c) return 'hello from server side!' app.run(host='127.0.0.1', port=8080)Client code :
# client side from flask_native_stubs import setup_client # notes : take flask_native_stubs Auto generated code ( If called "server_stubs.py") Put it where it can # Enough place to import . from my_lib.server_stubs import hello_world # To configure host and port port . setup_client(host='127.0.0.1', port=8080) # It's like calling a local function . No matter how it is written , still ide Type tips , Code completion, etc # can , It can be implemented completely according to the habits of ordinary function calls ! response = hello_world(a=123, b=True, c='hello from client!') print(response) # -> 'hello from server side!'Use flask_native_stubs Auto generated code ( Referenced in the above code block “server_stubs.py”) What does it look like :
from flask_native_stubs.stubgen import magic_route # This is one that doesn't have any function " Pseudofunction " def hello_world(a: int, b: bool, c: str) -> str: ... # Here are three points . # Last use magic_route Magic method of this module hello_world Function grafted to request # Decorator . [magic_route(x) for x in ('hello_world',)]
Run a screenshot :


What is a stub file (stubs)
“stub” Quote from java j2ee Of stub Concept :
Invokes an object on a remote host for masking clients , You must provide some way to simulate local objects , Such local objects are called stubs (stub). The stub is responsible for receiving the local method call , And delegate them to their concrete implementation objects .
stay python in , Also have stub The concept of , Mainly used for the enhanced experience of type checking . ( With python The type annotation is becoming more and more perfect , Some people think that stub Its role in this regard has been weakened .)
We use the shell of the original function on the server side , The generated shapes are similar but do not have any real business logic “ Pseudo function ”, Call it stubs, In terms of concept and purpose of use, it is consistent with the above .
How to generate stub file (stubs)
Generate flask-native-stubs The stub file method of is very simple . Just put the app.run Change it to app.generate_stubs that will do :
# server side
from flask_native_stubs import app, auto_route
@auto_route()
def hello_world(a: int, b: bool, c: str) -> str:
print('received from client side:', a, b, c)
return 'hello from server side!'
# app.generate_stubs(output_dir: str, filenames_map: dict = None)
# args:
# output_dir: In which directory will the generated stub file be placed .
# filenames_map: ( Optional ) Customize input and output file names . Key is the input file path , Value is lose
# Out of the file path .
# Paths can use relative or absolute paths .
# When using relative paths , The input is relative to the current working directory ; The output is relative to # output_dir Parameters .
app.generate_stubs(
output_dir='../lib',
filenames_map={
'server.py': 'server_stubs.py'
}
)
Copy the generated stub file to the client project , Put it where the client can import . ( Like joining in sys.path, Or join PYTHONPATH In the environment variables .)
边栏推荐
- Backtracking method to solve batch job scheduling problem
- Kubernetes - YAML文件解读
- Basic SQL tutorial
- Redis single thread problem forced sorting layman literacy
- Visual upper system design and development (Halcon WinForm) -1 Process node design
- 自定义注解
- Summary of JVM knowledge points
- 使用Tengine解决负载均衡的Session问题
- Matlab r2011b neural network toolbox precautions
- Visual host system design and development (Halcon WinForm)
猜你喜欢

运维体系的构建

Tensorflow realizes verification code recognition (III)

秒杀系统1-登录功能

Creation and destruction of function stack frames

Seckill system 2 redis solves the problem of distributed session

Introduction to redis master-slave, sentinel and cluster mode

软件逆向破解入门系列(1)—xdbg32/64的常见配置及功能窗口

Can‘t connect to MySQL server on ‘localhost‘

视觉上位系统设计开发(halcon-winform)-3.图像控件

Jvm-06-execution engine
随机推荐
官网MapReduce实例代码详细批注
Enable multi-threaded download of chrome and edge browsers
[daily training] 395 Longest substring with at least k repeated characters
Functional modules and application scenarios covered by the productization of user portraits
开启 Chrome 和 Edge 浏览器多线程下载
Halcon与Winform学习第二节
Unity功能——Unity离线文档下载及使用
Matlab r2011b neural network toolbox precautions
Unityshader - materialcapture material capture effect (Emerald axe)
Detailed pointer advanced 1
Using TCL (tool command language) to manage Tornado (for VxWorks) can start the project
Tensorflow realizes verification code recognition (II)
Popular understanding of linear regression (I)
需要知道的字符串函数
Jvm-04-runtime data area heap, method area
Jvm-09 byte code introduction
redis缓存穿透,缓存击穿,缓存雪崩解决方案
Srs4.0+obs studio+vlc3 (environment construction and basic use demonstration)
"Seven weapons" in the "treasure chest" of machine learning: Zhou Zhihua leads the publication of the new book "machine learning theory guide"
mysql innodb 存储引擎的特性—行锁剖析