Creating Multipage applications using Streamlit (efficiently!) | by Prakhar Rathi | Towards Data Science

How often have you had a great ML project idea that you think you would be able to show-off on your resume? You quickly collect data, analyze it and build models on it; all inside a Jupyter Notebook and then you face your worst nightmare— somebody asks you to deploy the model and show inferencing with a user-friendly interface. If you’re working alone, this can be really intimidating at times and why wouldn’t it be? You’re a data scientist and not a web developer! That’s where Streamlit comes in. It’s an awesome tool that quickly lets you build interactive web applications and showcase your models with graphics that could put many developers to shame.
The best thing about Streamlit is that you don’t even need to know the basics of web development to get started or to even create your first web application. So if you’re somebody who’s into Data Science but reluctant to deploy your models because you don’t like web development, then this is your chance to build beautiful data apps using just Python!
There are many resources out there which can teach you how to get started with Streamlit. My personal favorite is this one. However, once you get started you will soon realize that building just one or two pages is not enough and a good application will have many more pages and that is where the need to build multi-page applications arises. So, I decided to write about it. In this article, I will walk you through building your first Multipage Streamlit application and deploying it. The prerequisite is knowing the basics of Python and Streamlit.
Edit: The Streamlit Community members were kind enough to let me showcase my app on the Streamlit Sharing Platform so I have deployed it there. Anyone can test it in real-time now. You can find the app here.
In this section, I will walk you through a project I made where I implemented the multipage functionality of Streamlit. Now, this wasn’t done using an if-else method where I keep checking which entry of the streamlit.selectbox() the user has picked and display pages accordingly. That method sort of looks something like this.
import streamlit as st# Create a page dropdown page = st.selectbox("Choose your page", ["Page 1", "Page 2", "Page 3"]) if page == "Page 1":    # Display details of page 1elif page == "Page 2":    # Display details of page 2elif page == "Page 3":    # Display details of page 3
While the above method is not incorrect, I find it to be extremely annoying because I have to manage so many if-else’s and the code inside them. As programmers, we should be able to write neater applications and use object-oriented programming, wherever required. That’s what I am here to do!
The application that I made was called ‘DataSpeare’ (bad wordplay, I know) It is an AI based tool that automates the process of data analysis and machine learning for non-technical users. It can take a data set, identify patterns in the data, interpret the result, and then produce an output story that is understandable to a business user based on the context of the problem. The features of the application are as follows:-
turn the data into interactive data stories based on the given data
generate deep insights, infer pattern and help in business decisions.
provide personalization profiles; these could be represented as meta data describing what would be of interest to a given user.
generate reports understandable to a business user with interactive and intuitive interface.
You can read more about the application here. If you want to get the source code of the project then you can go to the GitHub repository. In this article, I will not go over the whole application because it has a large code base but I will go over the basic structure.
First things first, you need to get Streamlit on to your device. Personally, I would recommend using a virtual environment but this step is optional.
pip install streamlit
For my application, there are a bunch of other requirements which you can download if you are building the whole thing. Now, the most important part is to build the structure of the application. I am using a class based structure which will let you add pages as an instance of the MultiPageclass in your application. This has been created in the file named multipage.py in my project repo.
It’s a simple class which has two methods:-
add() : It is used to add more pages to your application. Each new page is appended on to the instance variable pages.
run(): This method creates a dropdown in the sidebar of your application so that you can select from all the options that you need.
It only requires you to import streamlit and you’re good to go!
This was the Class that handles the structure of your application but you actually need file to actually add the pages in and manage the code. This has been done in the app.py file.
This is the main file in our Streamlit application which will run to load all the pages. In line 8, I have created an instance appof the MultiPage class which I have imported in line 4. The app instance lets me add pages to the application as well as run the application. The pages have been added from line 14 onwards and the users can add their pages in a similar manner. Finally, in line 21 I ran the application using the run function.
Note:- People using this structure only have to change the imports in line 5 and the pages which are added from line 14 and the rest of the code will remain the same.
Here’s a snippet of how your app navigation will look like depending on which pages you have added to your application.
Finally, you can create a page for your application in the manner given below. I am only showing one page and the rest of them can be seen here. I created a new directory called pages and put all the pages files inside it and you can do the same.
The page that I have shown below is called data_upload.py. In this page, I upload the dataset and make a local copy of it. This is useful in transporting data between pages. There is an alternate way to do this using Session variables but I wouldn’t recommend it for beginners (Maybe I will write another article on it). The uploaded dataset is stored in a csv format in the data folder and is read by the other pages. Since, this is an automated data analysis application, it also generates a column data for each column in the updated dataset. That is done in line 44 using the genMetaData() function defined in the utils.py file. You can create other pages in a similar fashion and build your application.
Note:- It is important that you write your code inside the app() function because that is being called in the structure of the multipage application.
Once all the pages have been made, imported and added to your app, it’s time to run your app using the following command in your command prompt.
streamlit run app.py
Once you run the application, your cmd should look like this and once you redirect to the local URL, you will see your application.
You can also build multipage applications inside multipage applications using this method and by creating a new instance of your inside the page that you define.
There are amazing things that you can do with Streamlit and it’s other components like session variables. I might write another article on Session States and Variables but I wanted to keep this one short and informative. I hope you liked it and this was helpful. Best of luck!