简介
本文说明怎么在dify agent 应用读取excel数据,并且在聊天窗口显示echart。
具体流程
- 准备exce文件数据如下
姓名 | 身高 |
---|---|
小明 | 170 |
小张 | 180 |
小李 | 156 |
- 工作室
- 创建空白应用
- 应用名称 & 图标 填写 【读excel文件渲染】,选择应用类型选择【Chatflow】,点击创建
- 如下图打开文件上传功能
image.png
开始节点后面添加一个文档提取器的节点,其输入节点选择sys.files
在后添加代码执行,输入变量files值为【文档提取/text Array[string]】,选择python,代码如下
import re
import json
def parse_markdown_table(markdown: str):
if isinstance(markdown, list):
markdown = "\n".join(markdown) # 处理列表情况
lines = markdown.strip().split("\n")
if len(lines) < 3:
return [], [] # 避免索引错误
data_rows = lines[2:] # 跳过表头和分隔符行
names, heights = [], []
for row in data_rows:
cells = re.split(r'\s*\|\s*', row.strip('|'))
if len(cells) >= 2:
names.append(cells[0])
try:
heights.append(int(cells[1]))
except ValueError:
heights.append(0) # 避免转换错误
return names, heights
def main(files) -> dict:
if isinstance(files, list):
files = "\n".join(files) # 处理列表输入
names, heights = parse_markdown_table(files)
return {
"result": json.dumps({
"xAxis": {
"type": "category",
"data": names
},
"yAxis": {
"type": "value"
},
"series": [
{
"data": heights,
"type": "bar",
"showBackground": True,
"backgroundStyle": {
"color": "rgba(180, 180, 180, 0.2)"
}
}
]
}, ensure_ascii=False) # 确保输出双引号
}
输出变量result,类型选择string
新增llm节点,system对话的内容填写 {{#sys.query#}}
添加输出节点,回复正文如下设置:
image.png