页面树结构

版本比较

标识

  • 该行被添加。
  • 该行被删除。
  • 格式已经改变。

...

1、使用分析模式,输入问句执行一个查询,点击 添加到仪表盘 按钮,如下图所示:

Image Modified

2、页面自动跳转到仪表盘页面,并且会显示刚才的查询结果,如下图所示:

...

3、点击 编辑 按钮,可以修改python代码,如下图所示

Image Modified

2 手工编写python代码

...

我们现在想修改下代码,除了查出“品牌”,还希望查出“产品名称”,效果如下图所示:

Image Modified

Image Modified

2.2 代码解读

在仪表盘Python组件中,这里只会贴一个代码片段,然后系统会自动将用户编写的代码片段提交到后台的Jupyter Kernel中执行。一些基础包,也是已经在Jupyter Kernel中初始化了,当然也可以在该代码片段中添加所需的包,但只能添加Jupyter容器中已导入的包。

...

代码块
# 创建数据
data = {
    '姓名': ['张三', '李四', '王五', '赵六', '孙七'],
    '年龄': [20, 22, 21, 23, 20],
    '成绩': [85.5, 90.0, 78.5, 88.0, 92.5]
}

# 创建 DataFrame
df_result = pd.DataFrame(data)

# 输出结果
import json
json_result = json.dumps([{"output_variable_name": "df_result", "type": "dataframe", "desc": "学生成绩表"}])
json_result

Image Modified

2.3.2 图形输出

代码块
# 创建数据
data = {
'姓名': ['张三', '李四', '王五', '赵六', '孙七'],
'年龄': [20, 22, 21, 23, 20],
'成绩': [85.5, 90.0, 78.5, 88.0, 92.5]
}

# 创建 DataFrame
df_result = pd.DataFrame(data)

import plotly.graph_objects as go
# 创建图形
fig = go.Figure()

# 添加销售额柱状图
fig.add_trace(go.Bar(
x=df_result['姓名'],
y=df_result['成绩'],
name='成绩'
))

# 设置图形布局
fig.update_layout(
title='学生成绩',
xaxis_title='姓名',
yaxis_title='成绩',
legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1
)
)

# 输出结果
import json
json_result = json.dumps([{"output_variable_name": "fig", "type": "plotly", "desc": "学生成绩表"}])
json_result

Image Modified

2.3.3 多结果集输出

...

代码块
# 创建数据
data = {
'姓名': ['张三', '李四', '王五', '赵六', '孙七'],
'年龄': [20, 22, 21, 23, 20],
'成绩': [85.5, 90.0, 78.5, 88.0, 92.5]
}

# 创建 DataFrame
df_result = pd.DataFrame(data)

import plotly.graph_objects as go
# 创建图形
fig = go.Figure()

# 添加销售额柱状图
fig.add_trace(go.Bar(
x=df_result['姓名'],
y=df_result['成绩'],
name='成绩'
))

# 设置图形布局
fig.update_layout(
title='学生成绩',
xaxis_title='姓名',
yaxis_title='成绩',
legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1
)
)

# 输出结果
import json
json_result = json.dumps([{"output_variable_name": "fig", "type": "plotly", "desc": "学生成绩表"},{"output_variable_name": "df_result", "type": "dataframe", "desc": "学生成绩表2"}])
json_result

Image Modified

3 python代码中支持的内置函数

...

代码块
# 查询各年海尔的销售额
sql_query_haier_sales = """
SELECT
  "数据日期"."销售_年" AS "销售年",
  SUM("销售表"."销售额") AS "海尔销售额"
FROM "销售表"
JOIN "产品表" ON "销售表"."product_id" = "产品表"."product_id"
JOIN "数据日期" ON "销售表"."time_id" = "数据日期"."time_id"
WHERE "产品表"."品牌" = '海尔'
GROUP BY "数据日期"."销售_年"
ORDER BY "销售年"
"""
output_field = ["销售年", "海尔销售额"]
df_haier_sales = detail_sql_data_query_assistant(sql_query_haier_sales, output_field)

# 输出需要的结果,并将结果按照json格式输出
import json

json_result = json.dumps([{"output_variable_name": "df_haier_sales", "type": "dataframe", "desc": "各年海尔的销售额"}])
json_result

Image Modified

4 查询结果的输出

4.1 表格输出

...

代码块
out_text = "你好,这是一个文本输出"

# 输出结果
import json
json_result = json.dumps([{"output_variable_name": "out_text", "type": "text", "desc": "学生成绩表"}])
json_result

Image Modified

代码块
# 输出结果
import json
json_result = json.dumps([{"output_variable_name": "out_text", "type": "text", "desc": "学生成绩表"}])
json_result

...

代码块
out_text = "你好,这是一个文本输出"+ str(${品牌})

# 输出结果
import json
json_result = json.dumps([{"output_variable_name": "out_text", "type": "text", "desc": "学生成绩表"}])
json_result

Image Modified

代码块
out_text = "你好,这是一个文本输出"+ str(${品牌})

...

代码块
sql_json = {
"sel": ["销售_年月", "品牌", "销售额", "销售额同比增长率"],
"from": "销售表",
"conds": [${品牌}]
}
df_sales = detail_trend_data_query_assistant(sql_json)

# 输出结果
import json
json_result = json.dumps([{"output_variable_name": "df_sales", "type": "dataframe", "desc": "各年海尔及类似品牌的销售额和同比数据"}])
json_result

Image Modified

代码块
sql_json = {
"sel": ["销售_年月", "品牌", "销售额", "销售额同比增长率"],
"from": "销售表",
"conds": [${品牌}]
}

...