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

Better handling of empty dataframe in gr.DataFrame #5256

Merged
merged 11 commits into from Aug 18, 2023
Merged

Better handling of empty dataframe in gr.DataFrame #5256

merged 11 commits into from Aug 18, 2023

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Aug 17, 2023

We were handling empty dataframes incorrectly in the gr.Dataframe --> we were turning them into single-row arrays with each cell being "".

This fixes that -- would appreciate if folks could test this out to make sure I didn't miss anything as the dataframe frontend code is quite complex.

Fixes: #5162

Test code:

import gradio as gr
import pandas as pd

df_orig = pd.DataFrame([], columns=['a', 'b', 'c'])

df_fill = pd.DataFrame([[1, 2, 3]], columns=['a', 'b', 'c'])


with gr.Blocks() as demo:
    df = gr.Dataframe(value=df_orig, interactive=True, row_count=(0, "dynamic"))
    df2 = gr.Dataframe(value=df_fill, interactive=True, row_count=(0, "dynamic"))
    btn = gr.Button()
    text = gr.Textbox()
    btn.click(fn=lambda x: x.shape, inputs=df, outputs=text)
demo.queue().launch()

Also adds some stories for gr.DataFrame to prevent regressions like #5236 and some stories (unrelatedly) for gr.Dropdown()

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Aug 17, 2023

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
Visual tests all good! Build review
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/8bc5dded137d309c1cc3319456ebf1019d0a9236/gradio-3.40.1-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@8bc5dded137d309c1cc3319456ebf1019d0a9236#subdirectory=client/python"

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Aug 17, 2023

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/dataframe patch
gradio patch
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Better handling of empty dataframe in gr.DataFrame

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@abidlabs abidlabs mentioned this pull request Aug 17, 2023
1 task
@abidlabs abidlabs marked this pull request as ready for review August 17, 2023 23:34
@pngwn
Copy link
Member

pngwn commented Aug 18, 2023

This is interesting. So I'm not convinced this issue is actually a bug, it was by design. The reason some empty rows are rendered is so a user can click into the cells and start typing, tabbing, etc. With no rows present this isn't possible.

@abidlabs
Copy link
Member Author

The reason some empty rows are rendered is so a user can click into the cells and start typing, tabbing, etc. With no rows present this isn't possible.

We shouldn't be mutating the data to achieve this. As it stands, if you pass in a dataframe with 0 rows into a gr.DataFrame and try to get it back, it will have a single row, which can cause problems for downstream processing.

I don't think the UX issue is really there either, since we include a prominent "Add Row" button underneath the dataframe (as long as interactive=True).

@pngwn
Copy link
Member

pngwn commented Aug 18, 2023

Yeah, that makes sense.

Copy link
Member

@pngwn pngwn left a comment

Choose a reason for hiding this comment

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

Looks good. Thanks for fixing this @abidlabs!

@abidlabs
Copy link
Member Author

Thanks @pngwn!

@abidlabs abidlabs merged commit 933db53 into main Aug 18, 2023
20 checks passed
@abidlabs abidlabs deleted the df branch August 18, 2023 15:20
@pngwn pngwn mentioned this pull request Aug 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Empty gr.Dataframe is not empty
3 participants