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 1 year ago • 5 comments
trafficstars

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

@desertproject Nice one :)

One question though: Instead of expecting (forcing) the user to serialize the dataframe to JSON in a specific orientation, why don't we just accept the DataFrame element and call to_json with orient="split" ourselves behind the scenes, for example in the __post_init__ method?

This way people wouldn't have to serialize/deserialize their dataframes throughout and could just pass a DataFrame at any point without having to worry about the underlying implementation.

hadarsharon avatar Sep 25 '24 07:09 hadarsharon

@desertproject Cool feature! Could you please create an E2E test for it, so we can support it towards the future? I'd also love some ~~screenshots/screengrab of how it is supposed to work and/or~~ a cookbook entry. 🙏🏼 🥺

(Forget the screenshot, I just seen it in the issue.)

dokterbob avatar Sep 25 '24 11:09 dokterbob

Instead of expecting (forcing) the user to serialize the dataframe to JSON in a specific orientation, why don't we just accept the DataFrame element and call to_json with orient="split" ourselves behind the scenes, for example in the __post_init__ method?

@hadarsharon That's a great idea! I had thought of something similar but wasn’t sure exactly where it would fit. Using the __post_init__ method for this makes perfect sense. Thank you for the suggestion!

desertproject avatar Sep 25 '24 11:09 desertproject

Could you please create an E2E test for it, so we can support it towards the future? I'd also love a cookbook entry. 🙏🏼 🥺

@dokterbob I don’t have any experience with E2E testing or Cypress, but I’d be happy to give it a try! Could you provide some guidance on the specific tests you’d like to see? I can also take care of the cookbook entry without any problem.

desertproject avatar Sep 25 '24 12:09 desertproject

Could you please create an E2E test for it, so we can support it towards the future? I'd also love a cookbook entry. 🙏🏼 🥺

@dokterbob I don’t have any experience with E2E testing or Cypress, but I’d be happy to give it a try! Could you provide some guidance on the specific tests you’d like to see? I can also take care of the cookbook entry without any problem.

Basically, you'd create a test suite similar to the ones already there: https://github.com/Chainlit/chainlit/tree/main/cypress/e2e

Create a test project setup to use your feature, then test everything you'd normally test for it. E.g. return a dataframe, ensure it's properly rendered. If there's interaction possible, simulate it.

I highly recommend to use something like Claude if you haven't written tests before (but of course, check the work for common slipups!).

Similarly unit tests for Python code would be great as well. Note how the unit tests are laid out to match the Python modules.

dokterbob avatar Sep 26 '24 15:09 dokterbob

When will a new tag for this change be released? Eager to try it out:)

sundar-sarvam avatar Oct 19 '24 14:10 sundar-sarvam

@sundar-sarvam Should be in today's 2.0.dev1 pre-release.

@desertproject We'd love a PR also for the docs https://github.com/Chainlit/docs!

dokterbob avatar Oct 22 '24 09:10 dokterbob

I'll take care of it!

desertproject avatar Oct 22 '24 20:10 desertproject

hi everyone, it seems like i cant use the cl.Dataframe,

File "E:\project.venv1\Lib\site-packages\chainlit\utils.py", line 70, in getattr module_path = registry[name] ~~~~~~~~^^^^^^ KeyError: 'Dataframe'

can anyone help? also, im using Chainlit, version 1.1.402

wac4s avatar Mar 24 '25 08:03 wac4s