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 insrc
, 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 yurcomponents.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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file streamlit-project-template-0.1.2.tar.gz
.
File metadata
- Download URL: streamlit-project-template-0.1.2.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2afec9ef0873bae6950742bdb3beeddbfb6928f07680d58ba497f32c3b8c4f58 |
|
MD5 | e489f4c51ee80e2a0584ce63e3ef1f8e |
|
BLAKE2b-256 | 020a076c2afe740edbdbf7e52871360a458ae0cfa04503d809e0df56a57ebfb1 |
File details
Details for the file streamlit_project_template-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: streamlit_project_template-0.1.2-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca3c7c278c3517b4007efb8dc47e32969aaf87f0377ae486977b662f04875bc6 |
|
MD5 | bc8af794a924ebf1a79ab28fa2dde868 |
|
BLAKE2b-256 | 6e5d184b23d844dd57110f13fa00cce086e2f2f6470433c5e2a0fa22aab52940 |