Bayhaqy commited on
Commit
89f3dc3
·
verified ·
1 Parent(s): 5573f9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -56
app.py CHANGED
@@ -1,9 +1,15 @@
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 = {
@@ -15,12 +21,12 @@ def add_entry(username, countrycode, sbucode, brandcode, conceptcode, channel):
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
@@ -28,11 +34,11 @@ def delete_entry(index):
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
@@ -45,61 +51,87 @@ def update_entry(index, username, countrycode, sbucode, brandcode, conceptcode,
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()
 
1
  import gradio as gr
2
  import pandas as pd
3
 
4
+ # In-memory data store
5
  df = pd.DataFrame(columns=["USERNAME", "COUNTRYCODE", "SBUCODE", "BRANDCODE", "CONCEPTCODE", "CHANNEL"])
6
 
7
+ def login(username, password):
8
+ if username == "admin" and password == "admin":
9
+ return gr.update(visible=False), gr.update(visible=True), True, show_data()
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 = {
 
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):
32
  global df
 
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
 
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()
57
  except Exception as e:
58
+ return gr.Textbox.update(value=f"Error: {e}"), show_data()
59
 
60
  def show_data():
61
+ display_df = df.copy()
62
+ display_df.index.name = 'Index'
63
+ return display_df.reset_index()
64
 
65
  with gr.Blocks() as app:
66
+ logged_in = gr.State(False)
67
+
68
+ with gr.Column(visible=True) as login_section:
69
+ gr.Markdown("## Login")
70
+ login_user = gr.Textbox(label="Username")
71
+ login_pass = gr.Textbox(label="Password", type="password")
72
+ login_btn = gr.Button("Login")
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)
114
+ del_table = gr.Dataframe(label="Data", interactive=False)
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()