Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,21 @@ 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": "
|
| 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 get_active_data():
|
| 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()
|
|
@@ -27,60 +27,52 @@ def login(username, password):
|
|
| 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 |
-
session_user = session_user.strip().upper()
|
| 34 |
bulk_df = pd.DataFrame(bulk_df).dropna(how='all')
|
| 35 |
if bulk_df.empty:
|
| 36 |
return get_active_data()
|
| 37 |
|
| 38 |
bulk_df = to_upper(bulk_df)
|
| 39 |
|
| 40 |
-
required_cols = ["COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL", "REMARK"]
|
| 41 |
bulk_df = bulk_df[required_cols].dropna()
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
# Buang data duplikat berdasarkan PK
|
| 47 |
-
pk_cols = ["USERNAME", "COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL", "REMARK"]
|
| 48 |
bulk_df = bulk_df.drop_duplicates(subset=pk_cols)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
# Buang yang sudah ada (duplikat di df)
|
| 51 |
-
df_temp = pd.concat([df, bulk_df], ignore_index=True)
|
| 52 |
-
df_temp = df_temp.drop_duplicates(subset=pk_cols, keep=False)
|
| 53 |
-
|
| 54 |
-
df_new = df_temp[df_temp["USERNAME"] == session_user]
|
| 55 |
-
df_new = df_new[pk_cols]
|
| 56 |
-
|
| 57 |
-
df = pd.concat([df, df_new], ignore_index=True)
|
| 58 |
return get_active_data()
|
| 59 |
|
| 60 |
-
# === Soft Delete
|
| 61 |
-
def
|
| 62 |
global df
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
def add_user(new_username, new_password):
|
| 75 |
global users_df
|
| 76 |
new_username = new_username.strip().upper()
|
| 77 |
new_password = new_password.strip()
|
| 78 |
if not new_username or not new_password:
|
| 79 |
return "Username/password tidak boleh kosong.", show_users()
|
| 80 |
-
|
| 81 |
if new_username in users_df["USERNAME"].values:
|
| 82 |
return "Username sudah ada.", show_users()
|
| 83 |
-
|
| 84 |
users_df = pd.concat([users_df, pd.DataFrame([{"USERNAME": new_username, "PASSWORD": new_password}])], ignore_index=True)
|
| 85 |
return f"User {new_username} ditambahkan.", show_users()
|
| 86 |
|
|
@@ -95,7 +87,6 @@ def delete_user(del_username):
|
|
| 95 |
# === UI ===
|
| 96 |
with gr.Blocks() as app:
|
| 97 |
logged_in = gr.State(False)
|
| 98 |
-
session_user = gr.State("")
|
| 99 |
|
| 100 |
with gr.Column(visible=True) as login_section:
|
| 101 |
gr.Markdown("## Login")
|
|
@@ -105,45 +96,41 @@ with gr.Blocks() as app:
|
|
| 105 |
login_error = gr.Textbox(visible=False, interactive=False, show_label=False)
|
| 106 |
|
| 107 |
with gr.Column(visible=False) as main_section:
|
| 108 |
-
gr.Markdown("## Input
|
| 109 |
|
| 110 |
-
with gr.
|
| 111 |
bulk_df = gr.Dataframe(
|
| 112 |
-
headers=["COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL", "REMARK"],
|
| 113 |
row_count=5,
|
| 114 |
-
col_count=(
|
| 115 |
-
label="
|
| 116 |
)
|
| 117 |
-
bulk_btn = gr.Button("Submit
|
| 118 |
-
bulk_table = gr.Dataframe(label="Resulting Data", interactive=False)
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
|
| 124 |
-
|
| 125 |
-
del_btn = gr.Button("Delete")
|
| 126 |
-
del_status = gr.Textbox(visible=True, interactive=False, show_label=False)
|
| 127 |
-
del_table = gr.Dataframe(label="Data", interactive=False)
|
| 128 |
|
| 129 |
-
|
|
|
|
| 130 |
new_username = gr.Textbox(label="New Username")
|
| 131 |
new_password = gr.Textbox(label="New Password")
|
| 132 |
add_user_btn = gr.Button("Add User")
|
|
|
|
| 133 |
del_user = gr.Textbox(label="Username to Delete")
|
| 134 |
del_user_btn = gr.Button("Delete User")
|
| 135 |
-
user_status = gr.Textbox(visible=True, interactive=False, show_label=False)
|
| 136 |
-
user_table = gr.Dataframe(label="User List", interactive=False)
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
outputs=[login_error, login_section, main_section, logged_in, view_table, user_table])\
|
| 141 |
-
.then(lambda u: u.strip().upper(), inputs=login_user, outputs=session_user)
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
|
|
|
| 145 |
|
| 146 |
-
|
|
|
|
| 147 |
|
| 148 |
add_user_btn.click(fn=add_user, inputs=[new_username, new_password], outputs=[user_status, user_table])
|
| 149 |
del_user_btn.click(fn=delete_user, inputs=del_user, outputs=[user_status, user_table])
|
|
|
|
| 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"}]) # Default login user
|
| 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 get_active_data():
|
| 13 |
+
data = df[df["REMARK"] != "DELETE"].copy()
|
| 14 |
+
data.index.name = "Index"
|
| 15 |
+
return data.reset_index()
|
| 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()
|
|
|
|
| 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 get_active_data()
|
| 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 |
return get_active_data()
|
| 52 |
|
| 53 |
+
# === Soft Delete using selected index ===
|
| 54 |
+
def delete_selected_rows(selected_rows):
|
| 55 |
global df
|
| 56 |
+
if not selected_rows:
|
| 57 |
+
return "No rows selected.", get_active_data()
|
| 58 |
+
|
| 59 |
+
for idx in selected_rows:
|
| 60 |
+
try:
|
| 61 |
+
original_index = int(idx)
|
| 62 |
+
df.at[original_index, "REMARK"] = "DELETE"
|
| 63 |
+
except:
|
| 64 |
+
continue
|
| 65 |
+
return "Rows marked as DELETE.", get_active_data()
|
| 66 |
+
|
| 67 |
+
# === User Management ===
|
| 68 |
def add_user(new_username, new_password):
|
| 69 |
global users_df
|
| 70 |
new_username = new_username.strip().upper()
|
| 71 |
new_password = new_password.strip()
|
| 72 |
if not new_username or not new_password:
|
| 73 |
return "Username/password tidak boleh kosong.", show_users()
|
|
|
|
| 74 |
if new_username in users_df["USERNAME"].values:
|
| 75 |
return "Username sudah ada.", show_users()
|
|
|
|
| 76 |
users_df = pd.concat([users_df, pd.DataFrame([{"USERNAME": new_username, "PASSWORD": new_password}])], ignore_index=True)
|
| 77 |
return f"User {new_username} ditambahkan.", show_users()
|
| 78 |
|
|
|
|
| 87 |
# === UI ===
|
| 88 |
with gr.Blocks() as app:
|
| 89 |
logged_in = gr.State(False)
|
|
|
|
| 90 |
|
| 91 |
with gr.Column(visible=True) as login_section:
|
| 92 |
gr.Markdown("## Login")
|
|
|
|
| 96 |
login_error = gr.Textbox(visible=False, interactive=False, show_label=False)
|
| 97 |
|
| 98 |
with gr.Column(visible=False) as main_section:
|
| 99 |
+
gr.Markdown("## Input & View Data")
|
| 100 |
|
| 101 |
+
with gr.Row():
|
| 102 |
bulk_df = gr.Dataframe(
|
| 103 |
+
headers=["USERNAME", "COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL", "REMARK"],
|
| 104 |
row_count=5,
|
| 105 |
+
col_count=(7, "fixed"),
|
| 106 |
+
label="Input Data"
|
| 107 |
)
|
| 108 |
+
bulk_btn = gr.Button("Submit Data")
|
|
|
|
| 109 |
|
| 110 |
+
result_table = gr.Dataframe(label="Active Data", interactive=False, row_selectable="multi")
|
| 111 |
+
delete_btn = gr.Button("Delete Selected Rows")
|
| 112 |
+
delete_status = gr.Textbox(visible=True, interactive=False, show_label=False)
|
| 113 |
|
| 114 |
+
gr.Markdown("---")
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
gr.Markdown("## User Management (Login Access Only)")
|
| 117 |
+
with gr.Row():
|
| 118 |
new_username = gr.Textbox(label="New Username")
|
| 119 |
new_password = gr.Textbox(label="New Password")
|
| 120 |
add_user_btn = gr.Button("Add User")
|
| 121 |
+
with gr.Row():
|
| 122 |
del_user = gr.Textbox(label="Username to Delete")
|
| 123 |
del_user_btn = gr.Button("Delete User")
|
|
|
|
|
|
|
| 124 |
|
| 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])
|