/
fingerprint_app_streamlit.py
102 lines (80 loc) · 3.23 KB
/
fingerprint_app_streamlit.py
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
import streamlit as st
import pandas as pd
import xlrd
import base64
from io import BytesIO
st.write("""
# Fingerprint absent transpose app
This app transpose the data from fingerprint machine for JR Raynaldi Group
""")
# turn off upload error warning
# st.set_option('deprecation.showfileUploaderEncoding', False)
# -----------------------------------------------
# create sidebar
st.sidebar.header('User Input Features')
# Collects user input features into dataframe
# upload xls file
uploaded_file = st.sidebar.file_uploader("Upload your input Excel file", type=["xls","xlsx"])
if uploaded_file is not None:
input_df = pd.read_excel(uploaded_file, engine = "openpyxl")
# select time format
time_format = st.sidebar.selectbox('Time_format:',("%d/%m/%Y %H:%M","%m/%d/%Y %H:%M","%m/%d/%Y %I:%M %p","%d/%m/%Y %H.%M"))
# create button to run function
run_funct = st.sidebar.button('Run function')
# -----------------------------------------------
# Fingerprint transpose function
def fingerprint_transpose(input_df, time_format):
df2 = input_df.copy()
df2['Waktu'] = pd.to_datetime(df2['Waktu'], format =time_format)
df2['Tanggal'] = df2['Waktu'].dt.strftime('%d/%m/%Y')
df2['Jam'] = df2['Waktu'].dt.strftime('%H:%M')
df2 = df2[['Nama','No. ID','Tanggal','Jam','Status']]
xxx = df2.sort_values(['Nama','Tanggal', 'Jam'], ascending=[True,True,True])
yyy = xxx.groupby(['Nama','Tanggal','Jam'])['Status'].count()
yy = yyy.copy()
zz = yy.reset_index()
zz = zz.drop('Status', axis = 1)
group = zz.groupby(['Nama','Tanggal'])
dfzz = group.apply(lambda x: x['Jam'].unique())
dfzz = dfzz.apply(pd.Series)
dfzz = dfzz.reset_index()
return dfzz
#global result_file_name
#result_file_name = "fingerprint_result.xlsx"
#writer = pd.ExcelWriter(result_file_name, datetime_format='hh:mm')
#dfzz.to_excel(writer, "Sheet1")
#writer.close()
#print('done')
# function to download excel.
# taken from: https://discuss.streamlit.io/t/how-to-download-file-in-streamlit/1806/12
def to_excel(df):
output = BytesIO()
writer = pd.ExcelWriter(output, engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
writer.close()
processed_data = output.getvalue()
return processed_data
def get_table_download_link(df):
"""Generates a link allowing the data in a given panda dataframe to be downloaded
in: dataframe
out: href string
"""
val = to_excel(df)
b64 = base64.b64encode(val) # val looks like b'...'
return f'<a href="data:application/octet-stream;base64,{b64.decode()}" download="extract.xlsx">Download csv file</a>' # decode b'abc' => abc
# -----------------------------------------------
# Displays the user input features
st.subheader('Input file')
if uploaded_file is not None:
st.write(input_df.head(2))
st.write('')
st.write(input_df.Waktu[0])
st.subheader('Time format yang dipilih')
if time_format is not None:
st.write(time_format)
st.subheader('Preview and Download result')
if run_funct == True:
df = fingerprint_transpose(input_df, time_format)
st.write(df.head(3))
st.markdown(get_table_download_link(df), unsafe_allow_html=True)
st.write('Selesai. Silahkan klik kanan dan save link di atas')