Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,76 @@
|
|
| 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 |
-
users_df = pd.DataFrame([{"USERNAME": "ADMIN", "PASSWORD": "ADMIN"}]) #
|
| 7 |
|
| 8 |
# === Utility Functions ===
|
| 9 |
def to_upper(df):
|
| 10 |
return df.applymap(lambda x: str(x).upper() if pd.notnull(x) else x)
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
display_df = df.copy()
|
| 14 |
display_df.index.name = 'Index'
|
| 15 |
return display_df.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()
|
| 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()
|
| 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 |
-
|
|
|
|
| 40 |
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
bulk_df =
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
row[["COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL"]].values
|
| 54 |
|
| 55 |
-
df_new = pd.DataFrame(bulk_new)
|
| 56 |
df = pd.concat([df, df_new], ignore_index=True)
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
# === Delete Entry ===
|
| 61 |
def delete_entry(index):
|
| 62 |
global df
|
| 63 |
try:
|
| 64 |
idx = int(index)
|
| 65 |
if 0 <= idx < len(df):
|
| 66 |
-
df =
|
| 67 |
-
return "
|
| 68 |
else:
|
| 69 |
-
return "Index
|
| 70 |
except:
|
| 71 |
-
return "
|
| 72 |
|
| 73 |
-
# === User Management ===
|
| 74 |
def add_user(new_username, new_password):
|
| 75 |
global users_df
|
| 76 |
new_username = new_username.strip().upper()
|
|
@@ -95,6 +95,7 @@ def delete_user(del_username):
|
|
| 95 |
# === UI ===
|
| 96 |
with gr.Blocks() as app:
|
| 97 |
logged_in = gr.State(False)
|
|
|
|
| 98 |
|
| 99 |
with gr.Column(visible=True) as login_section:
|
| 100 |
gr.Markdown("## Login")
|
|
@@ -108,7 +109,7 @@ with gr.Blocks() as app:
|
|
| 108 |
|
| 109 |
with gr.Tab("Bulk Create/Update"):
|
| 110 |
bulk_df = gr.Dataframe(
|
| 111 |
-
headers=["
|
| 112 |
row_count=5,
|
| 113 |
col_count=(6, "fixed"),
|
| 114 |
label="Paste or type data"
|
|
@@ -116,7 +117,6 @@ with gr.Blocks() as app:
|
|
| 116 |
bulk_btn = gr.Button("Submit Bulk Data")
|
| 117 |
bulk_table = gr.Dataframe(label="Resulting Data", interactive=False)
|
| 118 |
|
| 119 |
-
|
| 120 |
with gr.Tab("List Data"):
|
| 121 |
view_btn = gr.Button("Refresh Table")
|
| 122 |
view_table = gr.Dataframe(label="All Data", interactive=False)
|
|
@@ -135,12 +135,15 @@ with gr.Blocks() as app:
|
|
| 135 |
user_status = gr.Textbox(visible=True, interactive=False, show_label=False)
|
| 136 |
user_table = gr.Dataframe(label="User List", interactive=False)
|
| 137 |
|
|
|
|
| 138 |
login_btn.click(fn=login, inputs=[login_user, login_pass],
|
| 139 |
-
outputs=[login_error, login_section, main_section, logged_in, view_table])
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
| 142 |
del_btn.click(fn=delete_entry, inputs=del_index, outputs=[del_status, del_table])
|
| 143 |
-
view_btn.click(fn=show_data, outputs=view_table)
|
| 144 |
|
| 145 |
add_user_btn.click(fn=add_user, inputs=[new_username, new_password], outputs=[user_status, user_table])
|
| 146 |
del_user_btn.click(fn=delete_user, inputs=del_user, outputs=[user_status, user_table])
|
|
|
|
| 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"}]) # Default user
|
| 7 |
|
| 8 |
# === Utility Functions ===
|
| 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 |
+
display_df = df[df["REMARK"] != "DELETE"].copy()
|
| 14 |
display_df.index.name = 'Index'
|
| 15 |
return display_df.reset_index()
|
| 16 |
|
| 17 |
def show_users():
|
| 18 |
return users_df.copy()
|
| 19 |
|
| 20 |
+
# === Login Function ===
|
| 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, get_active_data(), show_users()
|
| 27 |
else:
|
| 28 |
+
return "Invalid credentials", gr.update(visible=True), gr.update(visible=False), False, pd.DataFrame(), pd.DataFrame()
|
| 29 |
|
| 30 |
+
# === Bulk Create/Update ===
|
| 31 |
+
def bulk_create_or_update(bulk_df, session_user):
|
| 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 |
+
# Tambahkan kolom USERNAME dari session
|
| 44 |
+
bulk_df["USERNAME"] = session_user
|
| 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 Function ===
|
|
|
|
|
|
|
| 61 |
def delete_entry(index):
|
| 62 |
global df
|
| 63 |
try:
|
| 64 |
idx = int(index)
|
| 65 |
if 0 <= idx < len(df):
|
| 66 |
+
df.at[idx, "REMARK"] = "DELETE"
|
| 67 |
+
return "Tandai DELETE", get_active_data()
|
| 68 |
else:
|
| 69 |
+
return "Index tidak valid.", get_active_data()
|
| 70 |
except:
|
| 71 |
+
return "Index tidak valid.", get_active_data()
|
| 72 |
|
| 73 |
+
# === User Management Functions ===
|
| 74 |
def add_user(new_username, new_password):
|
| 75 |
global users_df
|
| 76 |
new_username = new_username.strip().upper()
|
|
|
|
| 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")
|
|
|
|
| 109 |
|
| 110 |
with gr.Tab("Bulk Create/Update"):
|
| 111 |
bulk_df = gr.Dataframe(
|
| 112 |
+
headers=["COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL", "REMARK"],
|
| 113 |
row_count=5,
|
| 114 |
col_count=(6, "fixed"),
|
| 115 |
label="Paste or type data"
|
|
|
|
| 117 |
bulk_btn = gr.Button("Submit Bulk Data")
|
| 118 |
bulk_table = gr.Dataframe(label="Resulting Data", interactive=False)
|
| 119 |
|
|
|
|
| 120 |
with gr.Tab("List Data"):
|
| 121 |
view_btn = gr.Button("Refresh Table")
|
| 122 |
view_table = gr.Dataframe(label="All Data", interactive=False)
|
|
|
|
| 135 |
user_status = gr.Textbox(visible=True, interactive=False, show_label=False)
|
| 136 |
user_table = gr.Dataframe(label="User List", interactive=False)
|
| 137 |
|
| 138 |
+
# === Event Bindings ===
|
| 139 |
login_btn.click(fn=login, inputs=[login_user, login_pass],
|
| 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 |
+
bulk_btn.click(fn=bulk_create_or_update, inputs=[bulk_df, session_user], outputs=[bulk_table])
|
| 144 |
+
view_btn.click(fn=get_active_data, outputs=view_table)
|
| 145 |
+
|
| 146 |
del_btn.click(fn=delete_entry, inputs=del_index, outputs=[del_status, del_table])
|
|
|
|
| 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])
|