import pandas as pd import gradio as gr ADMIN_USERNAME = "admin" ADMIN_PASSWORD = "adminpassword" sheets = pd.DataFrame() # 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 type(sheets) == dict: data = sheets["employee_authentication"] usernames = data['ชื่อ-สกุลพนักงาน'].unique() # Display usernames in an HTML table for easy copying html_content = "

Available Usernames:

" return html_content else: return "No data available. Please wait for the admin to upload data." def authenticate(input_username, input_password): 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 true_password == input_password # Simplified for this example def show_employee_data(username, password): 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 available for this user.") else: raise gr.Error("No data uploaded by admin.") else: gr.Warning("Invalid username or password.") # 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") # File upload section (initially hidden) with gr.Group(visible=False) as upload_group: gr.Markdown("### Upload Data") upload_inputs = [gr.File(label="Upload Excel File")] upload_button = gr.Button("Upload") upload_button.click(update_data, inputs=upload_inputs) # 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 upload button to the file upload function upload_button.click( fn=update_data, inputs=upload_inputs, ) with gr.Tab("Employee Interface"): with gr.Tab("View Data"): show_user_button = gr.Button("Show Usernames") view_username = gr.Markdown(label="Avilable username") view_inputs = [gr.Textbox(label="Username"), gr.Textbox(label="Password")] view_button = gr.Button("View") 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) demo.launch()