选择框有一个索引参数来表示将显示在框中的值。我们可以用它来更新盒子。我们将使用会话状态来更新所有页面中的索引。 st.session_state.country 已跟踪国家/地区值。
主程序
import streamlit as st
if 'index' not in st.session_state:
st.session_state.index = 0
if 'countries' not in st.session_state:
st.session_state.countries = ('France', 'Spain', 'Italy',
'England', 'Belgium', 'Portugal','Sweden')
st.header('Main')
st.write('Countries')
st.dataframe(st.session_state.countries)
篮球.py
页面/baskeball.py
import streamlit as st
st.header('Basketball')
Host_Country = st.selectbox(
label='Select HomeTeamName name:',
options=st.session_state.countries,
index=st.session_state.index,
key='country')
# Update the index. It is used in the selectbox.
st.session_state.index = st.session_state.countries.index(st.session_state.country)
st.write(f'value of country: {st.session_state.country}')
排球.py
页面/volleyball.py
import streamlit as st
st.header('Volleyball')
Host_Country = st.selectbox(
label='Select HomeTeamName name:',
options=st.session_state.countries,
index=st.session_state.index,
key='country')
# Update the index. It is used in the selectbox.
st.session_state.index = st.session_state.countries.index(st.session_state.country)
st.write(f'value of country: {st.session_state.country}')
样本
篮球选择西班牙。
转到排球页面。
国家都一样!