Skip to content
New issue

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

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

Already on GitHub? # to your account

Gradio load update dataframe have some bug after 4.16 version #8160

Closed
1 task done
littleping1987 opened this issue Apr 29, 2024 · 12 comments · Fixed by #10360
Closed
1 task done

Gradio load update dataframe have some bug after 4.16 version #8160

littleping1987 opened this issue Apr 29, 2024 · 12 comments · Fixed by #10360
Assignees
Labels
bug Something isn't working 💾 Dataframe Regression Bugs did not exist in previous versions of Gradio

Comments

@littleping1987
Copy link

Describe the bug

In Gradio 4.16, this feature worked well. However, in the latest version, there was an issue with updating the data in the Dataframe. When the table data changes, the Dataframe can recognize the data in a timely manner and add new ones. However, when the data already exists and the data status is updated, it cannot be updated and the page needs to be refreshed.

Have you searched existing issues? 🔎

  • I have searched and found no existing issues

Reproduction

import gradio as gr

with gr.Blocks() as B:
    with gr.Tab("🔎扫描任务管理", elem_id="tab3"):
        with gr.Column(elem_id='column3'):
            task_info = gr.DataFrame(label='🕒历史扫描任务列表',
                                     headers=["任务ID", "任务名称", "报告名称", "创建时间", '任务状态'],
                                     datatype=["str", 'str', 'str', 'str', 'str'],
                                     elem_id="dataframe-task", interactive=True)

    B.load(show_scan_result, outputs=[task_info], every=10)

Screenshot

953e95fbffb2ee5efca7ff905f325374

Logs

No response

System Info

The latest version

Severity

Blocking usage of gradio

@littleping1987 littleping1987 added the bug Something isn't working label Apr 29, 2024
@littleping1987
Copy link
Author

maybe apply_diff in B.Load has some bug

@abidlabs
Copy link
Member

Hi @littleping1987 thanks for including the code snippet. It would be great if you can include a function show_scan_result with mock data so that we can immediately repro this issue. But we'll take a look at it!

@abidlabs abidlabs added the Regression Bugs did not exist in previous versions of Gradio label Apr 29, 2024
@sfeltman
Copy link

sfeltman commented Jun 29, 2024

@abidlabs Hello, I am also running into something similar:

import pandas as pd
import gradio as gr
from random import randrange

_data = {
    "Freddy": 100,
    "Maria": 80,
    "Mark": 10,
}

def get_scores():
    for name in _data.keys():
        _data[name] = randrange(start=100)
    return pd.DataFrame({
                            "Score": _data.values(),
                            "Name": _data.keys(),
                        })

with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            gr.DataFrame(get_scores, every=5)

demo.queue().launch()

I would expect the dataframe to redraw as the data changes. Also it does not refresh when rows are removed, only added.

@sfeltman
Copy link

I confirmed this is indeed a regression when moving from 4.16 to 4.17.

@sfeltman
Copy link

Git bisect reveals the following commit as the problem:
68a54a7

@abidlabs
Copy link
Member

Hi, apologies for the late follow up. We haven't had a chance to look into this issue, but the Gradio codebase has changed quite significantly since this issue was created, particularly with the release of Gradio 5. Could you let us know if this is still an issue in the latest version of Gradio (pip install --upgrade gradio)? Thanks!

@mahdi13
Copy link

mahdi13 commented Oct 18, 2024

Hi @abidlabs , I'm facing the similar issues, exactly after 4.16.0 (4.16.0 is that last version working). And I checked recently, the problem still exists in gradio==5.1.0

To reproduce please try the below code with once gradio==5.1.0 and once gradio==4.16.0.
Here is a sample code:

import asyncio

import gradio as gr
import pandas as pd


# Create and initialize the dataframe with "Status" column
# Coroutine to update the dataframe, row by row, every 2 seconds
async def process_dataframe(df):
    for i in range(df.shape[0]):
        # Print the update process for the current row
        print(f"Updating row {i}...")

        # Update the status of the current row
        df.at[i, "Status"] = "Processing"

        # Yield the updated dataframe to the UI
        yield df

        # Print that the yield for the current row is finished
        print(f"Yield finished for row {i}")

        # Sleep for 2 seconds before processing the next row
        await asyncio.sleep(2)

    print("All rows updated!")


# Gradio interface
def create_my_test_block():
    df = pd.DataFrame({
        "Column A": [1, 2, 3],
        "Column B": [4, 5, 6],
        "Status": ["TODO", "TODO", "TODO"]
    })

    with gr.Blocks(title="My Block") as demo:
        # Display the dataframe as output
        dataframe_output = gr.Dataframe(df)

        # Button to start the update process
        start_button = gr.Button("Start Updating")

        # Directly pass the coroutine function to the button's click handler
        start_button.click(process_dataframe, inputs=[dataframe_output], outputs=[dataframe_output])

    return demo


# Launch the Gradio interface
demo = create_my_test_block()
demo.launch()

You can see the table doesn't get updated after the first update.
Let me know if you needed any more clarification!

@yan123456jie
Copy link

i also facing this issues, my version is 5.1.0

import json
import time
import gradio as gr
import pandas as pd


def llm_fun():
    df = pd.DataFrame(["init"], columns=['c'])
    yield df
    time.sleep(1)
    df = pd.DataFrame(["init1"], columns=['c'])
    yield df
    time.sleep(1)
    df = pd.DataFrame(["init2"], columns=['c'])
    yield df

def get_iface():
    with gr.Blocks() as iface:
        with gr.Row():
            submit_button = gr.Button("submit")
        with gr.Tabs():
            with gr.Row():
                resource_df = gr.DataFrame(
                    value=pd.DataFrame(columns=['c']),
                    every=1
                )

        submit_button.click(
            llm_fun,
            inputs=[],
            outputs=[resource_df],
            concurrency_limit=20
        )

    return iface

iface = get_iface()
iface.launch(server_name="0.0.0.0", `server_port=8864)`

@abidlabs
Copy link
Member

We'll look into this issue after the holidays!

@AndriiZelenko
Copy link

Have not been fixed as for this one:

import pandas as pd
import gradio as gr
import random


data = {}
generate_table = False
table_generated = False

def main():
    global data, generate_table, table_generated
    data = {}
    while True:
        if generate_table and not table_generated: 
  
            table_generated = True

            num_rows = random.randint(5, 15)
            data = {str(k) : [num_rows]  for k in range(num_rows)}
            # data = {'column' : [num_rows] * num_rows}
 
        if not generate_table:
            data = {}
       
        yield pd.DataFrame(data)

def change_state():
    global generate_table, table_generated, data
    generate_table = not generate_table
    table_generated = False
    data = {}

    print("State reset: data =", data)
    yield pd.DataFrame(data)

with gr.Blocks() as app:
    with gr.Row():
     
        change_button = gr.Button("Main")
        generate_button = gr.Button("generate")
        result_back_table = gr.DataFrame(interactive=False, label="Detection Per Item")
        
          

    change_button.click(fn=main, inputs=[], outputs=[result_back_table])

    generate_button.click(fn = change_state, inputs = [], outputs = [result_back_table])


app.launch()

upon second click of generate(refreshing table = {})) it keep some values from previous state @abidlabs take a look on this one please

@abidlabs
Copy link
Member

I'm quite confused with that code example @AndriiZelenko, what is it that you're trying to do exactly? Can you explain the expected behavior or provide a simpler repro perhaps without the globals?

@AndriiZelenko
Copy link

AndriiZelenko commented Feb 12, 2025

I'm quite confused with that code example @AndriiZelenko, what is it that you're trying to do exactly? Can you explain the expected behavior or provide a simpler repro perhaps without the globals?

Yep, so general idea is, i would like to create a gradio demo app, which will continuously read-write some data(like a video stream app), then upon button click, i would like to upload result of parsed data to dataframe and keep it there, while still running my while loop of reading-writing data. Then upon clicking button again, it should reset dataframe value to initial state, but what it really does it keep some random values in dataframe.

  1. click main to start loop

  2. click generate to get initial version of dataframe
    Image

  3. click generate to reset dataframe
    Image

and this one still contains some random values in the end of dataframe
@abidlabs

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working 💾 Dataframe Regression Bugs did not exist in previous versions of Gradio
Projects
None yet
6 participants