这是一个使用 Flask 构建的简单服务器,它通过 mermaid-cli 从 Mermaid 语法生成图表。
npm install -g @mermaid-js/mermaid-clipip install -r requirements.txt
npm install -g @mermaid-js/mermaid-cli
启动服务器:
python server.py
默认情况下,服务器运行在 http://localhost:5000。
服务器会在项目文件夹中创建一个名为 temp_files 的本地目录来存储临时文件。这种方法:
打开浏览器并导航到 http://localhost:5000 使用 Web 接口。
发送一个 POST 请求到 /generate,请求体包含你的 Mermaid 图表的 JSON 数据:
{
"mermaid": "graph TD\nA[Client] --> B[Load Balancer]\nB --> C[Server1]\nB --> D[Server2]",
"theme": "default", // 可选:default, dark, forest, neutral
"background": "white" // 可选:white, transparent
}
服务器将返回渲染后的图表的 PNG 图像。
使用 curl 的示例:
curl -X POST http://localhost:5000/generate \
-H "Content-Type: application/json" \
-d '{"mermaid":"graph TD\nA[Client] --> B[Load Balancer]"}' \
--output diagram.png
运行包含的测试脚本来验证一切是否正常工作:
python test.py
这将生成一个样本图表,并将其保存为 output_diagram.png。
如果你遇到错误:
temp_files 目录存在且具有适当的权限