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

Merged
merged 9 commits into from
Oct 16, 2024

Conversation

desertproject
Copy link
Contributor

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

desertproject and others added 7 commits October 14, 2024 11:33
- 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.
- Created sample DataFrame with 15 rows (more than 10) in main.py to test pagination
- Added Cypress test to validate Dataframe rendering, sorting, pagination, and rows per page functionality
Copy link
Collaborator

@dokterbob dokterbob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely looks good to me.

I'd like to ask @willydouhard to take a quick look at it as well, other than that we're good to merge!

@dokterbob dokterbob added the review-me Ready for review! label Oct 14, 2024
@dokterbob dokterbob merged commit f6159d5 into Chainlit:main Oct 16, 2024
16 checks passed
@sundar-sarvam
Copy link

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

@hayescode hayescode mentioned this pull request Oct 21, 2024
@dokterbob
Copy link
Collaborator

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

@desertproject
Copy link
Contributor Author

I'll take care of it!

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. review-me Ready for review! size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants