Need two page navigaitn and data tranfer code

I kindly request that you provide me with straightforward code that accomplishes the following objectives: the main page should not refresh when navigating, and a new page should open upon clicking. The data should be transmitted to the new page using st.write, without href utilizing button elements. The application should consist of two pages—one serving as the main interface and the other functioning as a user workspace. Data transfer should occur seamlessly from the main page to the user page.

Welcome to the Streamlit community, and thanks for your thoughtful question! :balloon: In Streamlit, navigation between pages (without a full page refresh) and seamless data transfer is best achieved using Streamlit’s multipage app architecture with st.Page and st.navigation, and by storing data in st.session_state. However, it’s important to note that Streamlit always reruns the script on navigation, but session state persists data across pages. Opening a new page in a separate tab (like a browser tab) is not natively supported with button elements—navigation is designed to happen within the same app session, not via href or new tabs.

Here’s a minimal example of a two-page app where clicking a button on the main page navigates to the user workspace and passes data using st.session_state and st.write. This uses st.switch_page for navigation and session state for data transfer, as recommended in the docs. Opening in a new tab is not supported with st.switch_page or button elements—navigation always happens in the same tab. Data is displayed on the user page using st.write:

main_page.py:

import streamlit as st

if "user_data" not in st.session_state:
    st.session_state.user_data = None

user_input = st.text_input("Enter some data:")
if st.button("Go to User Workspace"):
    st.session_state.user_data = user_input
    st.switch_page("user_workspace.py")

user_workspace.py:

import streamlit as st

st.title("User Workspace")
st.write("Received data:", st.session_state.get("user_data", "No data received"))

This approach ensures data is transferred via session state and navigation is handled by Streamlit’s built-in multipage system, but navigation will always occur in the same browser tab and the script will rerun (not a true SPA). For more, see st.switch_page docs and multipage app concepts.

Sources: