Create ui.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import streamlit as stimport psutilimport speedtestimport plotly.graph_objects as goimport time
# Function to get CPU and memory usagedef get_system_metrics(): cpu_usage = psutil.cpu_percent(interval=1) memory_info = psutil.virtual_memory() memory_usage = memory_info.percent return cpu_usage, memory_usage
# Function to get internet speeddef get_internet_speed(): st = speedtest.Speedtest() download_speed = st.download() / 1_000_000 # Convert to Mbps upload_speed = st.upload() / 1_000_000 # Convert to Mbps return download_speed, upload_speed
# Function to create a gauge chartdef create_gauge_chart(value, title, min_value=0, max_value=100): fig = go.Figure(go.Indicator( mode="gauge+number", value=value, title={'text': title}, gauge={'axis': {'range': [min_value, max_value]}, 'bar': {'color': "lightblue"}, 'bgcolor': "white", 'steps': [{'range': [min_value, max_value], 'color': "lightgrey"}], 'threshold': {'line': {'color': "red", 'width': 4}, 'thickness': 0.75, 'value': max_value}})) return fig
# Streamlit layoutst.title("System Monitoring Dashboard")
# Create columns for each gaugecol1, col2 = st.columns(2)col3, col4 = st.columns(2)
# Update the metrics in real-timewhile True: cpu_usage, memory_usage = get_system_metrics() download_speed, upload_speed = get_internet_speed()
# Create gauge charts with col1: st.plotly_chart(create_gauge_chart(cpu_usage, "CPU Usage"), use_container_width=True) with col2: st.plotly_chart(create_gauge_chart(memory_usage, "Memory Usage"), use_container_width=True) with col3: st.plotly_chart(create_gauge_chart(download_speed, "Download Speed (Mbps)", 0, 1000), use_container_width=True) with col4: st.plotly_chart(create_gauge_chart(upload_speed, "Upload Speed (Mbps)", 0, 1000), use_container_width=True)
# Refresh the metrics every 5 seconds time.sleep(5)ui.pyAdded+57