chainlit
chainlit copied to clipboard
Add support for displaying pandas DataFrame as an interactive table
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:
- 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.
- Create a DataFrame Element: Pass the JSON string as the content parameter when creating the DataFrame element.
- 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()