Streamlit + PyPerfmon

Summary

Hello, im trying to use pyperfmon on streamlit

Steps to reproduce

Code snippet:

import time
import streamlit as st
from pyperfmon import pyperfmon

st.set_page_config(page_title="Streamlit PC Dashboard")
st.title("PC Dashboard")

pm = pyperfmon.pyperfmon()
pm.connect("localhost")
while True:
    strcpu = (pm.getCounter(r"Processor\0\% Processor Time"))
    strmem = (pm.getCounter(r"Memory\Pages/sec"))
    CPUdados = strcpu[1]
    MemDados = strmem[1]
    col1, col2 = st.columns(2)
    col1.metric("CPU", CPUdados)
    col2.metric("MEM", MemDados)
    time.sleep(1)

as result the colum just keeps reloading, not the data.

it shout just replace the value of the resource.

Debug info

  • Streamlit version: Latest
  • Python version: 3.11
  • OS version: Windows 11
  • Browser version: Chrome Latest

Links

Hi @Allan_Pena,

Thanks for posting!

You can try using the st.empty method to create a placeholder and then update it inside your loop like so:

import time
import streamlit as st
from pyperfmon import pyperfmon

st.set_page_config(page_title="Streamlit PC Dashboard")
st.title("PC Dashboard")

pm = pyperfmon.pyperfmon()
pm.connect("localhost")

col1, col2 = st.columns(2)
cpu_placeholder = col1.empty()
mem_placeholder = col2.empty()

while True:
    strcpu = (pm.getCounter(r"Processor\0\% Processor Time"))
    strmem = (pm.getCounter(r"Memory\Pages/sec"))
    CPUdados = strcpu[1]
    MemDados = strmem[1]
    
    cpu_placeholder.metric(label="CPU", value=CPUdados)
    mem_placeholder.metric(label="MEM", value=MemDados)
    
    time.sleep(1)

Let me know if this resolves the issue.

1 Like

Thank’s mate!

1 Like

Glad it solved the issue. Happy Streamlit-ing! :balloon:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.