This is as much a confirmation from Streamlit developers as I think it might be a good reference for Streamlit users who have similar needs as I do. Here is my solution for simultaneously providing for the three functionalities mentioned in the title above:
Home.py:
import streamlit as st
def main():
selectbox_options = [1, 2, 3]
if 'my_selectbox_key' not in st.session_state:
st.session_state['my_selectbox_key'] = selectbox_options[0]
for key, val in st.session_state.items():
if not key.endswith('__do_not_persist'):
st.session_state[key] = val
st.title('Home page')
st.selectbox('Select from options:', selectbox_options, key='my_selectbox_key')
if __name__ == '__main__':
main()
pages/a_different_page.py:
import streamlit as st
def dummy_function_calling_multiprocessing_functionality():
pass
def main():
# (1) Code like the following is needed to preserve widget state on different Pages
# Can't use "st.session_state.update(st.session_state)" because it will include all widgets including st.data_editor widgets which would throw a "StreamlitAPIException: Values for st.button, st.download_button, st.file_uploader, st.data_editor, st.chat_input, and st.form cannot be set using st.session_state." error
for key, val in st.session_state.items():
if not key.endswith('__do_not_persist'):
st.session_state[key] = val
st.title('Another page')
# (2) Different keys in the data editors are needed to prevent a "DuplicateWidgetID: There are multiple identical st.data_editor widgets with the same generated key." error
st.write('One directory listing:')
st.session_state['data_editor1_edited'] = st.data_editor(['a', 'b', 'c'], key='data_editor1__do_not_persist')
st.write('Another directory listing (same exact contents):')
st.session_state['data_editor2_edited'] = st.data_editor(['a', 'b', 'c'], key='data_editor2__do_not_persist')
dummy_function_calling_multiprocessing_functionality()
# (3) Needed for performing multiprocessing functionality such as using Python's "multiprocessing" module
if __name__ == '__main__':
main()
I would argue that the ability to provide all three of these functionalities (summarized below), while perhaps not originally intended by the Streamlit creators/developers, is important for developing robust workflows that can be managed by a nice Streamlit interface.
Streamlit developers including @blackary, @okld, @mathcatsand, @Goyo, can you please confirm that the above is the correct way to achieve all three of these functions simultaneously: (1) multipage widget state persistence, (2) data_editors with identical contents, and (3) multiprocessing capability?
Thank you, and regardless, thanks Streamlit creators/developers/moderators for all your hard work and such a great product!
Referencing posts:
- How to keep widget value remembered in Multi-page app when switching pages using a streamlit button - Using Streamlit - Streamlit
- Multi-page apps with widget state preservation. The simple way - Show the Community! - Streamlit
- [Request for input] Keyed widget state persistence - discussion, possible fixes · Issue #6074 · streamlit/streamlit (github.com)
- python - Retaining changes for Streamlit data editor when hiding or switching between widgets/pages - Stack Overflow
- Streamlit session_state with multiprocesssing - Using Streamlit - Streamlit