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

How can I set the sorting field in gr.Dataframe? #10296

Open
q275343119 opened this issue Jan 6, 2025 · 2 comments
Open

How can I set the sorting field in gr.Dataframe? #10296

q275343119 opened this issue Jan 6, 2025 · 2 comments

Comments

@q275343119
Copy link

My use case is as follows:

I want to control the sorting field of the table through a dropdown menu. Suppose I choose to sort based on col_1. I will first sort the DataFrame (df), then pass the sorted df to gr.Dataframe. However, if I click on another column header in the table to sort it, the sorting by the dropdown becomes meaningless. This is because, no matter how I sort the df, once it is passed to gr.Dataframe, it will sort according to the column header selected on the interface.

Is there a parameter in gr.Dataframe that allows controlling the sorting field and the order (ascending or descending)?

Additional Info: I am using Gradio version 4.20.0.

@abidlabs
Copy link
Member

abidlabs commented Jan 6, 2025

Hi @q275343119, I'm not sure I understand the question, you are already sorting the dataframe and passing the result to the gr.DataFrame component, right? So it is already being displayed in the correct order? Are you trying to prevent users from being able to sort the gr.DataFrame by clicking on a different column?

cc @hannahblair for visibility

@q275343119
Copy link
Author

Hi @q275343119, I'm not sure I understand the question, you are already sorting the dataframe and passing the result to the gr.DataFrame component, right? So it is already being displayed in the correct order? Are you trying to prevent users from being able to sort the gr.DataFrame by clicking on a different column?

cc @hannahblair for visibility

Apologies, I didn't express myself clearly. I have attached the demo code for your reference:

import gradio as gr
import pandas as pd

# Sample DataFrame
data = {
    "Name": ["Alice", "Bob", "Charlie", "David"],
    "Age": [25, 45, 35, 40],
    "Score": [88, 92, 95, 85]
}
df = pd.DataFrame(data)

# Function to sort the DataFrame based on the selected column
def sort_dataframe(df, column_name):
    sorted_df = df.sort_values(by=column_name)
    return sorted_df

# Gradio Interface
def update_dataframe(column_name):
    sorted_df = sort_dataframe(df, column_name)
    return sorted_df

iface = gr.Interface(
    fn=update_dataframe,
    inputs=[gr.Dropdown(choices=df.columns.tolist(), label="Sort by")],
    outputs=gr.DataFrame(),
    live=True
)

iface.launch()

Let me now describe my scenario. First, I select age from the dropdown to sort, then click on the header of name to sort by that column. After this, no matter whether I try to sort by age or score, it doesn't work as expected.
The GIF is shown below:
chrome-capture-2025-1-7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants