Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for displaying pandas DataFrame as an interactive table #1373

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

desertproject
Copy link

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()

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. backend Pertains to the Python backend. frontend Pertains to the frontend. labels Sep 24, 2024
@hadarsharon
Copy link

@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.

@dokterbob
Copy link
Collaborator

dokterbob commented Sep 25, 2024

@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.)

@desertproject
Copy link
Author

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
Copy link
Author

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 desertproject changed the title Add support for displaying pandas DataFrame as a table Add support for displaying pandas DataFrame as an interactive table Sep 25, 2024
- Updated Dataframe class to accept pandas DataFrame directly.
- Automatically serialize DataFrame to JSON with orient="split" in __post_init__.
- Simplified interface, removing the need for users to manually serialize data.
- Added loading state to DataGrid using the loading prop.
- Refactored data parsing logic to handle loading more cleanly.
@dokterbob
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Pertains to the Python backend. frontend Pertains to the frontend. size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants