Bayhaqy commited on
Commit
5573f9e
·
verified ·
1 Parent(s): 32647ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -48
app.py CHANGED
@@ -1,10 +1,12 @@
1
  import gradio as gr
 
2
 
3
- # Data dummy awal (list of dicts)
4
- data = []
5
 
6
- def create_entry(username, countrycode, sbucode, brandcode, conceptcode, channel):
7
- new_entry = {
 
8
  "USERNAME": username,
9
  "COUNTRYCODE": countrycode,
10
  "SBUCODE": sbucode,
@@ -12,52 +14,92 @@ def create_entry(username, countrycode, sbucode, brandcode, conceptcode, channel
12
  "CONCEPTCODE": conceptcode,
13
  "CHANNEL": channel
14
  }
15
- data.append(new_entry)
16
- return "Entry added!", show_data()
17
 
18
- def show_data():
19
- return "\n".join([f"{i+1}. {entry}" for i, entry in enumerate(data)]) or "No data yet."
 
 
20
 
21
  def delete_entry(index):
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  try:
23
- index = int(index) - 1
24
- if 0 <= index < len(data):
25
- deleted = data.pop(index)
26
- return f"Deleted: {deleted}", show_data()
 
 
 
 
 
27
  else:
28
- return "Invalid index", show_data()
29
- except:
30
- return "Enter a valid number", show_data()
31
-
32
- with gr.Blocks() as demo:
33
- gr.Markdown("## Simple CRUD App")
34
-
35
- with gr.Row():
36
- username = gr.Textbox(label="USERNAME")
37
- country = gr.Textbox(label="COUNTRYCODE")
38
- sbu = gr.Textbox(label="SBUCODE")
39
- brand = gr.Textbox(label="BRANDCODE")
40
- concept = gr.Textbox(label="CONCEPTCODE")
41
- channel = gr.Textbox(label="CHANNEL")
42
-
43
- create_btn = gr.Button("Add Entry")
44
- output_text = gr.Textbox(label="Current Data", lines=10)
45
-
46
- create_btn.click(
47
- create_entry,
48
- inputs=[username, country, sbu, brand, concept, channel],
49
- outputs=[gr.Textbox(visible=False), output_text]
50
- )
51
-
52
- with gr.Row():
53
- del_index = gr.Textbox(label="Delete Entry No")
54
- delete_btn = gr.Button("Delete")
55
-
56
- delete_btn.click(delete_entry, inputs=del_index, outputs=[gr.Textbox(visible=False), output_text])
57
-
58
- gr.Markdown("### Current Records:")
59
- show_btn = gr.Button("Refresh Data")
60
- show_btn.click(fn=lambda: "", outputs=gr.Textbox(visible=False)) # Dummy refresh trigger
61
- show_btn.click(fn=show_data, outputs=output_text)
62
-
63
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import pandas as pd
3
 
4
+ # In-memory storage
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 = {
10
  "USERNAME": username,
11
  "COUNTRYCODE": countrycode,
12
  "SBUCODE": sbucode,
 
14
  "CONCEPTCODE": conceptcode,
15
  "CHANNEL": channel
16
  }
17
+ df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
18
+ return df
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 df
24
 
25
  def delete_entry(index):
26
+ global df
27
+ try:
28
+ idx = int(index)
29
+ if 0 <= idx < len(df):
30
+ df = df.drop(idx).reset_index(drop=True)
31
+ return df
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
39
  try:
40
+ idx = int(index)
41
+ if 0 <= idx < len(df):
42
+ df.at[idx, "USERNAME"] = username
43
+ df.at[idx, "COUNTRYCODE"] = countrycode
44
+ df.at[idx, "SBUCODE"] = sbucode
45
+ df.at[idx, "BRANDCODE"] = brandcode
46
+ df.at[idx, "CONCEPTCODE"] = conceptcode
47
+ df.at[idx, "CHANNEL"] = channel
48
+ return df
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
+ return df
56
+
57
+ with gr.Blocks() as app:
58
+ gr.Markdown("## CRUD App with Excel-style Bulk Input")
59
+
60
+ with gr.Tab("Add Entry"):
61
+ with gr.Row():
62
+ username = gr.Textbox(label="USERNAME")
63
+ countrycode = gr.Textbox(label="COUNTRYCODE")
64
+ sbucode = gr.Textbox(label="SBUCODE")
65
+ brandcode = gr.Textbox(label="BRANDCODE")
66
+ conceptcode = gr.Textbox(label="CONCEPTCODE")
67
+ channel = gr.Textbox(label="CHANNEL")
68
+ add_btn = gr.Button("Add")
69
+ add_btn.click(add_entry,
70
+ inputs=[username, countrycode, sbucode, brandcode, conceptcode, channel],
71
+ outputs=gr.Dataframe())
72
+
73
+ with gr.Tab("Bulk Input (Excel-like)"):
74
+ bulk_df = gr.Dataframe(
75
+ headers=["USERNAME", "COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL"],
76
+ row_count=5,
77
+ col_count=(6, "fixed"),
78
+ label="Paste or type your data here"
79
+ )
80
+ bulk_btn = gr.Button("Submit Bulk Data")
81
+ bulk_btn.click(bulk_input, inputs=bulk_df, outputs=gr.Dataframe())
82
+
83
+ with gr.Tab("Update Entry"):
84
+ upd_index = gr.Textbox(label="Index to Update")
85
+ upd_username = gr.Textbox(label="USERNAME")
86
+ upd_country = gr.Textbox(label="COUNTRYCODE")
87
+ upd_sbu = gr.Textbox(label="SBUCODE")
88
+ upd_brand = gr.Textbox(label="BRANDCODE")
89
+ upd_concept = gr.Textbox(label="CONCEPTCODE")
90
+ upd_channel = gr.Textbox(label="CHANNEL")
91
+ update_btn = gr.Button("Update Entry")
92
+ update_btn.click(update_entry,
93
+ inputs=[upd_index, upd_username, upd_country, upd_sbu, upd_brand, upd_concept, upd_channel],
94
+ outputs=gr.Dataframe())
95
+
96
+ with gr.Tab("Delete Entry"):
97
+ del_index = gr.Textbox(label="Index to Delete")
98
+ del_btn = gr.Button("Delete")
99
+ del_btn.click(delete_entry, inputs=del_index, outputs=gr.Dataframe())
100
+
101
+ with gr.Tab("View Data"):
102
+ view_btn = gr.Button("Refresh Table")
103
+ view_btn.click(show_data, outputs=gr.Dataframe())
104
+
105
+ app.launch()