当前位置:首页>python>别再手动画架构图了!Python一行代码自动生成

别再手动画架构图了!Python一行代码自动生成

  • 2026-10-12 16:01:37
别再手动画架构图了!Python一行代码自动生成

你是不是也有这样的困扰?

每次要画系统架构图,都得打开Visio或者Draw.io,一个个拖组件、画连线,折腾半天。

好不容易画完了,架构一改又得重来。

说实话,我之前也是这样。

直到我发现了一个神器——Python的diagrams库,从此告别手动画图。


这个库有多好用?

简单来说,就是用代码生成架构图。

你只需要几行Python代码,就能生成专业的云服务架构图。

不用再手动拖拽组件,不用再调整布局,代码写完,图就出来了。

就像写文档一样自然。

代码就是最好的架构文档。

怎么开始?

先安装两个东西:Python和GraphViz。

安装命令:

# macOS / Linuxsudo apt-get install graphviz        # Ubuntu/Debianbrew install graphviz                # macOS# Windows → 下载 MSI:https://graphviz.org/download/pip install -U diagrams graphviz

验证是否安装成功:

dot -V>>> dot - graphviz version 8.1.0 (20230707.0739)

核心概念速览

diagrams库有几个核心概念,一学就会:

概念
说明
代码示例
Diagram
整张图画布
with Diagram(...)
Node
组件节点
EC2("web")
Cluster
逻辑分组
with Cluster("DMZ"):
Edge
连接线
>>
 或 -
Custom
自定义图标
Custom("CDN", "./cdn.png")

不用记,用多了就熟了。


来个实战例子

咱们画一个微服务架构图,包含API网关、用户服务和订单服务。

代码是这样的:

from diagrams import Diagram, Clusterfrom diagrams.aws.compute import ECSfrom diagrams.aws.network import APIGateway, ELBfrom diagrams.aws.database import RDS, ElastiCachefrom diagrams.aws.storage import S3from diagrams.aws.security import WAFwith Diagram("Microservice Architecture", show=True, direction="LR", filename="micro"):    waf = WAF("WAF")    api = APIGateway("API Gateway")    with Cluster("User Service"):        user_lb = ELB("User-LB")        user_ecs = ECS("User-API")        user_db = RDS("User-DB")        user_cache = ElastiCache("Redis")        user_lb >> user_ecs >> user_cache        user_ecs >> user_db    with Cluster("Order Service"):        order_lb = ELB("Order-LB")        order_ecs = ECS("Order-API")        order_db = RDS("Order-DB")        order_lb >> order_ecs >> order_db    storage = S3("Static/Avatar")    user_ecs >> storage    order_ecs >> storage    waf >> api >> user_lb    waf >> api >> order_lb

运行一下:

python 00_microservice.py# 生成 micro.png(高清,4000×1800)

你看,就这么简单。

一个包含WAF、API网关、两个微服务集群(各自有负载均衡、服务实例、数据库、缓存)以及共享存储的架构图就生成了。


还有这些玩法

事件驱动架构

用EventBridge、SQS和Lambda组合,画一个事件驱动的架构:

from diagrams import Diagramfrom diagrams.aws.compute import Lambdafrom diagrams.aws.database import DynamoDBfrom diagrams.aws.integration import SQS, Eventbridgewith Diagram("Event-Driven", show=True, direction="TB"):    bus = Eventbridge("EventBridge")    queue = SQS("Order Queue")    producer = Lambda("Producer")    consumer = Lambda("Consumer")    db = DynamoDB("Order DB")    producer >> bus >> queue >> consumer >> db    db << producer  # 双向连接

混合云架构

AWS、GCP、Azure三家云服务放在一起:

from diagrams import Diagram, Cluster, Edgefrom diagrams.aws.compute import EC2from diagrams.gcp.compute import ComputeEnginefrom diagrams.azure.compute import VirtualMachinesfrom diagrams.onprem.network import Internetwith Diagram("Hybrid-Cloud", show=True):    inet = Internet("Internet")    with Cluster("AWS"):        aws_vpc = EC2("EC2-VPC")    with Cluster("GCP"):        gcp_vpc = ComputeEngine("GCE-VPC")    with Cluster("Azure"):        az_vpc = VirtualMachines("VM-VNet")    inet >> [aws_vpc, gcp_vpc, az_vpc]    aws_vpc - Edge(label="IPSec VPN") - gcp_vpc    gcp_vpc - Edge(label="专线") - az_vpc

Serverless架构

API Gateway + Cognito + Lambda + DynamoDB:

from diagrams import Diagramfrom diagrams.aws.compute import Lambdafrom diagrams.aws.database import DynamoDBfrom diagrams.aws.network import APIGatewayfrom diagrams.aws.security import Cognitowith Diagram("Serverless Backend", show=True):    api = APIGateway("API")    auth = Cognito("Auth")    func = Lambda("Lambda")    db = DynamoDB("DynamoDB")    api >> auth >> func >> db

还有CI/CD流水线、自定义图标、多层架构等等,玩法太多写不完。


实用小技巧

给你几个我常用的技巧:

保持简洁:别在一张图里塞太多东西,太乱了就拆成多张。

逻辑分组:用Cluster把相关的组件包在一起,一眼就能看懂。

一致方向:统一从左到右或者从上到下,别一会儿横一会儿竖。

版本控制:图表代码和项目代码放一起,用Git管理,改架构有历史记录。

自动化生成:把图表生成集成到CI/CD流程里,每次发布自动更新文档。


写在最后

说实话,我为什么这么喜欢diagrams这个库?

因为它解决了一个痛点:架构文档和代码不同步。

传统的架构图是静态的,代码改了,图忘了改,文档就过期了。

用diagrams,架构图就是代码。

代码更新了,重新运行一下,架构图就自动更新了。

架构文档永远和代码保持同步。

这就是我说的:代码就是最好的架构文档。


💬 你平时怎么画架构图?用的什么工具?评论区聊聊~

如果你觉得这篇文章有用,欢迎点赞、转发、收藏、留言、推荐❤!👇
更多Python阅读>>>
Python Pandas自动化处理Excel数据,替代vlookup
Python 90个经典使用技巧
Python Selenium库自动化网页任务
Python 6个GUI界面开发的库
Python 120个字典操作方法
Python 容器化应用代码并使用Kubernetes进行自动化服务发布
Python 自动化批量设置 Excel 格式
Python 用pandas自动化获取表格网页数据
Python Webbrowser自动化控制浏览器窗口
Python 15个高效代码写法:提升开发效率
Python pathlib的使用方法
Python 自动化监控大文件
Python 文件监控利器Watchdog的使用方法
Python 50个列表操作方法
Python 40个常见场景的代码示例
Python 回调函数的常见应用场景
Python SQLite数据库:轻量级数据存储解决方案
Python 20个数据清洗技巧,提升数据质量
Python 50个常用统计分析方法
Python requests库用法详解
Python 嵌套数据结构
Python 深拷贝与浅拷贝
Python 25个函数的开发技巧
Python PEP8代码风格
Python 30个元组操作方法
Python 20个提高学习效率的工具
Python 自动化生成二维码
Python 30个自动化日常任务脚本:职场效率提升利器
Python数据可视化的10种方法
Python 20个常用的开发库
Python 自动化文件管理的10个模板
Python 命令行参数解析的3个方法,提升脚本效率
Python 开发中常见的高效写法
Python 50个命令行技巧
Python 40个实用代码案例:提升效率
Python 30个自动化办公图像处理的方法
Python 25个实用的自动化办公代码,拿来即用
“无他,惟手熟尔”!有需要就用起来。
------加入知识场与更多人一起学习------

https://ima.qq.com/wiki/?shareId=f2628818f0874da17b71ffa0e5e8408114e7dbad46f1745bbd1cc1365277631c

最新文章

随机文章