How to upload dataframe with content of file upload? I did not find any documentation of this.
I want to upload a CSV file, display the data in an interactive table and then us the table conents to create a plot. But I am stuck with gluing the components together.
import gradio as gr
default_csv = "Phase,Activity,Start date,End date\n\"Mapping the Field\",\"Literature review\",2024-01-01,2024-01-31"
def process_csv_text(text):
df = pd.read_csv(StringIO(text), parse_dates=["Start date", "End date"])
return df
with gr.Blocks() as demo:
upload_button = gr.UploadButton(label="Upload Timetable", file_types = ['.csv'], live=True, file_count = "single")
table = gr.Dataframe(headers=["Phase", "Activity", "Start date", "End date"], col_count=4, default=process_csv_text(default_csv))
image = gr.Plot()
upload_button.click(fn=process_csv_text, inputs=upload_button, outputs=table, api_name="upload_csv")
demo.launch()
hi @soerendip ,
here is youβre code with a few fixes
differences
default β value
click β upload
process_csv_text(text: string)-> process_csv_text(file: tempfile._TemporaryFileWrapper)
so when it loads itβs a string, then is a temp file with a file.name the file path
import gradio as gr
import pandas as pd
from io import StringIO
default_csv = "Phase,Activity,Start date,End date\n\"Mapping the Field\",\"Literature review\",2024-01-01,2024-01-31"
def process_csv_text(temp_file):
if isinstance(temp_file, str):
df = pd.read_csv(StringIO(temp_file), parse_dates=["Start date", "End date"])
else:
df = pd.read_csv(temp_file.name, parse_dates=["Start date", "End date"])
print(df)
return df
with gr.Blocks() as demo:
upload_button = gr.UploadButton(label="Upload Timetable", file_types = ['.csv'], live=True, file_count = "single")
table = gr.Dataframe(headers=["Phase", "Activity", "Start date", "End date"], type="pandas", col_count=4, value=process_csv_text(default_csv))
image = gr.Plot()
upload_button.upload(fn=process_csv_text, inputs=upload_button, outputs=table, api_name="upload_csv")
demo.launch(debug=True)