Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,72 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
-
#
|
| 5 |
df = pd.DataFrame(columns=["USERNAME", "COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL", "REMARK"])
|
| 6 |
-
users_df = pd.DataFrame([{"USERNAME": "admin", "PASSWORD": "admin"}])
|
| 7 |
|
| 8 |
-
# === Utility ===
|
| 9 |
def to_upper(df):
|
| 10 |
return df.applymap(lambda x: str(x).upper() if pd.notnull(x) else x)
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
return
|
| 16 |
|
| 17 |
def show_users():
|
| 18 |
return users_df.copy()
|
| 19 |
|
| 20 |
-
# === Login ===
|
| 21 |
def login(username, password):
|
| 22 |
username = username.strip().upper()
|
| 23 |
password = password.strip()
|
| 24 |
matched = users_df[(users_df["USERNAME"] == username) & (users_df["PASSWORD"] == password)]
|
| 25 |
if not matched.empty:
|
| 26 |
-
return "", gr.update(visible=False), gr.update(visible=True), True,
|
| 27 |
else:
|
| 28 |
return "Invalid credentials", gr.update(visible=True), gr.update(visible=False), False, pd.DataFrame(), pd.DataFrame()
|
| 29 |
|
| 30 |
-
# === Bulk Create ===
|
| 31 |
def bulk_create_or_update(bulk_df):
|
| 32 |
global df
|
| 33 |
bulk_df = pd.DataFrame(bulk_df).dropna(how='all')
|
| 34 |
if bulk_df.empty:
|
| 35 |
-
return
|
| 36 |
|
| 37 |
bulk_df = to_upper(bulk_df)
|
| 38 |
-
|
| 39 |
required_cols = ["USERNAME", "COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL", "REMARK"]
|
| 40 |
bulk_df = bulk_df[required_cols].dropna()
|
| 41 |
-
|
| 42 |
-
# Remove duplicates
|
| 43 |
pk_cols = required_cols
|
| 44 |
bulk_df = bulk_df.drop_duplicates(subset=pk_cols)
|
| 45 |
-
|
| 46 |
-
# Only add rows not already in df
|
| 47 |
temp_df = pd.concat([df, bulk_df], ignore_index=True)
|
| 48 |
df = temp_df.drop_duplicates(subset=pk_cols, keep=False)
|
| 49 |
df = pd.concat([df, bulk_df], ignore_index=True)
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
# === Soft Delete using selected index ===
|
| 54 |
-
def delete_selected_rows(selected_rows):
|
| 55 |
global df
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
def add_user(new_username, new_password):
|
| 69 |
global users_df
|
| 70 |
new_username = new_username.strip().upper()
|
|
@@ -84,7 +86,6 @@ def delete_user(del_username):
|
|
| 84 |
users_df = users_df[users_df["USERNAME"] != del_username].reset_index(drop=True)
|
| 85 |
return f"User {del_username} dihapus.", show_users()
|
| 86 |
|
| 87 |
-
# === UI ===
|
| 88 |
with gr.Blocks() as app:
|
| 89 |
logged_in = gr.State(False)
|
| 90 |
|
|
@@ -107,7 +108,7 @@ with gr.Blocks() as app:
|
|
| 107 |
)
|
| 108 |
bulk_btn = gr.Button("Submit Data")
|
| 109 |
|
| 110 |
-
result_table = gr.Dataframe(label="
|
| 111 |
delete_btn = gr.Button("Delete Selected Rows")
|
| 112 |
delete_status = gr.Textbox(visible=True, interactive=False, show_label=False)
|
| 113 |
|
|
@@ -125,13 +126,10 @@ with gr.Blocks() as app:
|
|
| 125 |
user_status = gr.Textbox(visible=True, interactive=False, show_label=False)
|
| 126 |
user_table = gr.Dataframe(label="User List", interactive=False)
|
| 127 |
|
| 128 |
-
# === Events ===
|
| 129 |
login_btn.click(fn=login, inputs=[login_user, login_pass],
|
| 130 |
outputs=[login_error, login_section, main_section, logged_in, result_table, user_table])
|
| 131 |
-
|
| 132 |
bulk_btn.click(fn=bulk_create_or_update, inputs=bulk_df, outputs=result_table)
|
| 133 |
delete_btn.click(fn=delete_selected_rows, inputs=result_table, outputs=[delete_status, result_table])
|
| 134 |
-
|
| 135 |
add_user_btn.click(fn=add_user, inputs=[new_username, new_password], outputs=[user_status, user_table])
|
| 136 |
del_user_btn.click(fn=delete_user, inputs=del_user, outputs=[user_status, user_table])
|
| 137 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
+
# In-memory data
|
| 5 |
df = pd.DataFrame(columns=["USERNAME", "COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL", "REMARK"])
|
| 6 |
+
users_df = pd.DataFrame([{"USERNAME": "admin", "PASSWORD": "admin"}])
|
| 7 |
|
|
|
|
| 8 |
def to_upper(df):
|
| 9 |
return df.applymap(lambda x: str(x).upper() if pd.notnull(x) else x)
|
| 10 |
|
| 11 |
+
def get_active_data_with_select():
|
| 12 |
+
active = df[df["REMARK"] != "DELETE"].copy()
|
| 13 |
+
active["SELECT"] = False
|
| 14 |
+
return active.reset_index()
|
| 15 |
|
| 16 |
def show_users():
|
| 17 |
return users_df.copy()
|
| 18 |
|
|
|
|
| 19 |
def login(username, password):
|
| 20 |
username = username.strip().upper()
|
| 21 |
password = password.strip()
|
| 22 |
matched = users_df[(users_df["USERNAME"] == username) & (users_df["PASSWORD"] == password)]
|
| 23 |
if not matched.empty:
|
| 24 |
+
return "", gr.update(visible=False), gr.update(visible=True), True, get_active_data_with_select(), show_users()
|
| 25 |
else:
|
| 26 |
return "Invalid credentials", gr.update(visible=True), gr.update(visible=False), False, pd.DataFrame(), pd.DataFrame()
|
| 27 |
|
|
|
|
| 28 |
def bulk_create_or_update(bulk_df):
|
| 29 |
global df
|
| 30 |
bulk_df = pd.DataFrame(bulk_df).dropna(how='all')
|
| 31 |
if bulk_df.empty:
|
| 32 |
+
return get_active_data_with_select()
|
| 33 |
|
| 34 |
bulk_df = to_upper(bulk_df)
|
|
|
|
| 35 |
required_cols = ["USERNAME", "COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL", "REMARK"]
|
| 36 |
bulk_df = bulk_df[required_cols].dropna()
|
|
|
|
|
|
|
| 37 |
pk_cols = required_cols
|
| 38 |
bulk_df = bulk_df.drop_duplicates(subset=pk_cols)
|
| 39 |
+
|
|
|
|
| 40 |
temp_df = pd.concat([df, bulk_df], ignore_index=True)
|
| 41 |
df = temp_df.drop_duplicates(subset=pk_cols, keep=False)
|
| 42 |
df = pd.concat([df, bulk_df], ignore_index=True)
|
| 43 |
+
return get_active_data_with_select()
|
| 44 |
|
| 45 |
+
def delete_selected_rows(table_with_select):
|
|
|
|
|
|
|
|
|
|
| 46 |
global df
|
| 47 |
+
df_view = pd.DataFrame(table_with_select)
|
| 48 |
+
if "SELECT" not in df_view.columns:
|
| 49 |
+
return "No SELECT column found.", get_active_data_with_select()
|
| 50 |
+
|
| 51 |
+
selected_indexes = df_view[df_view["SELECT"] == True].index.tolist()
|
| 52 |
+
if not selected_indexes:
|
| 53 |
+
return "No rows selected.", get_active_data_with_select()
|
| 54 |
+
|
| 55 |
+
for idx in selected_indexes:
|
| 56 |
+
row = df_view.loc[idx]
|
| 57 |
+
mask = (
|
| 58 |
+
(df["USERNAME"] == row["USERNAME"]) &
|
| 59 |
+
(df["COUNTRYCODE"] == row["COUNTRYCODE"]) &
|
| 60 |
+
(df["SBUCODE"] == row["SBUCODE"]) &
|
| 61 |
+
(df["BRANDCODE"] == row["BRANDCODE"]) &
|
| 62 |
+
(df["CONCEPTCODE"] == row["CONCEPTCODE"]) &
|
| 63 |
+
(df["CHANNEL"] == row["CHANNEL"]) &
|
| 64 |
+
(df["REMARK"] != "DELETE")
|
| 65 |
+
)
|
| 66 |
+
df.loc[mask, "REMARK"] = "DELETE"
|
| 67 |
+
|
| 68 |
+
return "Marked selected as DELETE.", get_active_data_with_select()
|
| 69 |
+
|
| 70 |
def add_user(new_username, new_password):
|
| 71 |
global users_df
|
| 72 |
new_username = new_username.strip().upper()
|
|
|
|
| 86 |
users_df = users_df[users_df["USERNAME"] != del_username].reset_index(drop=True)
|
| 87 |
return f"User {del_username} dihapus.", show_users()
|
| 88 |
|
|
|
|
| 89 |
with gr.Blocks() as app:
|
| 90 |
logged_in = gr.State(False)
|
| 91 |
|
|
|
|
| 108 |
)
|
| 109 |
bulk_btn = gr.Button("Submit Data")
|
| 110 |
|
| 111 |
+
result_table = gr.Dataframe(label="Data with Select", interactive=True)
|
| 112 |
delete_btn = gr.Button("Delete Selected Rows")
|
| 113 |
delete_status = gr.Textbox(visible=True, interactive=False, show_label=False)
|
| 114 |
|
|
|
|
| 126 |
user_status = gr.Textbox(visible=True, interactive=False, show_label=False)
|
| 127 |
user_table = gr.Dataframe(label="User List", interactive=False)
|
| 128 |
|
|
|
|
| 129 |
login_btn.click(fn=login, inputs=[login_user, login_pass],
|
| 130 |
outputs=[login_error, login_section, main_section, logged_in, result_table, user_table])
|
|
|
|
| 131 |
bulk_btn.click(fn=bulk_create_or_update, inputs=bulk_df, outputs=result_table)
|
| 132 |
delete_btn.click(fn=delete_selected_rows, inputs=result_table, outputs=[delete_status, result_table])
|
|
|
|
| 133 |
add_user_btn.click(fn=add_user, inputs=[new_username, new_password], outputs=[user_status, user_table])
|
| 134 |
del_user_btn.click(fn=delete_user, inputs=del_user, outputs=[user_status, user_table])
|
| 135 |
|