Bayhaqy commited on
Commit
693d05a
·
verified ·
1 Parent(s): 89f3dc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -68
app.py CHANGED
@@ -10,22 +10,41 @@ def login(username, password):
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 = {
16
- "USERNAME": username,
17
- "COUNTRYCODE": countrycode,
18
- "SBUCODE": sbucode,
19
- "BRANDCODE": brandcode,
20
- "CONCEPTCODE": conceptcode,
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):
@@ -34,23 +53,23 @@ def delete_entry(index):
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
45
  try:
46
  idx = int(index)
47
  if 0 <= idx < len(df):
48
- df.at[idx, "USERNAME"] = username
49
- df.at[idx, "COUNTRYCODE"] = countrycode
50
- df.at[idx, "SBUCODE"] = sbucode
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()
@@ -73,41 +92,19 @@ with gr.Blocks() as app:
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)
@@ -115,23 +112,13 @@ with gr.Blocks() as app:
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()
 
10
  else:
11
  return gr.update(visible=True, value="Invalid credentials"), gr.update(visible=False), False, pd.DataFrame()
12
 
13
+ def to_upper(df):
14
+ return df.applymap(lambda x: str(x).upper() if pd.notnull(x) else x)
 
 
 
 
 
 
 
 
 
 
15
 
16
+ def bulk_create_or_update(bulk_df):
17
  global df
18
+ bulk_df = pd.DataFrame(bulk_df).dropna(how='all')
19
+ if bulk_df.empty:
20
+ return show_data()
21
+
22
+ bulk_df = to_upper(bulk_df)
23
+
24
+ # Hanya ambil kolom penting
25
+ bulk_df = bulk_df[["USERNAME", "COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL"]]
26
+
27
+ # Buat index gabungan
28
+ merge_keys = ["USERNAME", "COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL"]
29
+
30
+ # 1. Remove exact duplicates (same row exists in both)
31
+ df_merge = pd.merge(bulk_df, df, on=merge_keys, how='inner')
32
+ bulk_df = pd.concat([bulk_df, df_merge]).drop_duplicates(keep=False)
33
+
34
+ # 2. Pisahkan insert dan update
35
+ existing_usernames = df["USERNAME"].unique()
36
+ bulk_existing = bulk_df[bulk_df["USERNAME"].isin(existing_usernames)]
37
+ bulk_new = bulk_df[~bulk_df["USERNAME"].isin(existing_usernames)]
38
+
39
+ # Update: overwrite rows with same USERNAME
40
+ for _, row in bulk_existing.iterrows():
41
+ df.loc[df["USERNAME"] == row["USERNAME"], ["COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL"]] = \
42
+ row[["COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL"]].values
43
+
44
+ # Insert: append rows
45
+ df_new = pd.DataFrame(bulk_new)
46
+ df = pd.concat([df, df_new], ignore_index=True)
47
+
48
  return show_data()
49
 
50
  def delete_entry(index):
 
53
  idx = int(index)
54
  if 0 <= idx < len(df):
55
  df = df.drop(idx).reset_index(drop=True)
56
+ return gr.Textbox.update(value="Deleted."), show_data()
57
  else:
58
+ return gr.Textbox.update(value="Index out of range."), show_data()
59
+ except:
60
+ return gr.Textbox.update(value="Invalid index."), show_data()
61
 
62
  def update_entry(index, username, countrycode, sbucode, brandcode, conceptcode, channel):
63
  global df
64
  try:
65
  idx = int(index)
66
  if 0 <= idx < len(df):
67
+ df.at[idx, "USERNAME"] = username.upper()
68
+ df.at[idx, "COUNTRYCODE"] = countrycode.upper()
69
+ df.at[idx, "SBUCODE"] = sbucode.upper()
70
+ df.at[idx, "BRANDCODE"] = brandcode.upper()
71
+ df.at[idx, "CONCEPTCODE"] = conceptcode.upper()
72
+ df.at[idx, "CHANNEL"] = channel.upper()
73
  return show_data()
74
  else:
75
  return gr.Textbox.update(value=f"Index {index} out of range"), show_data()
 
92
  login_error = gr.Textbox(visible=False, interactive=False, show_label=False)
93
 
94
  with gr.Column(visible=False) as main_section:
95
+ gr.Markdown("## Bulk CRUD App (Auto Uppercase + Smart Merge)")
96
+
97
+ with gr.Tab("Bulk Create/Update"):
 
 
 
 
 
 
 
 
 
 
 
98
  bulk_df = gr.Dataframe(
99
  headers=["USERNAME", "COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL"],
100
  row_count=5,
101
  col_count=(6, "fixed"),
102
+ label="Paste or type data (seperti Excel)"
103
  )
104
  bulk_btn = gr.Button("Submit Bulk Data")
105
+ bulk_table = gr.Dataframe(label="Resulting Data", interactive=False)
106
+
107
+ with gr.Tab("Manual Delete"):
 
 
 
 
 
 
 
 
 
 
 
108
  del_index = gr.Textbox(label="Index to Delete")
109
  del_btn = gr.Button("Delete")
110
  del_status = gr.Textbox(visible=True, interactive=False, show_label=False)
 
112
 
113
  with gr.Tab("View Data"):
114
  view_btn = gr.Button("Refresh Table")
115
+ view_table = gr.Dataframe(label="All Data", interactive=False)
116
 
117
+ login_btn.click(fn=login, inputs=[login_user, login_pass],
118
  outputs=[login_error, main_section, logged_in, view_table])
119
 
120
+ bulk_btn.click(fn=bulk_create_or_update, inputs=bulk_df, outputs=bulk_table)
121
+ del_btn.click(fn=delete_entry, inputs=del_index, outputs=[del_status, del_table])
122
+ view_btn.click(fn=show_data, outputs=view_table)
 
 
 
 
 
 
 
 
 
 
123
 
124
  app.launch()