In this article, I am going to write about streamlit, give a quick overview and also build a simple web application. It’s not going to be extremely complex or feature-rich by any stretch of the imagination, but it will give you the skills you need to get started building your web apps in an incredibly small amount of time using streamlit.
What is Streamlit?
Streamlit is a web application framework for data science and machine learning. It’s built on top of Flask, which is the micro-framework written in Python and used by several websites. Streamlit provides many powerful tools that make it easy to build your apps with Python, including:
- A high-level API for building interactive visualizations based on pandas DataFrame objects
- A web app template to help you get started quickly
- An interactive dashboard UI powered by D3 charts
With This Powerful Tool Now You Can Build Data Science Apps Quickly
So what does Streamlit do? Well, it helps you build data science apps super quickly. Streamlit is so powerful that it also allows you to build data science apps for the web and even other kinds of data science projects. You can even take your existing web app and make it into a great data science app with Streamlit.
How to install Streamlit?
To install Streamlit, you have several options:
- Pip
- Anaconda
- Docker/Docker-compose
- Conda
Using Streamlit
To get started, you’ll have to install streamlit. You can install it with the following line of code:
pip install streamlit
To check and test use the following:
streamlit hello
You can read more about the prerequisites and installation process on their official site.
Let’s build a simple Machine Learning WebApp
With streamlit, we can build a web app to predict the type of Iris flower by using the Iris Dataset. You can use any text editor of your choosing. I will be using PyCharm for this project.
First, you’ll want to make sure that your project has imported the Streamlit library. If it hasn’t, you can do so by adding this line of code:
import streamlit as st
Also, let’s import the rest of the libraries that will be needed for this project.
import pandas as pd
from sklearn import datasets
from sklearn.ensemble import RandomForestClassifier
Next, we’ll give our web app the main heading. It can be done with the following:
st.write("""A Simple Machine Learning Web Application""")
In the next step, we’ll create a sidebar that will have all the features which the user can adjust. For that, we’ll need to use the following function.
st.sidebar.header('Input Parameters')
def user_input_features():
sepal_length = st.sidebar.slider('Sepal length', 4.3, 7.9, 5.4)
sepal_width = st.sidebar.slider('Sepal width', 2.0, 4.4, 3.4)
petal_length = st.sidebar.slider('Petal length', 1.0, 6.9, 1.3)
petal_width = st.sidebar.slider('Petal width', 0.1, 2.5, 0.2)
data = {'sepal_length': sepal_length, 'sepal_width': sepal_width, 'petal_length': petal_length, 'petal_width': petal_width}
features = pd.DataFrame(data, index=[0])
return features
To end, we’ll initialize and load the iris dataset and divide the dataset into data, and target for training and testing. We’ll also initialize and use the classifier and fit it onto our training and testing dataset. Finally, we will also add a few subheaders, and also initialize prediction.
df = user_input_features()
st.subheader('User Input parameters')
st.write(df)
iris = datasets.load_iris()
X = iris.data
Y = iris.target
clf = RandomForestClassifier()
clf.fit(X, Y)prediction = clf.predict(df)
prediction_proba = clf.predict_proba(df)
st.subheader('Labels of Class and their respective index number')
st.write(iris.target_names)
st.subheader('Prediction')
st.write(iris.target_names[prediction])
st.subheader('Prediction Probability')
st.write(prediction_proba)
Once you run the code, you’ll get a warning in the output:
“to view this Streamlit app on a browser, run it with the following command.”
All you have to do is copy the line and paste into the cmd and run it. Your application will open in a web browser for you to use.
Congratulations! You just built your first web application with streamlit. To view more applications or to get an idea of how other users are using streamlit, you can go here to their official site to check out their projects. Also, I will be uploading more projects streamlit in the near future.
Conclusion
You know what they say, “There’s an app for that.” But now there could be an app for your data science projects. With Streamlit, you can make a pretty, interactive web app in minutes — so long as you have some Python skills, of course.
The best part is that the library is free and open source. So, if you want to create something that other people can access and maybe even contribute to themselves — your options are endless. Don’t let this get into your head though because it’s still being developed by someone who knows Python well enough to use it effectively; this means there is no need for you to be overwhelmed when trying out all these new features. I hope you enjoyed this article. I will be posting more Streamlit projects soon.
Article originally published on Medium by Author: https://medium.com/towardsdev/build-ml-web-apps-super-quick-with-streamlit-6fdb8deedabc
About the Creator
Hassan
I'm a data scientist by day and a writer by night, so you'll often find me writing about Analytics. But lately, I've been branching into other topics. I hope you enjoy reading my articles as much as I enjoy writing them.
Comments
There are no comments for this story
Be the first to respond and start the conversation.