temp / app.py
Kamonphanpro's picture
fix: show only username
d88bfa0
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)