I have managed to send the pandas dataframe to the second page dynamically: when the user changes the tickers, the pandas dataframe updates. Is this the correct way to do it?
when I go back to page 1 the page loads again triggering the st.multiselect default selection. How can I retain the tickers selected by the user? example: page 1 loads with the default tickers: GOOG,AAPL user adds another ticker: TSLA and then navigates to page 2, when he returns to page 1 we want to keep his/her selection (all three tickers GOOG,AAPL and TSLA). how can we achieve that?
please see sample code:
page 1:
import pandas as pd
import streamlit as st
import yfinance as yf
default_tickers = ['GOOG','AAPL']
stocks = ['GOOG','AAPL','TSLA','MSFT','AMZN']
st.write("### Select Stocks:")
multiselection = st.multiselect(options= stocks, label= 'multiselect', \
default= default_tickers \
)
start = pd.to_datetime('2021/10/06')
end = pd.to_datetime('2022/10/06')
# Read data
df = yf.download(multiselection,start,end)['Close']
if 'key' not in st.session_state:
st.session_state['key'] = df
st.session_state.key = df
def print_df():
df = st.write(st.session_state.key)
return df
st.write(print_df())
page 2:
import sys
import path
# directory reach
directory = path.Path(__file__).abspath()
# setting path
sys.path.append(directory.parent)
#from my_modules.hide_head_foot import hide
import streamlit as st
from sessionstate import print_df
df = print_df()
st.write(df)
But, you can accomplish what you’re looking for by manually keeping the selected stock options in session state, as well as the stock data. Here’s one way to do it:
page1
import pandas as pd
import streamlit as st
import yfinance as yf
default_tickers = ["GOOG", "AAPL"]
stocks = ["GOOG", "AAPL", "TSLA", "MSFT", "AMZN"]
st.write("### Select Stocks:")
if "stock_options" not in st.session_state:
st.session_state["stock_options"] = default_tickers
multiselection = st.multiselect(
options=stocks,
label="multiselect",
default=st.session_state["stock_options"],
)
st.session_state["stock_options"] = multiselection
start = pd.to_datetime("2021/10/06")
end = pd.to_datetime("2022/10/06")
# NOTE: This isn't required, but will make the app automatically reuse the data once it's been loaded the first time
@st.experimental_memo
def get_data(stocks, start, end):
df = yf.download(stocks, start, end)["Close"]
return df
df = get_data(multiselection, start, end)
st.session_state["key"] = df
st.write(st.session_state["key"])
page2
import streamlit as st
df = st.session_state["key"]
st.write(df)
Yup! You can either add a comment about your use-case if you think it’s different from what’s already been written, or you can hit the Subscribe button on the right
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.