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

gr.Dataframe style is not working on gradio 5.16.0 #10572

Open
1 task done
jamie0725 opened this issue Feb 12, 2025 · 2 comments · May be fixed by #10597
Open
1 task done

gr.Dataframe style is not working on gradio 5.16.0 #10572

jamie0725 opened this issue Feb 12, 2025 · 2 comments · May be fixed by #10597
Assignees
Labels
bug Something isn't working 💾 Dataframe Priority High priority issues Regression Bugs did not exist in previous versions of Gradio

Comments

@jamie0725
Copy link

Describe the bug

As described in: https://www.gradio.app/guides/styling-the-gradio-dataframe, previously the style can display properly. But now it doesn't work in the FIRST run anymore. If run multiple times, the style can show properly.

Please check the code below and run it in https://www.gradio.app/playground for reproduction.

Have you searched existing issues? 🔎

  • I have searched and found no existing issues

Reproduction

import pandas as pd 
import gradio as gr

# Creating a sample dataframe
def run():
  df = pd.DataFrame({
      "A" : [14, 4, 5, 4, -1], 
      "B" : [5, 2, 54, 3, 2], 
      "C" : [20, 20, 7, 3, 8], 
      "D" : [14, 3, 6, 2, 6], 
      "E" : [23, 45, 64, 32, 23]
  }) 
  df = df.style.map(color_num, subset=["A"])

  return df

# Function to apply text color

def color_num(value: float) -> str:
    color = "red" if value >= 0 else "green"
    color_style = "color: {}".format(color)

    return color_style

# Displaying the styled dataframe in Gradio
with gr.Blocks() as demo:
    gr.Textbox("{}".format(gr.__version__))
    a = gr.DataFrame()
    b = gr.Button("run")
    b.click(run,outputs=a)
    
demo.launch()

Screenshot

Image Image

Logs

System Info

gradio 5.16.0

Severity

I can work around it

@jamie0725 jamie0725 added the bug Something isn't working label Feb 12, 2025
@abidlabs
Copy link
Member

I can reproduce this, thanks @jamie0725. We'll fix

@abidlabs abidlabs added Regression Bugs did not exist in previous versions of Gradio 💾 Dataframe labels Feb 12, 2025
@abidlabs abidlabs self-assigned this Feb 14, 2025
@abidlabs abidlabs added the Priority High priority issues label Feb 14, 2025
@abidlabs abidlabs linked a pull request Feb 14, 2025 that will close this issue
@BornaKhedri
Copy link

BornaKhedri commented Feb 15, 2025

Hi @abidlabs , I'm encountering a similar issue, but unlike OP, my styler renders correctly the first time, but not on subsequent dropdown changes. I can confirm style_dataframe() is being called each time the dropdown value changes (I verified this by adding some print statements in the styler function), but the styling only appears on the first render. I've included reproducible code below. Please let me know if I should open a separate issue.

p.s. I know when interactive=True, the gr.Dataframe won't render the styling altogether, therefore I've set it to False.

Reproducible Code

import pandas as pd 
import gradio as gr
import matplotlib.colors as mcolors
import matplotlib.pyplot as plt

df = pd.DataFrame({
    "num" : [1, 2 , 3, 4, 5, 6, 7, 8, 9, 10], 
    "channel" : ["alpha", "beta", "beta", "beta", "alpha", "beta", "alpha", "beta", "alpha", "alpha"], 
    "sort_zscore" : [-0.5, -0.3, -0.25, 0.25, 0.4, 0.5, 0.7, 0.75, 0.9, 1.0],
    "D" : [20, 20, 7, 3, 8, 20, 20, 7, 3, 8], 
    "E" : [14, 3, 6, 2, 6, 20, 20, 7, 3, 8]
})

def style_dataframe(df):
    # Compute vmin, vmax from zscore column
    vmin = df['sort_zscore'].min()
    vmax = df['sort_zscore'].max()

    # Create a normalizer and a colormap
    norm = mcolors.Normalize(vmin=vmin, vmax=vmax)
    # cmap = plt.get_cmap('YlGnBu')
    cmap = plt.get_cmap('PuBu')

    def color_row(row):
        """
        For each row:
          1. Get the row's zscore
          2. Normalize it
          3. Convert the normalized value into a hex color from the colormap
          4. Return a list of 'background-color: ...' for each cell
        """
        zscore = row['sort_zscore']
        normalized = norm(zscore)
        color = mcolors.to_hex(cmap(normalized))
        return [f'background-color: {color}'] * len(row)

    # Apply our row-coloring function across every row
    return df.style.apply(color_row, axis=1)

def update_table(channel):
    # Filter based on the selected channel
    filtered_df = df[df["channel"] == channel]
    return style_dataframe(filtered_df)

with gr.Blocks() as demo:
  channel_dropdown = gr.Dropdown(label="Select Channel", choices=["alpha", "beta"], value="beta")
  filtered_table = gr.DataFrame(update_table(channel_dropdown.value), interactive=False, show_search='filter')
  channel_dropdown.change(fn=update_table, inputs=channel_dropdown, outputs=filtered_table)

if __name__ == "__main__":
    demo.launch()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working 💾 Dataframe Priority High priority issues Regression Bugs did not exist in previous versions of Gradio
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants