-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
36 lines (30 loc) · 1.09 KB
/
app.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
from flask import Flask, render_template
import psutil
import time, random, threading
from turbo_flask import Turbo
from util import CPUutil
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
turbo = Turbo(app)
@app.route('/')
def index():
return render_template('index.html')
@app.before_request
def before_request():
thread = threading.Thread(target=update_load, daemon=True)
thread.start()
def update_load():
with app.app_context():
while True:
try:
time.sleep(1)
turbo.push(turbo.replace(render_template('loadavg.html'), 'load'))
except Exception as e:
print(f"Error in update_load: {e}")
break
@app.context_processor
def inject_load():
load = [str(CPUutil.getCpuLoadWindows()) + " %", str(round(psutil.virtual_memory().used / (1024 ** 3), 1)) + " GB", str(round(psutil.virtual_memory().free / (1024 ** 3) , 1)) + " GB"]
return {'loadCPU': load[0], 'loadRAM_used': load[1], 'loadRAM_free': load[2]}
if __name__ == '__main__':
app.run()