Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
|
|
|
|
| 8 |
"USERNAME": username,
|
| 9 |
"COUNTRYCODE": countrycode,
|
| 10 |
"SBUCODE": sbucode,
|
|
@@ -12,52 +14,92 @@ def create_entry(username, countrycode, sbucode, brandcode, conceptcode, channel
|
|
| 12 |
"CONCEPTCODE": conceptcode,
|
| 13 |
"CHANNEL": channel
|
| 14 |
}
|
| 15 |
-
|
| 16 |
-
return
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def delete_entry(index):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
try:
|
| 23 |
-
|
| 24 |
-
if 0 <=
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
else:
|
| 28 |
-
return "
|
| 29 |
-
except:
|
| 30 |
-
return "
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
|
| 4 |
+
# In-memory storage
|
| 5 |
+
df = pd.DataFrame(columns=["USERNAME", "COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL"])
|
| 6 |
|
| 7 |
+
def add_entry(username, countrycode, sbucode, brandcode, conceptcode, channel):
|
| 8 |
+
global df
|
| 9 |
+
new_row = {
|
| 10 |
"USERNAME": username,
|
| 11 |
"COUNTRYCODE": countrycode,
|
| 12 |
"SBUCODE": sbucode,
|
|
|
|
| 14 |
"CONCEPTCODE": conceptcode,
|
| 15 |
"CHANNEL": channel
|
| 16 |
}
|
| 17 |
+
df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
|
| 18 |
+
return df
|
| 19 |
|
| 20 |
+
def bulk_input(bulk_df):
|
| 21 |
+
global df
|
| 22 |
+
df = pd.concat([df, pd.DataFrame(bulk_df).dropna(how='all')], ignore_index=True)
|
| 23 |
+
return df
|
| 24 |
|
| 25 |
def delete_entry(index):
|
| 26 |
+
global df
|
| 27 |
+
try:
|
| 28 |
+
idx = int(index)
|
| 29 |
+
if 0 <= idx < len(df):
|
| 30 |
+
df = df.drop(idx).reset_index(drop=True)
|
| 31 |
+
return df
|
| 32 |
+
else:
|
| 33 |
+
return f"Index {index} out of range"
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"Error: {e}"
|
| 36 |
+
|
| 37 |
+
def update_entry(index, username, countrycode, sbucode, brandcode, conceptcode, channel):
|
| 38 |
+
global df
|
| 39 |
try:
|
| 40 |
+
idx = int(index)
|
| 41 |
+
if 0 <= idx < len(df):
|
| 42 |
+
df.at[idx, "USERNAME"] = username
|
| 43 |
+
df.at[idx, "COUNTRYCODE"] = countrycode
|
| 44 |
+
df.at[idx, "SBUCODE"] = sbucode
|
| 45 |
+
df.at[idx, "BRANDCODE"] = brandcode
|
| 46 |
+
df.at[idx, "CONCEPTCODE"] = conceptcode
|
| 47 |
+
df.at[idx, "CHANNEL"] = channel
|
| 48 |
+
return df
|
| 49 |
else:
|
| 50 |
+
return f"Index {index} out of range"
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return f"Error: {e}"
|
| 53 |
+
|
| 54 |
+
def show_data():
|
| 55 |
+
return df
|
| 56 |
+
|
| 57 |
+
with gr.Blocks() as app:
|
| 58 |
+
gr.Markdown("## CRUD App with Excel-style Bulk Input")
|
| 59 |
+
|
| 60 |
+
with gr.Tab("Add Entry"):
|
| 61 |
+
with gr.Row():
|
| 62 |
+
username = gr.Textbox(label="USERNAME")
|
| 63 |
+
countrycode = gr.Textbox(label="COUNTRYCODE")
|
| 64 |
+
sbucode = gr.Textbox(label="SBUCODE")
|
| 65 |
+
brandcode = gr.Textbox(label="BRANDCODE")
|
| 66 |
+
conceptcode = gr.Textbox(label="CONCEPTCODE")
|
| 67 |
+
channel = gr.Textbox(label="CHANNEL")
|
| 68 |
+
add_btn = gr.Button("Add")
|
| 69 |
+
add_btn.click(add_entry,
|
| 70 |
+
inputs=[username, countrycode, sbucode, brandcode, conceptcode, channel],
|
| 71 |
+
outputs=gr.Dataframe())
|
| 72 |
+
|
| 73 |
+
with gr.Tab("Bulk Input (Excel-like)"):
|
| 74 |
+
bulk_df = gr.Dataframe(
|
| 75 |
+
headers=["USERNAME", "COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL"],
|
| 76 |
+
row_count=5,
|
| 77 |
+
col_count=(6, "fixed"),
|
| 78 |
+
label="Paste or type your data here"
|
| 79 |
+
)
|
| 80 |
+
bulk_btn = gr.Button("Submit Bulk Data")
|
| 81 |
+
bulk_btn.click(bulk_input, inputs=bulk_df, outputs=gr.Dataframe())
|
| 82 |
+
|
| 83 |
+
with gr.Tab("Update Entry"):
|
| 84 |
+
upd_index = gr.Textbox(label="Index to Update")
|
| 85 |
+
upd_username = gr.Textbox(label="USERNAME")
|
| 86 |
+
upd_country = gr.Textbox(label="COUNTRYCODE")
|
| 87 |
+
upd_sbu = gr.Textbox(label="SBUCODE")
|
| 88 |
+
upd_brand = gr.Textbox(label="BRANDCODE")
|
| 89 |
+
upd_concept = gr.Textbox(label="CONCEPTCODE")
|
| 90 |
+
upd_channel = gr.Textbox(label="CHANNEL")
|
| 91 |
+
update_btn = gr.Button("Update Entry")
|
| 92 |
+
update_btn.click(update_entry,
|
| 93 |
+
inputs=[upd_index, upd_username, upd_country, upd_sbu, upd_brand, upd_concept, upd_channel],
|
| 94 |
+
outputs=gr.Dataframe())
|
| 95 |
+
|
| 96 |
+
with gr.Tab("Delete Entry"):
|
| 97 |
+
del_index = gr.Textbox(label="Index to Delete")
|
| 98 |
+
del_btn = gr.Button("Delete")
|
| 99 |
+
del_btn.click(delete_entry, inputs=del_index, outputs=gr.Dataframe())
|
| 100 |
+
|
| 101 |
+
with gr.Tab("View Data"):
|
| 102 |
+
view_btn = gr.Button("Refresh Table")
|
| 103 |
+
view_btn.click(show_data, outputs=gr.Dataframe())
|
| 104 |
+
|
| 105 |
+
app.launch()
|