how to split a streamlit app into a multi page app with 2 pages , one for input and one…
- User
how to split a streamlit app into a multi page app with 2 pages , one for input and one for results
- Assistant
You can split a Streamlit app into multiple pages by using the
st.sidebarfunction to create a navigation menu that allows users to switch between different pages. Below is an example of how you can create a multi-page app with Streamlit:-
Create two separate Python files for each page:
input_page.pyandresults_page.py. -
In
input_page.py, add the following code:
import streamlit as st def input_page(): st.title('Input Page') # Add your input fields and widgets here input_text = st.text_input('Enter some text') # Add a button to navigate to the results page if st.button('Show Results'): st.experimental_set_query_params(page='results')- In
results_page.py, add the following code:
import streamlit as st def results_page(): st.title('Results Page') # Add your result visualization code here st.write("Here are the results:")- In your main Streamlit app file, import the two page functions and create a simple navigation menu:
import streamlit as st from input_page import input_page from results_page import results_page # Add navigation menu using st.sidebar page = st.sidebar.selectbox('Select Page', ['Input', 'Results']) # Display the selected page if page == 'Input': input_page() else: results_page()- Run the Streamlit app using the command
streamlit run your_app_file.pywhereyour_app_file.pyis your main Streamlit app file.
Now, you should have a multi-page Streamlit app with an input page and a results page. Users can navigate between the two pages using the sidebar menu.
-
- User
how to automatically go to page 2 when user clicks a button in page 1
- Assistant
To automatically navigate to page 2 when a user clicks a button in page 1, you can use Streamlit's
st.experimental_set_query_paramsfunction to add query parameters to the URL, and then check for those parameters in your main app file to determine which page to display.Here's how you can achieve this:
- Update your
input_page.pyfile to include the following code:
import streamlit as st def input_page(): st.title('Input Page') # Add your input fields and widgets here input_text = st.text_input('Enter some text') # Add a button to navigate to the results page and add query parameter if st.button('Show Results'): st.experimental_set_query_params(page='results')- Update your main app file to handle query parameters and display the pages accordingly:
import streamlit as st from input_page import input_page from results_page import results_page # Check for query parameters to determine the current page query_params = st.experimental_get_query_params() page = query_params.get('page', ['input'])[0] # Display the selected page if page == 'input': input_page() else: results_page()Now, when the user clicks the "Show Results" button on the input page, it will automatically navigate to the results page without the need for the user to manually select it from a menu.
Run your Streamlit app as before using
streamlit run your_app_file.pyand test the functionality by clicking the button on the input page. This should automatically direct the user to the results page. - Update your
- User
how to prevent reloading of pagen when i switch back
- Assistant
To prevent the page from reloading when switching back and forth between pages in a multi-page Streamlit app, you can use Streamlit's
st.cachefunction to cache the output of each page function. This will ensure that the page content is stored and not recalculated unnecessarily when navigating between pages.Here's how you can modify your code to implement caching in your multi-page Streamlit app:
- Update your
input_page.pyandresults_page.pyfiles to include the@st.cachedecorator for each page function:
import streamlit as st @st.cache def input_page(): st.title('Input Page') # Add your input fields and widgets here input_text = st.text_input('Enter some text') # Add a button to navigate to the results page and add query parameter if st.button('Show Results'): st.experimental_set_query_params(page='results') @st.cache def results_page(): st.title('Results Page') # Add your result visualization code here st.write("Here are the results:")- Update your main app file to handle query parameters and display the pages accordingly:
import streamlit as st from input_page import input_page from results_page import results_page # Check for query parameters to determine the current page query_params = st.experimental_get_query_params() page = query_params.get('page', ['input'])[0] # Display the selected page if page == 'input': input_page() else: results_page()By adding the
@st.cachedecorator to your page functions, the output of each page will be stored in the cache and not recalculated every time the user switches between pages. This helps to improve the performance of your app and prevents unnecessary reloading of the pages.Run your Streamlit app as before using
streamlit run your_app_file.pyand test the functionality by switching back and forth between pages. You should notice that the pages are displayed instantly without any reloading. - Update your
- User
in streamlit how to prevent main.py to add as a page and only use files inside pages folder
- Assistant
To prevent the main file (
main.py) from being displayed as a page and only use files inside apagesfolder for displaying pages in a Streamlit app, you can organize your code structure and import the page functions from thepagesfolder in the main file.Here's a step-by-step guide on how to achieve this:
-
Create a
pagesfolder in your project directory. Inside thepagesfolder, create separate Python files for each page of your Streamlit app. For example, you can haveinput_page.pyandresults_page.pyinside thepagesfolder. -
Update the page functions in the
pagesfolder to have a consistent structure using Streamlit's functions. -
In your main
main.pyfile, import the page functions from thepagesfolder. You can organize the menu selection and page display in the main file.
Here is an example of how your project structure could look:
project_directory/ │ ├── main.py ├── pages/ │ ├── __init__.py │ ├── input_page.py │ └── results_page.pyIn your main file (
main.py), you can import and display the pages as follows:import streamlit as st from pages import input_page, results_page # Check for query parameters to determine the current page query_params = st.experimental_get_query_params() page = query_params.get('page', ['input'])[0] # Display the selected page if page == 'input': input_page.input_page() else: results_page.results_page()By organizing your code structure in this way, you can prevent the main file from being displayed as a page and only use the files inside the
pagesfolder for displaying the different pages of your Streamlit app. This approach can help keep your codebase manageable and maintain a cleaner project structure. -