当前位置:网站首页>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 .)
边栏推荐
- Markdown file titles are all reduced by one level
- 视觉上位系统设计开发(halcon-winform)
- Introduction to redis master-slave, sentinel and cluster mode
- Baidu AI Cloud helps Shizuishan upgrade the smart health care model of "Internet + elderly care services"
- 视觉上位系统设计开发(halcon-winform)-4.通信管理
- What are the composite types of Blackhorse Clickhouse, an OLAP database recognized in the industry
- 需要知道的字符串函数
- Digital image processing -- popular Canny edge detection
- 驱动与应用程序通信
- Visual upper system design and development (Halcon WinForm) -5 camera
猜你喜欢
从 flask 服务端代码自动生成客户端代码 -- flask-native-stubs 库介绍
使用AUR下载并安装常用程序
详解指针进阶1
What is machine reading comprehension? What are the applications? Finally someone made it clear
How are integer and floating-point types stored in memory
软件逆向破解入门系列(1)—xdbg32/64的常见配置及功能窗口
详解指针进阶2
Kubernetes vous emmène du début à la fin
Halcon与Winform学习第二节
秒杀系统1-登录功能
随机推荐
Digital image processing -- popular understanding of corrosion and expansion
Visual upper system design and development (Halcon WinForm) -5 camera
Jvm-03-runtime data area PC, stack, local method stack
Didi off the shelf! Data security is national security
高并发下之redis锁优化实战
[probably the most complete in Chinese] pushgateway entry notes
Atlas atlas torque gun USB communication tutorial based on mtcom
Reentrantlock usage and source code analysis
The wonderful use of do{}while()
Unityshader - materialcapture material capture effect (Emerald axe)
Stress test WebService with JMeter
Wechat payment -jsapi: code implementation (payment asynchronous callback, Chinese parameter solution)
Final review points of human-computer interaction
Halcon与Winform学习第一节
win32创建窗口及按钮(轻量级)
XWiki安装使用技巧
Creation and destruction of function stack frames
运维体系的构建
How are integer and floating-point types stored in memory
子类隐藏父类的同名函数