当前位置:网站首页>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 .)
边栏推荐
- PyTorch crop images differentiablly
- Visual upper system design and development (Halcon WinForm) -5 camera
- 需要知道的字符串函数
- Srs4.0+obs studio+vlc3 (environment construction and basic use demonstration)
- Construction of operation and maintenance system
- XWiki安装使用技巧
- Use of Tex editor
- C语言刷题~Leetcode与牛客网简单题
- Chapter 04_ Logical architecture
- Redis single thread problem forced sorting layman literacy
猜你喜欢
![MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)](/img/cd/2e4f5884d034ff704809f476bda288.png)
MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)

Seckill system 3- product list and product details

Visual upper system design and development (Halcon WinForm) -1 Process node design

详解指针进阶1

redis缓存穿透,缓存击穿,缓存雪崩解决方案

Baidu AI Cloud helps Shizuishan upgrade the smart health care model of "Internet + elderly care services"

Popular understanding of decision tree ID3

求字符串函数和长度不受限制的字符串函数的详解

Concurrency-02-visibility, atomicity, orderliness, volatile, CAS, atomic class, unsafe

GCC cannot find the library file after specifying the link library path
随机推荐
Characteristics of MySQL InnoDB storage engine -- Analysis of row lock
How to use annotations such as @notnull to verify and handle global exceptions
socket.io搭建分布式Web推送服务器
Introduction to redis master-slave, sentinel and cluster mode
求字符串函数和长度不受限制的字符串函数的详解
视觉上位系统设计开发(halcon-winform)-3.图像控件
Unityshader - materialcapture material capture effect (Emerald axe)
基于SVN分支开发模式流程浅析
【云原生训练营】模块七 Kubernetes 控制平面组件:调度器与控制器
Redis主从、哨兵、集群模式介绍
C language brush questions ~leetcode and simple questions of niuke.com
Use of Tex editor
秒杀系统3-商品列表和商品详情
Introduction, use and principle of synchronized
如何使用 @NotNull等注解校验 并全局异常处理
leetcode_ Power of Four
WinDbg分析dump文件
Srs4.0+obs studio+vlc3 (environment construction and basic use demonstration)
Summary of concurrent full knowledge points
Halcon与Winform学习第二节