Created
May 29, 2021 19:00
-
-
Save markusbuchholz/c581ab5969f9b499575afb4329084741 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import streamlit as st | |
import numpy as np | |
import plotly.express as px | |
from plotly.subplots import make_subplots | |
import plotly.graph_objects as go | |
import time | |
import psutil | |
start_button = st.empty() | |
placeholderA = st.empty() | |
placeholderB = st.empty() | |
CPU_CORE = psutil.cpu_count() | |
monitor_colors = ['rgb(33, 75, 99)', 'rgb(79, 129, 102)', 'rgb(151, 179, 100)', | |
'rgb(175, 49, 35)', 'rgb(36, 73, 147)'] | |
def monitor(): | |
cpu = psutil.cpu_percent(interval=1, percpu=True) | |
mem_virtual = psutil.virtual_memory() | |
mem_swap = psutil.swap_memory() | |
pids = psutil.pids() | |
core = [] | |
load_procent = [] | |
for i in range(CPU_CORE): | |
core.append('cpu'+str(i)) | |
load_procent.append(cpu[i]) | |
mem = ['total', 'available', 'used'] | |
mem_procent = ['percent'] | |
labels = ['percent', 'free'] | |
mem_info = [mem_virtual.total/10**9, mem_virtual.available/10**9, mem_virtual.used/10**9] | |
mem_info_procent = [mem_virtual.percent] | |
values = [mem_virtual.percent, (100 - mem_virtual.percent)] | |
figA = make_subplots(rows=1, cols=2) | |
figB = make_subplots(rows=1, cols=2) | |
figC = make_subplots(rows=1, cols=2) | |
figA.add_trace(go.Bar(x=core,y=load_procent, name='CPU', text='CPU'),row=1,col=1) | |
figA.add_trace(go.Bar(x=mem,y=mem_info, name='Memory', text='Memory'),row=1,col=2) | |
figB = go.Figure(data=[go.Pie(labels=labels, values=values, textinfo='label+percent', | |
insidetextorientation='radial', marker_colors=monitor_colors | |
)]) | |
figA.update_layout(height=400, width=800, title_text="CPU. " + " Memory") | |
figB.update_layout(height=400, width=800, title_text="Actual RAM usage") | |
placeholderA.write(figA) | |
placeholderB.write(figB) | |
if start_button.button('Start',key='start'): | |
start_button.empty() | |
if st.button('Stop',key='stop'): | |
pass | |
while True: | |
monitor() | |
time.sleep(1.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment