streamlit st.columns to show live stock data

Question:

Problem statement:
I need multiple columns in streamlit to categorize different stocks and show their current live value.
I have 2 dummy columns as shown below (in the code) to track live values but the problem is instead of overwriting the current ticker value, it starts to append the column and write new values below the existing one.

import streamlit as st
import yfinance as yf

st.set_page_config(page_title="Test", layout='wide')
tech_list = ['btc-usd', 'eth-usd']
etf_list = ['etc-usd']


(tech_ticker, tech_price, etf_ticker, etf_price, crypto_ticker, crypto_price,
finance_ticker, finance_price, transport_ticker, transport_price) = st.columns(10, gap='small')


with tech_ticker:
    for index, val in enumerate(tech_list):
        st.write(val)

with etf_ticker:
    for index, val in enumerate(etf_list):
        st.write(val)

while True:
    with tech_price:
        number1 = st.empty()
        with number1.container():
            for index, val in enumerate(tech_list):
                stock = yf.Ticker(val)
                price = stock.info['regularMarketPrice']
                st.write(": ", round(price, 1))

    with etf_price:
        number2 = st.empty()
        with number2.container():
            for index, val in enumerate(etf_list):
                stock = yf.Ticker(val)
                price = stock.info['regularMarketPrice']
                st.write(": ", round(price, 1))
Asked By: aps_s

||

Answers:

Put all the columns to an empty container:

import streamlit as st
import yfinance as yf

st.set_page_config(page_title="Test", layout='wide')
tech_list = ['btc-usd', 'eth-usd']
etf_list = ['etc-usd']


with st.empty():
    while True:
        (tech_ticker, tech_price, etf_ticker, etf_price, crypto_ticker,
         crypto_price, finance_ticker, finance_price, transport_ticker,
         transport_price) = st.columns(10, gap='small')

        with tech_ticker:
            for index, val in enumerate(tech_list):
                st.write(val)

        with etf_ticker:
            for index, val in enumerate(etf_list):
                st.write(val)

        with tech_price:
            for index, val in enumerate(tech_list):
                stock = yf.Ticker(val)
                price = stock.info['regularMarketPrice']
                st.write(": ", round(price, 1))

        with etf_price:
            for index, val in enumerate(etf_list):
                stock = yf.Ticker(val)
                price = stock.info['regularMarketPrice']
                st.write(": ", round(price, 1))

Answered By: Jamiu Shaibu

Define the holder outside the infinite loop.


# with etf_ticker:
#    for index, val in enumerate(etf_list):
#        st.write(val)

# Define holders.
with tech_price:
    number1 = st.empty()

with etf_price:
    number2 = st.empty()

# Update data - infinite loop
while True:        
    with number1.container():
        for index, val in enumerate(tech_list):
            stock = yf.Ticker(val)
            price = stock.info['regularMarketPrice']
            st.write(": ", round(price, 1))

    with number2.container():
        for index, val in enumerate(etf_list):
            stock = yf.Ticker(val)
            price = stock.info['regularMarketPrice']
            st.write(": ", round(price, 1))

Output

enter image description here

Answered By: ferdy
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.