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.
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.
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)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.