Skip to main content

A solid project structure from basit to advanced streamlit applications.

Project description

Streamlit Project Template

A streamlit project template to create cleaner code and cost-efficient maintable apps.

Template Hierarchy:

MyProject
|-- src
|  |-- __init__.py 
|  |-- callbacks.py
|  |-- components.py
|  |-- pages.py
|-- .gitignore
|-- app.py
|-- Dockerfile
|-- README.md
|-- requirements.txt

Installation:

$ pip install streamlit-project-template

Installation with streamlit-extras:

$ pip install streamlit-project-template[extras]

Create the Template

$ mkdir MyProject
$ cd MyProject
$ streamlit-project-template createtemplate

Docs:

  • MyProject: This is the top-level project folder, which you initialize your application template.

  • src: In this folder, you keep your modules for your application. It will start with 3 main modules (callbacks.py, components.py, pages.py) which are essential for this app architecture, but you can add more modules besides them.

  • __init__.py: When calling a module that is in another directory, python might not be able to find it. To solve this problem, we will create an __init__.py file in src, in order to tell python to treat this folder as a library.

  • callbacks.py: This file(s) will contain your streamlit callbacks.

    def set_page_home():
        """
        example callback that will set the selected page to 'home'
        """
        st.session_state['selected_page'] = 'home'
    
    
    def set_page_mypage1():
        """
        example callback that will set the selected page to 'mypage1'
        """
        st.session_state['selected_page'] = 'mypage1'
    
    
    def set_page_mypage2():
        """
        example callback that will set the selected page to 'mypage2'
        """
        st.session_state['selected_page'] = 'mypage2'
    
  • components.py: This is where you create your components. Each component is a one or multiple row of streamlit elements. This way you can use a component in multiple places without rewriting it and it will make maintaining easier and cost efficient.

    def component_say_hello():
        """
        example component with a home button and header
        """
        st.button('Home', on_click=callbacks.set_page_home)
        st.header('Hello World!')
    
    
    def component_change_page():
        """
        example component with a page selection
        """
        st.write('Where do you want to go?')
        st.button('Page 1', on_click=callbacks.set_page_mypage1)
        st.button('Page 2', on_click=callbacks.set_page_mypage2)
    
    
    def component_mypage1_title():
        """
        example component with mypage1 page info
        """
        st.write('My Page 1')
    
    def component_mypage2_title():
        """
        example component with mypage2 page info
        """
        st.write('My Page 2')
    
  • pages.py: In this file you will hold your pages as classes, which are built with components row by row as defined in yur components.py file(s).

    # HOME PAGE 
    class HomePage():
        """
        Your Home / Landing Page
        Here you can add your defined components under the loadPage() function
        """
        @staticmethod
        def load_home_page():
            """
            example home page load function
            """
            components.component_say_hello()
            components.component_change_page()
    
    
    # MYPAGE1
    class MyPage1():
        """
        Example Page class
        """
        @staticmethod
        def load_mypage1():
            """
            example page load function
            """
            components.component_say_hello()
            components.component_mypage1_title()
    
    
    # MYPAGE2
    class MyPage2():
        """
        Example Page class
        """
        @staticmethod
        def load_mypage2():
            """
            example page load function
            """
            components.component_say_hello()
            components.component_mypage2_title()
    
  • app.py: This is the main file you will run. It initialliy contains a page config, a session state which holds your current page name and the page navigation. All the session states should be initialized here, and the page navigation should be managed here in order to keep the project architecture organized and run one page at a time to reduce resource cost.

    # set session states
    if 'selected_page' not in st.session_state:
        st.session_state['selected_page'] = 'home'
    
    
    # page navigation
    match st.session_state['selected_page']:
        case 'home':
            HomePage.load_home_page()
    
        case 'mypage1':
            MyPage1.load_mypage1()
    
        case 'mypage2':
            MyPage2.load_mypage2()
    
  • .gitignore: Your standard .gitignore file.

  • Dockerfile: Initial dockerfile which is configured to run streamlit.

  • README.md: Introduce your project here.

  • requirements.txt: Required packages for your application will be here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit-project-template-0.1.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file streamlit-project-template-0.1.1.tar.gz.

File metadata

File hashes

Hashes for streamlit-project-template-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c58a68f6ec20cbdf055e27ff1982e1a1ef5c4742126c5e0282baf9c3b7000f5c
MD5 693320d5dd1882c9a30b0cec0215dca7
BLAKE2b-256 606fc31c03d008d0ed2ef1bf376df8aba6e065cf74e5fbc5309c790ec9798f44

See more details on using hashes here.

File details

Details for the file streamlit_project_template-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_project_template-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3f2a6b741c898afc6003a8ca0e58220338024f3f17ee495488f7c760ae650792
MD5 59b76253823ca6a79253f264bd66e760
BLAKE2b-256 2093eee52a33d5cfbaf4639574bddd7f8ee8d68c0e12c0db5a49feed6bd4b583

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page