This repo is a template for your semester project. It includes most of the infrastructure setup (containers) and sample code and data throughout. Explore it fully and ask questions.
- A GitHub Account
- A terminal-based or GUI git client
- VSCode with the Python Plugin
- A distrobution of Python running on your laptop (Choco (for Windows), brew (for Macs), miniconda, Anaconda, etc).
Currently, there are three major components which will each run in their own Docker Containers:
- Streamlit App in the
./app
directory - Flask REST api in the
./api
directory - SQL files for your data model and data base in the
./database-files
directory
If you are not familiar with web app development, this code base might be confusing. You will probably want two versions though:
- One version for you to explore, try things, break things, etc. We'll call this your Personal Repo
- One version of the repo that your team will share. We'll call this the Team Repo.
- In GitHub, click the fork button in the upper right corner of the repo screen.
- When prompted, give the new repo a unique name, perhaps including your last name and the word 'personal'.
- Once the fork has been created, clone YOUR forked version of the repo to your computer.
- Set up the
.env
file in theapi
folder based on the.env.template
file. - Start the docker containers.
Before you start: As a team, one person needs to assume the role of Team Project Repo Owner.
- The Team Project Repo Owner needs to fork this template repo into their own GitHub account and give the repo a name consistent with your project's name. If you're worried that the repo is public, don't. Every team is doing a different project.
- In the newly forked team repo, the Team Project Repo Owner should go to the Settings tab, choose Collaborators and Teams on the left-side panel. Add each of your team members to the repository with Write access.
- Each of the other team members will receive an invitation to join. Obviously accept the invite.
- Once that process is complete, each team member, including the repo owner, should clone the Team's Repo to their local machines (in a different location than your Personal Project Repo).
docker compose up -d
to start all the containers in the backgrounddocker compose down
to shutdown and delete the containersdocker compose up db -d
only start the database container (replace db with the other services as needed)docker compose stop
to "turn off" the containers but not delete them.
In most applications, when a user logs in, they assume a particular role. For instance, when one logs in to a stock price prediction app, they may be a single investor, a portfolio manager, or a corporate executive (of a publicly traded company). Each of those roles will likely present some similar features as well as some different features when compared to the other roles. So, how do you accomplish this in Streamlit? This is sometimes called Role-based Access Control, or RBAC for short.
The code in this project demonstrates how to implement a simple RBAC system in Streamlit but without actually using user authentication (usernames and passwords). The Streamlit pages from the original template repo are split up among 3 roles - Political Strategist, USAID Worker, and a System Administrator role (this is used for any sort of system tasks such as re-training ML model, etc.). It also demonstrates how to deploy an ML model.
Wrapping your head around this will take a little time and exploration of this code base. Some highlights are below.
- We need to turn off the standard panel of links on the left side of the Streamlit app. This is done through the
app/src/.streamlit/config.toml
file. So check that out. We are turning it off so we can control directly what links are shown. - Then I created a new python module in
app/src/modules/nav.py
. When you look at the file, you will se that there are functions for basically each page of the application. Thest.sidebar.page_link(...)
adds a single link to the sidebar. We have a separate function for each page so that we can organize the links/pages by role. - Next, check out the
app/src/Home.py
file. Notice that there are 3 buttons added to the page and when one is clicked, it redirects viast.switch_page(...)
to that Roles Home page inapp/src/pages
. But before the redirect, I set a few different variables in the Streamlitsession_state
object to track role, first name of the user, and that the user is now authenticated. - Notice near the top of
app/src/Home.py
and all other pages, there is a call toSideBarLinks(...)
from theapp/src/nav.py
module. This is the function that will use the role set insession_state
to determine what links to show the user in the sidebar. - The pages are organized by Role. Pages that start with a
0
are related to the Political Strategist role. Pages that start with a1
are related to the USAID worker role. And, pages that start with a2
are related to The System Administrator role.
Note: This project only contains the infrastructure for a hypothetical ML model.
- Build, train, and test your ML model in a Jupyter Notebook.
- Once you're happy with the model's performance, convert your Jupyter Notebook code for the ML model to a pure python script. You can include the
training
andtesting
functionality as well as theprediction
functionality. You may or may not need to include data cleaning, though. - Check out the
api/backend/ml_models
module. In this folder, I've put a sample (read fake) ML model inmodel01.py
. Thepredict
function will be called by the Flask REST API to perform 'real-time' prediction based on model parameter values that are stored in the database. Important: you would never want to hard code the model parameter weights directly in the prediction function. tl;dr - take some time to look over the code inmodel01.py
. - The prediction route for the REST API is in
api/backend/customers/customer_routes.py
. Basically, it accepts two URL parameters and passes them to theprediction
function in theml_models
module. Theprediction
route/function packages up the value(s) it receives from the model'spredict
function and send its back to Streamlit as JSON. - Back in streamlit, check out
app/src/pages/11_Prediction.py
. Here, I create two numeric input fields. When the button is pressed, it makes a request to the REST API URL/c/prediction/.../...
function and passes the values from the two inputs as URL parameters. It gets back the results from the route and displays them. Nothing fancy here.