Spaces:
Sleeping
Sleeping
Commit
·
01f8eb5
1
Parent(s):
b29b388
initial: add app.py
Browse files
app.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import gradio as gr
|
3 |
+
|
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
|
11 |
+
sheets = pd.read_excel(excel_file, sheet_name=None) # Read the uploaded file
|
12 |
+
if ("timesheet_all" not in sheets) | ("employee_authentication" not in sheets):
|
13 |
+
raise gr.Error(
|
14 |
+
"the excel file must contain the following sheets ['timesheet_all', 'employee_authentication']"
|
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:
|
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
|
81 |
+
if username == ADMIN_USERNAME and password == ADMIN_PASSWORD:
|
82 |
+
admin_authenticated = True
|
83 |
+
gr.Info("Login successful. You can now upload data.")
|
84 |
+
return {
|
85 |
+
upload_group: gr.Group(visible=True),
|
86 |
+
}
|
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()
|