python Streamlit不 Flink 地更新/覆盖列值

7hiiyaii  于 2022-12-05  发布在  Python
关注(0)|答案(1)|浏览(122)

问题陈述:我有两个专栏流光:一个用于ticker_symbol,另一个用于它的当前值。我希望每秒更新current_value(column2),但我目前的代码首先删除了current_price的写入值,然后写入新值。我希望current_value被覆盖而不被删除。这也意味着列中的最后一个ticker_symbol必须等待很长时间才能显示出来。的当前值,因为st.empty已删除前一个值。
我能做些什么来达到上面提到的目标?我应该不使用st.empty吗?在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()
8mmmxcuj

8mmmxcuj1#

这样做。

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)

样本模拟代码随机。

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)

相关问题