File size: 4,658 Bytes
01f8eb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 = "<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."
    
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()