Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
-
# In-memory
|
| 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 = {
|
|
@@ -15,12 +21,12 @@ def add_entry(username, countrycode, sbucode, brandcode, conceptcode, channel):
|
|
| 15 |
"CHANNEL": channel
|
| 16 |
}
|
| 17 |
df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
|
| 18 |
-
return
|
| 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
|
| 24 |
|
| 25 |
def delete_entry(index):
|
| 26 |
global df
|
|
@@ -28,11 +34,11 @@ def delete_entry(index):
|
|
| 28 |
idx = int(index)
|
| 29 |
if 0 <= idx < len(df):
|
| 30 |
df = df.drop(idx).reset_index(drop=True)
|
| 31 |
-
return
|
| 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
|
|
@@ -45,61 +51,87 @@ def update_entry(index, username, countrycode, sbucode, brandcode, conceptcode,
|
|
| 45 |
df.at[idx, "BRANDCODE"] = brandcode
|
| 46 |
df.at[idx, "CONCEPTCODE"] = conceptcode
|
| 47 |
df.at[idx, "CHANNEL"] = channel
|
| 48 |
-
return
|
| 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 |
-
|
|
|
|
|
|
|
| 56 |
|
| 57 |
with gr.Blocks() as app:
|
| 58 |
-
gr.
|
| 59 |
-
|
| 60 |
-
with gr.
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
+
# In-memory data store
|
| 5 |
df = pd.DataFrame(columns=["USERNAME", "COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL"])
|
| 6 |
|
| 7 |
+
def login(username, password):
|
| 8 |
+
if username == "admin" and password == "admin":
|
| 9 |
+
return gr.update(visible=False), gr.update(visible=True), True, show_data()
|
| 10 |
+
else:
|
| 11 |
+
return gr.update(visible=True, value="Invalid credentials"), gr.update(visible=False), False, pd.DataFrame()
|
| 12 |
+
|
| 13 |
def add_entry(username, countrycode, sbucode, brandcode, conceptcode, channel):
|
| 14 |
global df
|
| 15 |
new_row = {
|
|
|
|
| 21 |
"CHANNEL": channel
|
| 22 |
}
|
| 23 |
df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
|
| 24 |
+
return show_data()
|
| 25 |
|
| 26 |
def bulk_input(bulk_df):
|
| 27 |
global df
|
| 28 |
df = pd.concat([df, pd.DataFrame(bulk_df).dropna(how='all')], ignore_index=True)
|
| 29 |
+
return show_data()
|
| 30 |
|
| 31 |
def delete_entry(index):
|
| 32 |
global df
|
|
|
|
| 34 |
idx = int(index)
|
| 35 |
if 0 <= idx < len(df):
|
| 36 |
df = df.drop(idx).reset_index(drop=True)
|
| 37 |
+
return show_data()
|
| 38 |
else:
|
| 39 |
+
return gr.Textbox.update(value=f"Index {index} out of range"), show_data()
|
| 40 |
except Exception as e:
|
| 41 |
+
return gr.Textbox.update(value=f"Error: {e}"), show_data()
|
| 42 |
|
| 43 |
def update_entry(index, username, countrycode, sbucode, brandcode, conceptcode, channel):
|
| 44 |
global df
|
|
|
|
| 51 |
df.at[idx, "BRANDCODE"] = brandcode
|
| 52 |
df.at[idx, "CONCEPTCODE"] = conceptcode
|
| 53 |
df.at[idx, "CHANNEL"] = channel
|
| 54 |
+
return show_data()
|
| 55 |
else:
|
| 56 |
+
return gr.Textbox.update(value=f"Index {index} out of range"), show_data()
|
| 57 |
except Exception as e:
|
| 58 |
+
return gr.Textbox.update(value=f"Error: {e}"), show_data()
|
| 59 |
|
| 60 |
def show_data():
|
| 61 |
+
display_df = df.copy()
|
| 62 |
+
display_df.index.name = 'Index'
|
| 63 |
+
return display_df.reset_index()
|
| 64 |
|
| 65 |
with gr.Blocks() as app:
|
| 66 |
+
logged_in = gr.State(False)
|
| 67 |
+
|
| 68 |
+
with gr.Column(visible=True) as login_section:
|
| 69 |
+
gr.Markdown("## Login")
|
| 70 |
+
login_user = gr.Textbox(label="Username")
|
| 71 |
+
login_pass = gr.Textbox(label="Password", type="password")
|
| 72 |
+
login_btn = gr.Button("Login")
|
| 73 |
+
login_error = gr.Textbox(visible=False, interactive=False, show_label=False)
|
| 74 |
+
|
| 75 |
+
with gr.Column(visible=False) as main_section:
|
| 76 |
+
gr.Markdown("## CRUD App - Logged in as Admin")
|
| 77 |
+
|
| 78 |
+
with gr.Tab("Add Entry"):
|
| 79 |
+
with gr.Row():
|
| 80 |
+
username = gr.Textbox(label="USERNAME")
|
| 81 |
+
countrycode = gr.Textbox(label="COUNTRYCODE")
|
| 82 |
+
sbucode = gr.Textbox(label="SBUCODE")
|
| 83 |
+
brandcode = gr.Textbox(label="BRANDCODE")
|
| 84 |
+
conceptcode = gr.Textbox(label="CONCEPTCODE")
|
| 85 |
+
channel = gr.Textbox(label="CHANNEL")
|
| 86 |
+
add_btn = gr.Button("Add")
|
| 87 |
+
add_table = gr.Dataframe(label="Data", interactive=False)
|
| 88 |
+
|
| 89 |
+
with gr.Tab("Bulk Input"):
|
| 90 |
+
bulk_df = gr.Dataframe(
|
| 91 |
+
headers=["USERNAME", "COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL"],
|
| 92 |
+
row_count=5,
|
| 93 |
+
col_count=(6, "fixed"),
|
| 94 |
+
label="Paste or type your data here"
|
| 95 |
+
)
|
| 96 |
+
bulk_btn = gr.Button("Submit Bulk Data")
|
| 97 |
+
bulk_table = gr.Dataframe(label="Data", interactive=False)
|
| 98 |
+
|
| 99 |
+
with gr.Tab("Update Entry"):
|
| 100 |
+
upd_index = gr.Textbox(label="Index to Update")
|
| 101 |
+
upd_username = gr.Textbox(label="USERNAME")
|
| 102 |
+
upd_country = gr.Textbox(label="COUNTRYCODE")
|
| 103 |
+
upd_sbu = gr.Textbox(label="SBUCODE")
|
| 104 |
+
upd_brand = gr.Textbox(label="BRANDCODE")
|
| 105 |
+
upd_concept = gr.Textbox(label="CONCEPTCODE")
|
| 106 |
+
upd_channel = gr.Textbox(label="CHANNEL")
|
| 107 |
+
update_btn = gr.Button("Update Entry")
|
| 108 |
+
upd_table = gr.Dataframe(label="Data", interactive=False)
|
| 109 |
+
|
| 110 |
+
with gr.Tab("Delete Entry"):
|
| 111 |
+
del_index = gr.Textbox(label="Index to Delete")
|
| 112 |
+
del_btn = gr.Button("Delete")
|
| 113 |
+
del_status = gr.Textbox(visible=True, interactive=False, show_label=False)
|
| 114 |
+
del_table = gr.Dataframe(label="Data", interactive=False)
|
| 115 |
+
|
| 116 |
+
with gr.Tab("View Data"):
|
| 117 |
+
view_btn = gr.Button("Refresh Table")
|
| 118 |
+
view_table = gr.Dataframe(label="Data", interactive=False)
|
| 119 |
+
|
| 120 |
+
login_btn.click(fn=login, inputs=[login_user, login_pass],
|
| 121 |
+
outputs=[login_error, main_section, logged_in, view_table])
|
| 122 |
+
|
| 123 |
+
add_btn.click(add_entry,
|
| 124 |
+
inputs=[username, countrycode, sbucode, brandcode, conceptcode, channel],
|
| 125 |
+
outputs=add_table)
|
| 126 |
+
|
| 127 |
+
bulk_btn.click(bulk_input, inputs=bulk_df, outputs=bulk_table)
|
| 128 |
+
|
| 129 |
+
update_btn.click(update_entry,
|
| 130 |
+
inputs=[upd_index, upd_username, upd_country, upd_sbu, upd_brand, upd_concept, upd_channel],
|
| 131 |
+
outputs=[upd_table])
|
| 132 |
+
|
| 133 |
+
del_btn.click(delete_entry, inputs=del_index, outputs=[del_status, del_table])
|
| 134 |
+
|
| 135 |
+
view_btn.click(show_data, outputs=view_table)
|
| 136 |
|
| 137 |
app.launch()
|