Realtime data update to streamlit from python variable
Question:
New to python world. I am trying to understand how to update realtime data in streamlit app.(Reference Blog).
I am looping through the numbers 1 to 100, display the number
along with number * 10
. But streamlit always shows number as 1. How can I update the numbers(realtime update) in streamlit?
My code:
import threading
import streamlit as st
import time
global val, multiply
def test_run():
global val, multiply
for x in range(1, 100):
val = x
multiply = val * 10
print(val)
time.sleep(1)
return val, multiply
threading.Thread(target=test_run).start()
# dashboard title
st.title("Stramlit Learning")
# creating a single-element container.
placeholder = st.empty()
with placeholder.container():
col1, col2 = st.columns(2)
col1.metric(label="Current Value", value=val)
col2.metric(label="Multiply by 10 ", value=multiply)
Answers:
Here is a fixed version of your code: Values of the variables will get updated in every second.
import threading
import streamlit as st
import time
import queue
q = queue.Queue()
def test_run():
for x in range(1, 100):
val = x
multiply = val * 10
q.put((val, multiply))
print(val)
time.sleep(1)
def update_dashboard():
while True:
val, multiply = q.get()
col1, col2 = st.columns(2)
col1.metric(label="Current Value", value=val)
col2.metric(label="Multiply by 10 ", value=multiply)
threading.Thread(target=test_run).start()
# dashboard title
st.title("Streamlit Learning")
with st.empty():
update_dashboard()
New to python world. I am trying to understand how to update realtime data in streamlit app.(Reference Blog).
I am looping through the numbers 1 to 100, display the number
along with number * 10
. But streamlit always shows number as 1. How can I update the numbers(realtime update) in streamlit?
My code:
import threading
import streamlit as st
import time
global val, multiply
def test_run():
global val, multiply
for x in range(1, 100):
val = x
multiply = val * 10
print(val)
time.sleep(1)
return val, multiply
threading.Thread(target=test_run).start()
# dashboard title
st.title("Stramlit Learning")
# creating a single-element container.
placeholder = st.empty()
with placeholder.container():
col1, col2 = st.columns(2)
col1.metric(label="Current Value", value=val)
col2.metric(label="Multiply by 10 ", value=multiply)
Here is a fixed version of your code: Values of the variables will get updated in every second.
import threading
import streamlit as st
import time
import queue
q = queue.Queue()
def test_run():
for x in range(1, 100):
val = x
multiply = val * 10
q.put((val, multiply))
print(val)
time.sleep(1)
def update_dashboard():
while True:
val, multiply = q.get()
col1, col2 = st.columns(2)
col1.metric(label="Current Value", value=val)
col2.metric(label="Multiply by 10 ", value=multiply)
threading.Thread(target=test_run).start()
# dashboard title
st.title("Streamlit Learning")
with st.empty():
update_dashboard()