I get sensor data from a UDP socket (blocking) and would like to display changes on Sreamlit.
I’m trying to display thoes changes with the function write() from Streamlit.
For some reason the function place.write(data) gets only executed every third time.
Meanwhile the logging.info(f'data: {data}') gets execueted every time.
Expectation:
The write() function prints every time the logging function prints something out.
Code:
import socket
import streamlit as st
import time
import logging
import sys
UDP_IP = '127.0.0.1'
UDP_PORT = 1623
RUN = 1
st.header('Listening')
logging.info('run')
@st.cache
def bind_socket(ip,port):
logging.info('bind socket')
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.bind((ip, port))
return sock
place = st.empty()
sock = bind_socket(UDP_IP, UDP_PORT)
if st.button('stop'):
logging.info('stop')
RUN = 0
def main():
global RUN
try:
while RUN:
time.sleep(0.2)
logging.info('try')
data, addr = sock.recvfrom(65535)
place.write(data)
logging.info(f'data: {data}')
except Exception :
logging.info(f'except: {repr(Exception)} sys: {sys.exc_info()}')
finally:
logging.info('finally')
sock.close()
if __name__ == "__main__":
main()
Output of logger:
Output Streamlit: (changed place.write(data) to st.write(data)
Does someone know why it skypes write() and how to fix it?
What version of Streamlit are you using? I was unable to reproduce the behavior with Streamlit 1.7.0. Both logging.info() and st.write() get executed for every iteration of the while loop
Here’s a simple UDP client I used, along with your identical app:
Hey @snehankekre thanks for the fast replay.
I solved the problem but the behavior remains strange.
I’m using Anaconda with
Streamlit V1.5.0
Python V3.9
Just updatet to Streamlit V1.7.0 the issue remains.
I’m running your udp-client.py code.
I figured out by using another browser i’m getting a different output.
Running two browsers parallel Chrome and Brave while running the code the output looks like: Chrome: Brave:
The Issue:
When opening multiple tabs or browsers with the same streamlit address (http://localhost:8501/) the write() function distributes the output to only one of the tabs (like round robin). resulting in skipping displaying values.
Solution:
Close all other tabs of Streamlit in the browser.