Live Stock Price Chart -- Processing CSV updates

I have a program which displays a graph after a user clicks a check box:

# Declaring the format of the webapp

Page_Title = st.write('<h1>Sector Volatility Indices</h1>', unsafe_allow_html = True)
Communications_Selection = st.sidebar.checkbox(label = 'Communications')
Discretionary_Selection = st.sidebar.checkbox(label = 'Consumer Discretionary')


Sector_ETFs = [Item 1, Item 2]

def Straddle_Creator():
    Big_Data = pd.DataFrame()
    
    while 1:
    # goes through each item in list and creates big_data, which is a dataframe that holds values from the api call
        for ETF in Sector_ETFs:
            operation to get api call and send to DF1 and DF2
            Big_Data = pd.concat([DF1.reset_index(), DF2.reset_index().add_prefix('CD '), 
            Big_Data = Big_Data.set_index('Time')
            
        try:

        # When the respective checkboxes are chose, st.line_chart shows a graph on the webapp.

            if  Communications_Selection and Discretionary_Selection:
                st.line_chart(Big_Data[['Communications', 'CD Consumer Discretionary']])
    
            elif Communications_Selection:
                st.line_chart(data = Big_Data['Communications'])
    
            elif Discretionary_Selection:
                st.line_chart(data = Big_Data['CD Consumer Discretionary'])
   
        except:
            pass
        
Straddle_Creator()

Because of the while 1 loop, the streamlit app constantly runs and does not allow the user to see any graphs. This happens because when a user selects a checkbox, it re-runs the entire program with the the checkbox variable set to True and then returns the output.

So, is the best practice to create the changing dataframe in file1, then save the dataframe to a csv each time, then have file2 read that csv and apply the checkboxes? File 2 would be near instantaneous, but how would I implement this if the program cannot continuously run?

For further clarification, I am trying to implement this:

while1:

Data = pd.read_csv(updated_dataframe.csv)

if checkbox_selected:
st.line_chart()

The problem is that streamlit will just keep the webapp at “running…” because of the while loop. So what can I do to run this indefinitely? The program must continuosly receive the new data.

Can you explain why it needs to run indefinitely? Is there constantly new data coming in, and you want this app to serve as a live dashboard?

What happens if you remove the while 1? Does the app work as expected on one run?

Yes, it does run as intended without the while 1. I’m attempting to make it run indefinitely because it is reading from a csv which is constantly expanding because a different program saves new data to it.

Currently, I am able to implement this by not using the while loop and clicking the checkbox to refresh the graph (since it re-runs the function each time). However, constantly having to click and unclick a box to refresh the graph will be very unintuitive for the end user.

So is there a way to have streamlit rerun every n seconds?

In that case, you could try adding a sleep() at the end of each while loop, so that it pauses after drawing the graphs. You also might make two st.empty()s outside of the loop to create placeholders for the graphs so that they don’t get destroyed while the for ETF... section is running.

In streamlit, the program has to come to a complete stop in order to be interacted with. Because of this, any script that still has an active terminal will be displayed by streamlit as “running…”, because, in fairness, it is.

You can ignore everything in the first code sample. My reply with code is what I’m sending to streamlit.

Step 1: Read CSV →
Step 2: Check checkbox condition →
Step 3: If checked, display graph →

Problem = → Step 4: Repeat

Thank you

Sorry, I wasn’t reading the specific problem carefully. One option is to use tabs instead of checkboxes, like this:

from time import sleep

import pandas as pd
import streamlit as st


def get_data() -> pd.DataFrame:
    sleep(2) # simulate long-running process
    return pd.DataFrame(
        {
            "a": [1, 2, 3],
            "b": [4, 5, 6],
            "x": [7, 8, 9],
        }
    )


def show_graph(df: pd.DataFrame, y):
    st.line_chart(df, x="x", y=y)


placeholder = st.empty()

while True:
    # use placeholder so they graph doesn't get created multiple times
    with placeholder.container():
        a, b, both = st.tabs(["a", "b", "both"])

    df = get_data()

    with a:
        show_graph(df, "a")
    with b:
        show_graph(df, "b")
    with both:
        show_graph(df, ["a", "b"])

    sleep(1)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.