tnt306 commited on
Commit
5ccc81c
·
1 Parent(s): 47d95c0

Test UploadButton

Browse files
Files changed (1) hide show
  1. app.py +15 -14
app.py CHANGED
@@ -1,5 +1,7 @@
1
  from typing import Dict
2
  from pathlib import Path
 
 
3
 
4
  import gradio as gr
5
 
@@ -15,21 +17,20 @@ title = "This is title."
15
  description = "This is description."
16
  article = "This is article."
17
 
18
- def upload_file(filepath):
19
- name = Path(filepath).name
20
- return [gr.UploadButton(visible=False), gr.DownloadButton(label=f"Download {name}", value=filepath, visible=True)]
21
 
22
- def download_file():
23
- return [gr.UploadButton(visible=True), gr.DownloadButton(visible=False)]
 
 
 
 
 
24
 
25
  with gr.Blocks() as demo:
26
- gr.Markdown("First upload a file and and then you'll be able download it (but only once!)")
27
- with gr.Row():
28
- u = gr.UploadButton("Upload a file", file_count="single")
29
- d = gr.DownloadButton("Download the file", visible=False)
30
 
31
- u.upload(upload_file, u, [u, d])
32
- d.click(download_file, None, [u, d])
33
-
34
- demo.launch(debug=False,
35
- share=True)
 
1
  from typing import Dict
2
  from pathlib import Path
3
+ import pandas as pd
4
+ from io import StringIO
5
 
6
  import gradio as gr
7
 
 
17
  description = "This is description."
18
  article = "This is article."
19
 
20
+ default_csv = "Phase,Activity,Start date,End date\n\"Mapping the Field\",\"Literature review\",2024-01-01,2024-01-31"
 
 
21
 
22
+ def process_csv_text(temp_file):
23
+ if isinstance(temp_file, str):
24
+ df = pd.read_csv(StringIO(temp_file), parse_dates=["Start date", "End date"])
25
+ else:
26
+ df = pd.read_csv(temp_file.name, parse_dates=["Start date", "End date"])
27
+ print(df)
28
+ return df
29
 
30
  with gr.Blocks() as demo:
31
+ upload_button = gr.UploadButton(label="Upload Timetable", file_types = ['.csv'], live=True, file_count = "single")
32
+ table = gr.Dataframe(headers=["Phase", "Activity", "Start date", "End date"], type="pandas", col_count=4, value=process_csv_text(default_csv))
33
+ image = gr.Plot()
34
+ upload_button.upload(fn=process_csv_text, inputs=upload_button, outputs=table, api_name="upload_csv")
35
 
36
+ demo.launch(debug=True)