Start-off with Streamlit(Beginner’s Approach)

Gaurav Rajesh Sahani
Analytics Vidhya
Published in
4 min readAug 10, 2020

--

Streamlit is an open-source app framework that is the easiest way for data scientists and machine learning engineers to create beautiful, performant apps in only a few hours!

By the end of this blog, you are going to be comfortable with using Streamlit to build a basic streamlit web-application.

Let me introduce you all, with the dataset, I would be dealing with, to demonstrate building a basic web-application on streamlit.

Dataset considered, is Iris dataset, extracted from Kaggle, It includes three iris species with 50 samples each as well as some properties about each flower. One flower species is linearly separable from the other two, but the other two are not linearly separable from each other. The link for this dataset is given below.

I will be training this dataset, using a simple logistic regression technique( not giving much emphasis on training part), and saving the model as a Pickle file. Please refer to the GitHub link given at the end of the Blog, for the Code.

Before starting off with building streamlit application, Please make sure, you have installed streamlit, if not, Please enter the following command in your respective terminal.

$ pip install streamlit

Now, Let’s get started!

We’ll start calling the important libraries, which would be used to build your first streamlit application, as well as importing our Iris classification model, saved previously as (.pkl) file in a variable named “model”, using pickle.

import streamlit as st
import pandas as pd
import pickle
#from PIL import Image
model = pickle.load(open('IRIS-model.pkl', 'rb'))

Importing PIL Library is optional since it's used to import images in our web-application, but recommended to import so, just because it makes our application more presentable.

Now, we’ll give a header for our application, and import an Image, making our application look better:)

st.header("Iris Classification:")image = Image.open('image.png')
st.image(image, use_column_width=True,format='PNG')
st.write("Please insert values, to get Iris class prediction")

Well, coming to the important part, its time to take feature values as input from our Users, and based on those inputs, our Iris Classification model would predict the class of the Iris flower respectively, Streamlit provides us wonderful options to accept the inputs in form of text-boxes, as well as Slider options, here, We would go with Slider options, which makes easier for users to insert values.

We’ll accept each feature values such as Sepal length and width, and Petal length and width respectively, using slider option defining the range between the user can select a value, and store these values, in a variable named “data”, shown as,

SepalLengthCm = st.slider('SepalLengthCm:', 2.0, 6.0)
SepalWidthCm = st.slider('SepalWidthCm:', 0.0, 5.0)
PetalLengthCm = st.slider('PetalLengthCm',0.0, 3.0)
PetalWidthCm = st.slider('PetalWidthCm:', 0.0, 2.0)
data = {'SepalLengthCm': SepalLengthCm,
'SepalWidthCm': SepalWidthCm,
'PetalLengthCm': PetalLengthCm,
'PetalWidthCm': PetalWidthCm}

The data, we collected, needs to be converted into a DataFrame, before using this for prediction, which can be done as follows,

features = pd.DataFrame(data, index=[0])

Now, its time to provide these inputs, to our classifier model, which will finally predict the class of the Iris flower, as an Ouput!

pred_proba = model.predict_proba(features)
#or
prediction = model.predict(features)

Here, “predict_proba” command, will basically give the prediction, in form of probability percentages of each class predicted by our model, or you can just use “predict”, to simply predict the class.

We are done model predictions, except presenting the results to our Users, which can be done by,

st.subheader('Prediction Percentages:') st.write('**Probablity of Iris Class being Iris-setosa is ( in % )**:',pred_proba[0][0]*100)st.write('**Probablity of Isis Class being Iris-versicolor is ( in % )**:',pred_proba[0][1]*100)st.write('**Probablity of Isis Class being Iris-virginica ( in % )**:',pred_proba[0][2]*100)

Finally, we are done with coding your basic Streamlit Web-application,

Now perform the following steps, to open your Web-Application, which would render on your Local server,

  1. Open your terminal, and Activate the virtual environment(if any).
  2. Change the directory, in which you have saved the python file of your Web-application.
  3. Execute the command, “streamlit run app.py” to render your application on your local server!
$ activate YourVirtualenvironment$ cd Your/saved/file$ streamlit run app.py

Congratulations!, you have created your own Streamlit application!, and Probably your application would look like this,

Additionally, would suggest reading the official documentation of streamlit, and experiment new things, in order to explore many more beautiful features offered by Streamlit!

GitHub Link for the application:

Please leave a Upvote!, if you really like the explanation, as well as genuine comments to improve upon!, this motivates me to come up with more such blogs:)

Connect with me on Linkedin:

Thank you so much!

--

--

Gaurav Rajesh Sahani
Analytics Vidhya

Machine Learning, Deep Learning, and Cloud Enthusiast. Like to explore new things, enhancing and expanding my knowledge each and every day!