How to create a page in the code?

β€œI am familiar with creating a multi-page app using Streamlit. However, I am now want to know how to dynamically create new pages in real-time instead of having to create a .py script in advance in the pages folder. Can anyone guide me on how to implement a functionality where clicking a button would generate a new page?”

st.write(f'{porject_id}')
st.markdown(f"<a href='{project_id}' target='_blank'>{project_id}</a>", unsafe_allow_html=True)

Here, the detail page is dynamic, based on the corresponding project_id.

Here’s one way to do it

from pathlib import Path

import streamlit as st

page_name = st.text_input("Page name", "page2")

if page_name and st.button("Create new page"):
    (Path("pages") / f"{page_name}.py").write_text(
        f"""
import streamlit as st

st.write("Hello from {page_name}")
""",
        encoding="utf-8",
    )

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