How to split pages in partials

Hello,

I just started using Streamlit. Iโ€™m building a multipage application.

The page under the root will be a welcome page explaining the various operations performed by the subpages.

Each subpage will be very complex, so my ideal workflow would be to split the contents of these subpages into partials, importend inside the subpage.

Example of how Iโ€™d like to organize my code:

app_directory
+-- welcome.py 
+-- pages
     +-- subpage1.py
     +-- subpage2.py
     +-- partials
          +-- subpage1_header.py
          +-- subpage1_body1.py
          +-- subpage1_body2.py
          +-- subpage1_footer.py
          +-- subpage2_header.py
          +-- subpage2_body1.py
          +-- subpage2_body2.py
          +-- subpage2_footer.py

I donโ€™t want to use the magic commands, Iโ€™ll rather stick to the st.write convention.

So my question is as follows: is it possible to put the Streamlit elements inside these partials and then import the partials in the subpage, in order to have the subpage code less complex?

If yes, could you address me to a guide on how to realize it? My attempt at doing this with import statements didnโ€™t work.

Thanks in advance for any help.

Hey @umberto.biscotti,

Thanks for sharing this question!

Just to clarify, is the main motivation for splitting up the pages into multiple files just to keep the files shorter? Or is there something else youโ€™re trying to accomplish?

I would like this as well. It is much cleaner to manage an authentication page if I were able to do something like this:

import os
import streamlit as st
from streamlit_cookies_manager import EncryptedCookieManager
from dotenv import load_dotenv
from auth import authenticate
from partials import landing, login_form, register_form

# API Base URL
api_base_url = st.secrets["API_BASE_URL"]

# Create a cookie manager instance
cookie_manager = EncryptedCookieManager(
    prefix=st.secrets["COOKIE_MANAGER_PREFIX"],
    password=st.secrets["COOKIE_MANAGER_PASSWORD"]
)

def main():
    # Wait for the component to load and send us current cookies.
    if not cookie_manager.ready():
        st.stop()

    # Check if the user is already authenticated
    access_token = cookie_manager.get("access_token")

    st.header("AI Toolbox")

    if access_token:
        st.session_state["access_token"] = access_token
        landing()

    else:
        login_tab, register_tab = st.tabs(["Login", "Register"])

        with login_tab:
            access_token = login_form()

            if access_token:
                # Store the access token in a cookie
                cookie_manager.set("access_token", access_token)

                st.rerun()
            else:
                st.warning("Invalid credentials. Please try again.")

        with register_tab:
            access_token = register_form()

            if access_token:
                # Store the access token in a cookie
                cookie_manager.set("access_token", access_token)

                st.rerun()
            else:
                st.warning("Unable to authenticate. Please try again.")



if __name__ == "__main__":
    load_dotenv()
    main()