进阶 docs.plotly.com 2026-11-24 10:55:46 · 3 阅读
第20章 表格(Tables)
第20章 Tables
表格
以表格形式展示数据,内置排序、筛选功能和可自定义的样式。
表格默认使用 Dash AG Grid,开箱即用,自带排序、分页等功能。如果想更灵活地控制样式,可以在提示词中指定使用 HTML table。
基础表格示例
下面是创建基础表格的示例。
Show a table with , sorted by .
以下示例使用上述提示词结构和 Plotly Studio 内置数据集:
Show a table with serial number, weight, length, and factory location,
sorted by weight descending.
分页
使用分页可以自定义每页显示的记录数。
Show a table with . Add pagination with rows per page.
以下示例使用上述提示词结构和 Plotly Studio 内置数据集:
Show a table with serial number, weight, length, class,
factory location, and created date.
Sort by weight descending. Add pagination with 20 rows per page.
列对齐
列可以设置为右对齐、居中对齐或左对齐。
Show a table with . -align columns.
以下示例使用上述提示词结构和 Plotly Studio 内置数据集:
Show a table with serial number, weight, length, and class.
Show top 20 products. Left-align all columns.
列固定
固定列(有时也称为冻结列)可以在横向滚动表格时保持其可见。固定的列在滚动时位置不变,方便让 ID 或名称等重要参考信息始终显示在视野中。
Show a table with . Pin to the .
以下示例使用上述提示词结构和 Plotly Studio 内置数据集:
Show a table with serial number, weight, length, class, defect,
factory location, and created date.
Show top 20 products. Pin serial number to the left.
数值格式化
可以自定义数值的显示格式,而不改变底层数据。常见的格式化包括添加货币符号、指定小数位数、添加百分号等。
Show a table with .
Format with decimal places.
Format as currency.
以下示例为数值列指定了小数精度:
Show a table with serial number, weight, length, and class.
Show top 20 products.
Format weight and length with 2 decimal places.
日期格式化
可以在提示词中指定格式,以不同的样式显示日期和时间值,例如"Month Day, Year"。
Show a table with . Format as "".
以下示例为 Created date 列设置了易读的日期格式:
Show a table with serial number, weight, factory location, and created date.
Show top 20 products sorted by created date descending.
Format created date as "Month Day, Year" (e.g., November 19, 2024).
复选框
将布尔数据("True"/"False")显示为复选框,而不是文字。
Show a table with .
Display as checkboxes (checked for , unchecked for ).
以下示例使用上述提示词结构和 Plotly Studio 内置数据集:
Show a table with serial number, weight, length, class,
defect, and factory location, sorted by serial number.
Display defect as checkboxes (checked for defects, unchecked for no defects).
工具提示
可以为单元格添加悬停时显示的工具提示,提供额外的上下文信息或说明数据的含义。
Show a table with .
Add tooltips to showing "".
以下示例使用上述提示词结构和 Plotly Studio 内置数据集:
Show a table with serial number, weight, length, class,
defect, and factory location for the top 20 products.
Add tooltips to all columns showing the full cell value on hover.
Add a tooltip to weight showing "Product weight in kilograms".
Add a tooltip to defect showing "Quality inspection result".
聚合
可以将行分组并计算汇总统计信息,如总和、计数、平均值、最小值或最大值。聚合有助于分析数据中跨类别的模式和趋势。
Show a table grouped by with .
Sort by .
以下示例使用上述提示词结构和 Plotly Studio 内置数据集中的 factory_location 列:
Show a table grouped by factory location with product count.
Sort by count descending.
其他聚合示例
下面的提示词示例使用 Plotly Studio 内置数据集计算了多项统计信息(计数、平均值、最小值、最大值):
Show weight statistics by factory location:
count, average, min, and max weight.
Sort by average weight descending.
条件格式
可以根据列中的值对行应用样式。例如,用颜色编码高亮满足特定阈值或条件的行,方便一目了然地发现模式、异常值和重要行。
Show a table with .
Color rows where as .
以下示例使用上述提示词结构和 Plotly Studio 内置数据集:
Show a table with serial number, weight, length, defect,
factory location, and created date.
Sort by created date descending. Show top 100 products.
Color rows where weight > 0.8 as red.
Color rows where weight < 0.2 as orange.
Color rows where defect != "No Defect" as yellow.
Color rows in normal range (0.2-0.8) as green.
HTML 表格
如果需要更灵活地控制表格样式,可以让 Plotly Studio 生成 HTML 表格。
Create an HTML table with .
以下示例使用上述提示词结构和 Plotly Studio 内置数据集:
Create an HTML table with serial number, weight, length,
class, and factory location.
Show top 20 products sorted by weight descending.
Use alternating row colors (white and #eeeeee light gray).
Bold column headers with dark gray background (#4a4a4a).
Add a border around the entire table.
Right-align weight and length columns.
Use minimal cell padding for compact display.
提示词关键词参考
在提示词中使用这些关键词和短语可以自定义表格。
图表
以下是创建和自定义表格的一些关键词建议:
表格有两种类型——使用 table 可获得内置的 AG Grid 功能(如排序和分页),使用 HTML table 则可完全控制样式。
关键词/短语
说明
示例
列(Columns)
表格中要显示的列
serial number, weight, length, class
数据(Data)
在提示词中指定数据指令,可在表格显示之前对数据进行转换、筛选、排序或聚合。
Sort by weight descending.
Show top 20 products.
以下是一些关键词建议:
关键词/短语
说明
示例
Sort by
按某列升序或降序排列表格
Sort by weight descending
Show top
限制显示的行数
Show top 20 products
Filter
筛选数据,只显示特定记录
Filter to show only defect = true
Group by
对行进行分组以进行聚合
Group by factory location
Aggregation
计算汇总统计信息,如计数、总和、平均值、最小值或最大值
Count products per factory
Computed field
根据现有数据创建新的计算字段
Calculate shipping days as the difference between shipped date and created date in days
选项(Options)
在提示词中指定选项,可自定义表格外观并添加交互功能,如分页、格式化、对齐和条件样式。
Add pagination with 20 rows per page.
Format weight with 2 decimal places.
Pin serial number to the left.
以下是一些关键词建议:
关键词/短语
说明
示例
Pagination
添加分页并指定每页行数
Add pagination with 20 rows per page
Column alignment
将列设置为左对齐、居中对齐或右对齐
Right-align serial number, weight, and length columns
Pin column
将列固定在左侧或右侧,使其在滚动时保持可见
Pin serial number to the left
Format values
格式化数值:指定小数位数、显示为货币或百分比
Format weight with 2 decimal places
将价格格式化为货币
将折扣格式化为百分比 格式化日期 以特定格式显示日期值 将创建日期格式化为"月 日, 年" 显示为复选框 将布尔值显示为复选框 将缺陷字段显示为复选框 添加工具提示 添加鼠标悬停提示以提供额外上下文信息 为重量字段添加工具提示,显示"产品重量(千克)" 高亮行 应用条件格式,根据条件高亮显示行 将重量大于 0.8 的行标为红色
将价格格式化为货币
将折扣格式化为百分比 格式化日期 以特定格式显示日期值 将创建日期格式化为"月 日, 年" 显示为复选框 将布尔值显示为复选框 将缺陷字段显示为复选框 添加工具提示 添加鼠标悬停提示以提供额外上下文信息 为重量字段添加工具提示,显示"产品重量(千克)" 高亮行 应用条件格式,根据条件高亮显示行 将重量大于 0.8 的行标为红色