chainlit icon indicating copy to clipboard operation
chainlit copied to clipboard

Add support for displaying pandas DataFrame as an interactive table

Open desertproject opened this issue 5 months ago • 5 comments

This is an attempt to address issue #1350, and it may serve as a basis for future improvements and better implementations.

To use this functionality, follow these steps:

  1. Convert the DataFrame to JSON: Use the to_json() method with the "split" orientation to convert the DataFrame into a JSON string, formatting the data for display.
  2. Create a DataFrame Element: Pass the JSON string as the content parameter when creating the DataFrame element.
  3. Append and Send: Append the DataFrame element to a message and send it for display.
import chainlit as cl
import pandas as pd


@cl.on_chat_start
async def start():
    iris = pd.read_csv(
        "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv"
    )

    json_dataframe = iris.to_json(orient="split")

    elements = [
        cl.Dataframe(content=json_dataframe, display="inline", name="Dataframe")
    ]

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

desertproject avatar Sep 24 '24 18:09 desertproject