New Component: Streamlit-router allows you to create truly production-level multi-page applications

As part of a series of shared components, I will be sharing a multi-page Streamlit solution that was developed over two years ago. Developing apps with Streamlit is often limited to simple small script applications, but Streamlit-Router allows you to develop large-scale web applications without any performance or engineering scale burden. Streamlit-router provides a very clean syntax and interface for Python web development, making multi-page development very simple.

20240423141943

Install

pip install streamlit-router

Basic Usage

    def index(router):
        st.text("fron page index")
        x = st.number_input("task id")
        if st.button("create task"):
            router.redirect(*router.build("create_task", {"x": x}))
        if st.button("cancel task"):
            router.redirect(*router.build("cancel_task", {"x": x}))
        if st.button("view task"):
            router.redirect(*router.build("view_task", {"x": x}))
        st.text("others on page index")

    # variable router auto inject if as first params
    def cancel_task(router, x):
        st.text(f"fron page cancel task x={x}")
        if st.button("back to index"):
            router.redirect(*router.build("index"))
        st.text("others on page cancel task")

   # variable router auto inject if as first params
    def create_task(x, router):
        st.text(f"fron page create task x={x}")
        if st.button("back to index"):
            router.redirect(*router.build("index"))
        st.text("others on page create task")

    router = StreamlitRouter()
    router.register(index, '/')
    router.register(cancel_task, "/tasks/<int:x>", methods=['DELETE'])
    router.register(create_task, "/tasks/<int:x>", methods=['POST'])

    # deco also works
    @router.map("/tasks/<int:x>")
    def view_task(x):
        st.text(f"fron page view task x={x}")
        if st.button("back to index 2"):
            router.redirect(*router.build("index"))
        st.text("others on page view task")

    router.serve()
3 Likes

@mapix_i This looks like a very powerful approach to managing multi-page apps in Streamlit.

As I understand it, we could pass parameters through to each page URL in order to execute the task we want. Are there any security concerns with passing sensitive parameters through the page URL though? How do we deal with this? Thanks for any perspective you can give.

In the Streamlit framework, it is always recommended to use session state to pass sensitive information, as it works across multiple pages (router include). Additionally, the router library provides capabilities such as set_request_state, which can be used to manage temporary sensitive information within a page. This information will be automatically cleared when switching between pages.

I see thanks.