Kamonphanpro commited on
Commit
3d7532f
·
1 Parent(s): 2c68063

feature: add clear and delte data buttons

Browse files
Files changed (1) hide show
  1. app.py +107 -53
app.py CHANGED
@@ -4,7 +4,14 @@ import gradio as gr
4
  ADMIN_USERNAME = "admin"
5
  ADMIN_PASSWORD = "adminpassword"
6
 
7
- sheets = pd.DataFrame()
 
 
 
 
 
 
 
8
  # Function to handle file upload by the admin
9
  def update_data(excel_file):
10
  global sheets
@@ -15,66 +22,90 @@ def update_data(excel_file):
15
  )
16
  else:
17
  gr.Info(f"Data updated successfully")
18
-
 
19
  # Function to display a list of usernames
20
- def list_usernames():
21
- if (type(sheets) == dict and "employee_authentication" in sheets.keys()):
22
- data = sheets["employee_authentication"]
23
- usernames = data['ชื่อ-สกุลพนักงาน'].unique()
24
- # Display usernames in an HTML table for easy copying
25
- html_content = "<h4>Available Usernames:</h4><ul>"
26
- for username in usernames:
27
- html_content += f"<li>{username}</li>"
28
- html_content += "</ul>"
29
- return html_content
 
 
 
30
  else:
31
  return "No data available. Please wait for the admin to upload data."
32
-
 
33
  def authenticate(input_username, input_password):
34
- auth_data = sheets["employee_authentication"]
35
- if input_username not in set(auth_data["ชื่อ-สกุลพนักงาน"]):
36
- raise gr.Warning(
37
- "Can not find the specified employee name in the authenticate data"
38
- )
39
- true_password = auth_data.loc[
40
- auth_data["ชื่อ-สกุลพนักงาน"] == input_username, "password"
41
- ].values[0]
42
- return true_password == input_password # Simplified for this example
 
43
 
44
 
45
  def show_employee_data(username, password):
46
- data = sheets["timesheet_all"]
47
- if authenticate(username, password):
48
- if not data.empty:
49
- # Filter data for the logged-in user
50
- employee_data = data[data["ชื่อ-สกุลพนักงาน"] == username]
51
- if not employee_data.empty:
52
- return employee_data
 
 
 
53
  else:
54
- raise gr.Error("No data available for this user.")
55
  else:
56
- raise gr.Error("No data uploaded by admin.")
 
 
 
 
 
 
 
 
 
 
 
57
  else:
58
- gr.Warning("Invalid username or password.")
59
-
 
 
60
  # Create the Gradio interface
61
  with gr.Blocks() as demo:
62
-
63
  # create admin tab
64
  with gr.Tab("Admin Interface"):
65
  with gr.Group():
66
  gr.Markdown("### Admin Login")
67
  login_inputs = [gr.Textbox(label="Username"), gr.Textbox(label="Password")]
68
  login_button = gr.Button("Log in")
69
-
 
70
  # File upload section (initially hidden)
71
  with gr.Group(visible=False) as upload_group:
72
- gr.Markdown("### Upload Data")
73
- upload_inputs = [gr.File(label="Upload Excel File")]
 
 
74
  upload_button = gr.Button("Upload")
75
-
76
- upload_button.click(update_data, inputs=upload_inputs)
77
-
78
  # Function for Admin login
79
  def admin_login(username, password):
80
  global admin_authenticated
@@ -87,30 +118,53 @@ with gr.Blocks() as demo:
87
  else:
88
  admin_authenticated = False
89
  raise gr.Error("Invalid username or password.")
90
-
91
  # Link the login button to the validation function
92
  login_button.click(
93
- fn=admin_login,
94
- inputs=login_inputs,
95
- outputs=[upload_group]
 
 
 
 
 
96
  )
97
-
98
- # Link the upload button to the file upload function
99
  upload_button.click(
100
  fn=update_data,
101
  inputs=upload_inputs,
102
  )
103
 
 
 
 
 
 
104
  with gr.Tab("Employee Interface"):
105
  with gr.Tab("View Data"):
106
- show_user_button = gr.Button("Show Usernames")
 
 
 
107
  view_username = gr.Markdown(label="Avilable username")
108
-
109
- view_inputs = [gr.Textbox(label="Username"), gr.Textbox(label="Password")]
 
 
110
  view_button = gr.Button("View")
 
111
  view_outputs = gr.DataFrame(label="View data")
112
-
113
  show_user_button.click(list_usernames, outputs=view_username)
114
- view_button.click(show_employee_data, inputs=view_inputs, outputs=view_outputs)
115
-
116
- demo.launch(share=True)
 
 
 
 
 
 
 
 
4
  ADMIN_USERNAME = "admin"
5
  ADMIN_PASSWORD = "adminpassword"
6
 
7
+
8
+ def is_data_exist():
9
+ if "sheets" in globals():
10
+ return True
11
+ else:
12
+ return False
13
+
14
+
15
  # Function to handle file upload by the admin
16
  def update_data(excel_file):
17
  global sheets
 
22
  )
23
  else:
24
  gr.Info(f"Data updated successfully")
25
+
26
+
27
  # Function to display a list of usernames
28
+ def list_usernames():
29
+ if is_data_exist():
30
+ if isinstance(sheets, dict) and "employee_authentication" in sheets:
31
+ data = sheets["employee_authentication"]
32
+ usernames = data["ชื่อ-สกุลพนักงาน"].unique()
33
+ # Display usernames in an HTML table for easy copying
34
+ html_content = "<h4>Available Usernames:</h4><ul>"
35
+ for username in usernames:
36
+ html_content += f"<li>{username}</li>"
37
+ html_content += "</ul>"
38
+ return html_content
39
+ else:
40
+ return "No data available. Please wait for the admin to upload data."
41
  else:
42
  return "No data available. Please wait for the admin to upload data."
43
+
44
+
45
  def authenticate(input_username, input_password):
46
+ if is_data_exist():
47
+ auth_data = sheets["employee_authentication"]
48
+ if input_username not in set(auth_data["ชื่อ-สกุลพนักงาน"]):
49
+ raise gr.Warning(
50
+ "Can not find the specified employee name in the authenticate data"
51
+ )
52
+ true_password = auth_data.loc[
53
+ auth_data["ชื่อ-สกุลพนักงาน"] == input_username, "password"
54
+ ].values[0]
55
+ return true_password == input_password # Simplified for this example
56
 
57
 
58
  def show_employee_data(username, password):
59
+ if is_data_exist():
60
+ data = sheets["timesheet_all"]
61
+ if authenticate(username, password):
62
+ if not data.empty:
63
+ # Filter data for the logged-in user
64
+ employee_data = data[data["ชื่อ-สกุลพนักงาน"] == username]
65
+ if not employee_data.empty:
66
+ return employee_data
67
+ else:
68
+ raise gr.Error("No data available for this user.")
69
  else:
70
+ raise gr.Error("No data uploaded by admin.")
71
  else:
72
+ gr.Warning("Invalid username or password.")
73
+
74
+
75
+ # Function to clear input fields (resets them to empty)
76
+ def clear_fields():
77
+ return "", "" # Returns empty strings to reset username and password fields
78
+
79
+
80
+ def clear_data():
81
+ if "sheets" in globals():
82
+ del globals()["sheets"]
83
+ gr.Info("Now the data is cleared")
84
  else:
85
+ gr.Warning("No data to clear")
86
+
87
+
88
+ ########3 building app ##############
89
  # Create the Gradio interface
90
  with gr.Blocks() as demo:
91
+
92
  # create admin tab
93
  with gr.Tab("Admin Interface"):
94
  with gr.Group():
95
  gr.Markdown("### Admin Login")
96
  login_inputs = [gr.Textbox(label="Username"), gr.Textbox(label="Password")]
97
  login_button = gr.Button("Log in")
98
+ clear_button = gr.Button("Clear")
99
+
100
  # File upload section (initially hidden)
101
  with gr.Group(visible=False) as upload_group:
102
+ gr.Markdown("### Upload Data (an Excel file)")
103
+ upload_inputs = [
104
+ gr.File(label="Upload Excel File", file_types=[".xls", ".xlsx"])
105
+ ]
106
  upload_button = gr.Button("Upload")
107
+ clear_upload_button = gr.Button("Clear Uploaded Data")
108
+
 
109
  # Function for Admin login
110
  def admin_login(username, password):
111
  global admin_authenticated
 
118
  else:
119
  admin_authenticated = False
120
  raise gr.Error("Invalid username or password.")
121
+
122
  # Link the login button to the validation function
123
  login_button.click(
124
+ fn=admin_login, inputs=login_inputs, outputs=[upload_group]
125
+ )
126
+
127
+ # Link the clear button to the clear_fields function to reset the inputs
128
+ clear_button.click(
129
+ fn=clear_fields,
130
+ inputs=[], # No inputs are needed
131
+ outputs=login_inputs, # Clear the username and password fields
132
  )
133
+
134
+ # Link the upload button to the file upload function
135
  upload_button.click(
136
  fn=update_data,
137
  inputs=upload_inputs,
138
  )
139
 
140
+ # Link the clear data button
141
+ clear_upload_button.click(
142
+ fn=clear_data,
143
+ )
144
+
145
  with gr.Tab("Employee Interface"):
146
  with gr.Tab("View Data"):
147
+ gr.Markdown(
148
+ "### Is there any question? Contect Nitchakarn Chuangsuwanich (Prim)"
149
+ )
150
+ show_user_button = gr.Button("show username")
151
  view_username = gr.Markdown(label="Avilable username")
152
+ view_inputs = [
153
+ gr.Dropdown(label="Username", choices=[]),
154
+ gr.Textbox(label="Password"),
155
+ ]
156
  view_button = gr.Button("View")
157
+ clear_button = gr.Button("Clear")
158
  view_outputs = gr.DataFrame(label="View data")
159
+
160
  show_user_button.click(list_usernames, outputs=view_username)
161
+ view_button.click(
162
+ show_employee_data, inputs=view_inputs, outputs=view_outputs
163
+ )
164
+ clear_button.click(
165
+ fn=clear_fields,
166
+ inputs=[], # No inputs are needed
167
+ outputs=view_inputs, # Clear the username and password fields
168
+ )
169
+
170
+ demo.launch(share=True, debug=False)