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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.