Plotly 类允许您在聊天机器人 UI 中显示 Plotly 图表。此类接受 Plotly 图形对象。

Plotly 元素相对于 Pyplot 元素的优势在于它是交互式的(例如,用户可以在图表上进行缩放)。

属性

name
str

要在 UI 中显示的图表名称。

display
ElementDisplay

确定图表元素应如何在 UI 中显示。选项包括“侧边”、“内嵌”或“页面”。

size
ElementSize

确定图表的大小。仅在 display=“inline” 时有效。选项包括“小”、“中”(默认)或“大”。

figure
str

您要显示的 plotly.graph_objects.Figure 实例。

示例

import plotly.graph_objects as go
import chainlit as cl


@cl.on_chat_start
async def start():
    fig = go.Figure(
        data=[go.Bar(y=[2, 1, 3])],
        layout_title_text="An example figure",
    )
    elements = [cl.Plotly(name="chart", figure=fig, display="inline")]

    await cl.Message(content="This message has a chart", elements=elements).send()