cobot280pi / utils.py
gursi26's picture
added logs
2554ae8
raw
history blame
1.37 kB
import streamlit as st
import uuid, io, sys
def get_user_id():
if "user_id" not in st.session_state:
query_params = st.query_params
user_id = query_params.get("user_id", None)
if not user_id:
user_id = str(uuid.uuid4())
st.query_params["user_id"] = user_id
st.session_state.user_id = user_id
return st.session_state.user_id
class StdoutRedirector(io.StringIO):
def __init__(self, log_area, max_lines=20):
super().__init__()
self.log_area = log_area
self.logs = []
self.max_lines = max_lines
def write(self, message):
if message.strip():
self.logs.append(message.strip())
if len(self.logs) > self.max_lines:
self.logs.pop(0)
self.log_area.markdown(
f"<div class='log-window'>{'<br>'.join(self.logs)}</div>",
unsafe_allow_html=True
)
def render_log_window():
st.markdown("""
<style>
.log-window {
height: 150px;
overflow-y: auto;
font-family: monospace;
background-color: var(--background-color);
color: var(--text-color);
padding: 10px;
border-radius: 5px;
border: 2px solid white;
}
</style>
""", unsafe_allow_html=True)
log_area = st.empty()
stdout_redirector = StdoutRedirector(log_area)
sys.stdout = stdout_redirector