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