Getting started with Streamlit

Udbhav Pangotra
Geek Culture
Published in
3 min readSep 17, 2021

Use Streamlit to explain your EDA and Machine Learning models

Photo by XPS on Unsplash

Streamlit is a web application framework, that helps us build and develop Python-based web applications that can be used to share analytics results, build complex interactive experiences, and illustrate new machine learning models.
In a nutshell, think of it as an interactable interface for all your machine learning tasks, from providing a business case overview, to an EDA to explaining the model’s learning and impact!

Now, one of the things that I love about Streamlit is that developing on the framework is a fast, flexible, and fun process. You can see your changes in real-time, debug your code, find support and guides for whatever you need!

Data science is generally considered a modern-day dream job and it is challenging! One of the most challenging aspects of data science is considered to being able to explain what you did, how you did it, and what impact can it make. This very important communication is tough usually because of two things, complex systems are tough to explain, and sometimes the results are needed to be explained in a more simple and well-crafted interactive way, not your usual PowerPoint.

Think of this, the business owner asking you what would be the impact if they launch a new offer or change something in the campaign process and want to see the impact, well a static word or PowerPoint can’t solve your problem. This is where a web application that can take user input works the best. Now, why Streamlit when we have Flask or Django and we can deploy the whole solution over GCP or AWS? None of these options work that well. Many are slow, don’t take user input, or are suboptimal for informing the decision-making process so fundamental to data science. Now, this is where Streamlit comes in, a framework that is meant for high-speed performance and interaction.

But enough talk about the framework, let’s use it! We’ll follow certain simple steps and I will be providing code for the application we will make.

Installing Streamlit

To run any Streamlit apps, you must first install Streamlit. I’ve used a package manager called pip to do this, but you can install it using any package manager you choose (for example, brew).

pip install streamlit

Your project organization

I think one of the most important things about learning a new topic and creating multiple projects/apps is to make sure you have a good structure to your code. I would suggest to create all apps and projects you make in their own folders.

mkdir streamlit_apps
cd streamlit_apps
mkdir plotting_demo_app

We start by importing all the dependencies we will need throughout the program. The primary libraries we will use will be Yfinance and Streamlit. Yfinance will allow us to receive historical stock prices for any ticker, Streamlit will allow us to deploy the web app onto the localhost.

Now that we have the dependencies set up, we can finally get into the building of our app!

First, in order to run the program, head over to your terminal and type in the command

streamlit run stock_monitor.py.

Replace file_name.py with the file name you choose to create from this program. A web app should then open up on your localhost!

Now open the localhost on your browser and you can see the output!

I’ve included the entire code for this program in the GitHub Gist above as you can see, do try it at your end, should be an easy way to get the ball rolling, stay tuned for some more interactive dashboards!

You can follow me or reach out to me at
udbhavpangotra@protonmail.com
udbhavpangotra (Udbhav Pangotra) (github.com)
Udbhav Pangotra | LinkedIn

--

--