raspberry-pi-absensi/flask_app.py

209 lines
5.9 KiB
Python
Raw Normal View History

2024-04-12 14:07:48 +00:00
from flask import Flask, render_template , request,send_from_directory, abort
import subprocess
import threading
import json
import time
2024-04-12 14:07:48 +00:00
from werkzeug.utils import secure_filename
import os
dataset = "dataset.json"
absensi = "absensi.json"
app = Flask(__name__, static_folder='assets', template_folder='templates')
# Variable to track the status of the main.py process
main_process = None
# Initialize the scan status as False
scan_status = False
def run_main_py():
global main_process, scan_status
scan_status = True
main_process = subprocess.Popen(['python', 'main.py'], cwd='.')
main_process.wait() # Wait for the process to complete
main_process = None # Reset the process variable
print("main.py process completed.")
scan_status = False
def run_main2_py():
global main_process, scan_status
scan_status = True
main_process = subprocess.Popen(['python', 'main2.py'], cwd='.')
main_process.wait() # Wait for the process to complete
main_process = None # Reset the process variable
print("main2.py process completed.")
scan_status = False
@app.route('/')
def home():
today_date = time.strftime("%d/%m/%Y")
all_data = None
len_all_data = 0
# open absensi file
with open(absensi, 'r') as f:
data = json.load(f)
len_data = len(data)
# if data is not empty
if len_data > 0:
# get the last data
last_data = data[len_data - 1]
# check if "date" is today
if last_data['date'] == today_date:
# get all the "absensi" data
all_data = last_data['absensi']
# get the length of the "absensi" data
len_all_data = len(all_data)
return render_template('index.html', data=all_data, length=len_all_data, today_date=today_date)
2024-04-12 14:07:48 +00:00
@app.route('/faces/<path:filename>')
def serve_image(filename):
try:
return send_from_directory('faces', filename)
except FileNotFoundError:
abort(404)
@app.route('/cek_absensi')
def cek_absensi():
today_date = time.strftime("%d/%m/%Y")
2023-11-25 17:58:25 +00:00
this_month_and_year = time.strftime("%m/%Y")
all_data = None
len_all_data = 0
# open absensi file
with open(absensi, 'r') as f:
data = json.load(f)
len_data = len(data)
# if data is not empty
if len_data > 0:
# get the last data
last_data = data[len_data - 1]
# check if "date" is today
if last_data['date'] == today_date:
# get all the "absensi" data
all_data = last_data['absensi']
# get the length of the "absensi" data
len_all_data = len(all_data)
2023-11-25 17:58:25 +00:00
len_karyawan = 0
data_karyawan = None
# open the dataset file
with open(dataset, 'r') as f:
data_karyawan = json.load(f)
len_karyawan = len(data_karyawan)
this_month_absensi = []
# print(data)
for i in range(len_data):
# print(all_data[i])
if data[i]['date'][3:] == this_month_and_year:
this_month_absensi.append(data[i])
# print(this_month_absensi)
# print(data_karyawan)
# loop data_karyawan and then loop this_month_absensi to check if the name is exist, if exist then add the gaji
for i in range(len_karyawan):
pendapatan = 0
for j in range(len(this_month_absensi)):
for k in range(len(this_month_absensi[j]['absensi'])):
if data_karyawan[i]['name'] == this_month_absensi[j]['absensi'][k]['name']:
pendapatan += data_karyawan[i]['gaji']
data_karyawan[i]['pendapatan'] = pendapatan
print(data_karyawan)
2023-11-25 17:58:25 +00:00
return render_template('index2.html', data=all_data, length=len_all_data, today_date=today_date, data_karyawan=data_karyawan, length_karyawan=len_karyawan , bulan_ini=this_month_and_year)
@app.route('/scan_face')
def scan_face():
global main_process
if main_process is None or main_process.poll() is not None:
# If the main.py process is not running or has completed
# Start a new thread to run the main.py process
thread = threading.Thread(target=run_main_py)
thread.start()
return render_template('scan_face.html')
return "Scanning face is already in progress."
# Create a route that checks for scan_status
@app.route('/scan_status')
def check_scan_status():
global scan_status
return {'scan_status': scan_status}
@app.route('/tambah_karyawan')
def tambah_karyawan():
# open the dataset file
with open(dataset, 'r') as f:
data = json.load(f)
len_data = len(data)
print(data)
return render_template('tambah_karyawan.html', data=data, length=len_data)
@app.route('/tambah_karyawan', methods=['POST'])
def tambah_karyawan_post():
nama = request.form['nama']
nik = request.form['nik']
2023-11-25 17:58:25 +00:00
gaji = int(request.form['gaji'])
2024-04-12 14:07:48 +00:00
foto = request.files['file']
# print(nama)
# print(nik)
2024-04-12 14:07:48 +00:00
if foto:
foto_filename = secure_filename(nama + '.jpg')
foto.save(os.path.join('faces', foto_filename))
# open the dataset file
with open(dataset, 'r') as f:
data = json.load(f)
# insert the new data
data.append({
'name': nama,
2023-11-25 17:58:25 +00:00
'nik': nik,
'gaji': gaji
})
# save the dataset file
with open(dataset, 'w') as f:
json.dump(data, f)
# return the json
return {'status': 'OK'}
2023-11-25 17:58:25 +00:00
# dibawah ni untuk buka main2.py dan loading page
@app.route('/scan_face2')
def scan_face2():
global main_process
if main_process is None or main_process.poll() is not None:
# If the main.py process is not running or has completed
# Start a new thread to run the main.py process
thread = threading.Thread(target=run_main2_py)
thread.start()
return render_template('scan_face.html')
return "Scanning face is already in progress."
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000, debug=True)