Problem statement: I have 2 columns on streamlit: one for ticker_symbol and other for it's current value. I want to update the current_value every second (column2) but the code I have so far first removes the written value of the current_price and then writes the new value. I would like the current_value to be overwritten without being removed at all. This also means the last ticker_symbol in the col has to wait for a long time to show it's current value since the previous value gets removed by st.empty.
What can I do to achieve the goal mentioned above? Should I not use st.empty ? Are there any other alternatives in streamlit ?
import time
import yfinance as yf
import streamlit as st
st.set_page_config(page_title="Test", layout='wide')
stock_list = ['NVDA', 'AAPL', 'MSFT']
left, right, blank_col1, blank_col2, blank_col3, blank_col4, blank_col5, blank_col6, blank_col7, blank_col8, blank_col9, \
blank_col10 = st.columns(12, gap='small')
with left:
for index, val in enumerate(stock_list):
st.write(val)
with right:
while True:
numbers = st.empty()
with numbers.container():
for index, val in enumerate(stock_list):
stock = yf.Ticker(val)
price = stock.info['regularMarketPrice']
# st.write(": ", price)
st.write(": ", price)
time.sleep(0.5)
numbers.empty()
CodePudding user response:
Do like this.
with right:
# The number holder.
numbers = st.empty()
# The infinite loop prevents number holder from being emptied.
while True:
with numbers.container():
for index, val in enumerate(stock_list):
stock = yf.Ticker(val)
price = stock.info['regularMarketPrice']
st.write(": ", price)
time.sleep(0.5)
Sample simulation code with random.
import time
import streamlit as st
import random
st.set_page_config(page_title="Test", layout='wide')
stock_list = ['NVDA', 'AAPL', 'MSFT']
(left, right, blank_col1, blank_col2, blank_col3, blank_col4,
blank_col5, blank_col6, blank_col7, blank_col8, blank_col9,
blank_col10) = st.columns(12, gap='small')
with left:
for index, val in enumerate(stock_list):
st.write(val)
with right:
numbers = st.empty()
while True:
with numbers.container():
for index, val in enumerate(stock_list):
price = random.randint(-100, 100)
st.write(": ", price)
time.sleep(0.5)