Create a new page on click of submit

Hi All,

I am creating a new app to insert student records, from my main app page(Create.py) I insert records in to database. Using multipage concept I have created Search.py and Delete.py under /pages/ folder to search and delete records.

Upon clicking “Insert” in Create.py I intend to display the new records inserted in to database in a new page.

To achieve this I created a new View_Record.py in the same folder as Create.py and tried to do st.switch_page(“View_Record.py”) which gives me below error:

streamlit.errors.StreamlitAPIException: Could not find page:
C:\Users\SK\Desktop\python\View_Record.py. Must be the file path relative to the main script, from the directory: python. Only the main app file and files in the pages/ directory are supported.

To encounter this I placed the View_Record.py under /pages , this absolutely works fine to switch page but the “View Record” also gets listed in the side bar which I dont want.

I also tried hide this page using st_pages but unfortunately streamlit cannot switch to a page which is hidden.

Any pointers on how to achieve this? How would I launch a new page to display the record I inserted into DB by clicking “insert” button.

  1. Streamlit has an option to conditionally display sidebar links.
    You do this by running streamlit with the option to disable the sidebar rendering. Depending on the version of streamlit, the option name changes. In latest version of streamlit the option is client.showSidebarNavigation
    You can then use “st.page_link” inside “with st.sidebar” as you see fit in the page.
    Check this: Create custom navigation menus - Streamlit Docs

  2. Have you tried “switch_page” link with a “./” prefix perhaps? I dont know but worth a try…

1 Like

Here’s an example that works just fine, if client.showSidebarNavigation is set to false

streamlit_app.py

import streamlit as st
from pathlib import Path


for page in Path("pages").iterdir():
    left, right = st.columns(2)
    if left.button("Switch to " + page.name):
        st.switch_page(page)
    if right.button("Delete " + page.name):
        page.unlink()
        st.rerun()

page_name = st.text_input("Page name")
if st.button("Add new page"):
    Path("pages", f"{page_name}.py").touch()
    st.rerun()

config.toml

[client]
showSidebarNavigation = false
2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.