Simple Data Science Web App in Python using streamlit

Plaban Nayak
3 min readJun 18, 2020

--

Introduction

Streamlit is an open-source Python library that makes it easy to build beautiful custom web-apps for machine learning and data science.

Streamlit is an awesome new tool that allows engineers to quickly build highly interactive web applications around their data, machine learning models, and pretty much anything.

The best thing about Streamlit is it doesn’t require any knowledge of web development. If you know Python, you’re good to go!

Follow these steps and to get a sample app running in less than 5 minutes.

  1. You need to have Python 3.6 or greater installed
  2. Install streamlit
pip install streamlit

Getting Started — Building a Glass Identification Web App

dataset source :-https://archive.ics.uci.edu/ml/datasets/Glass+Identification

Attribute Information

  1. Id number: 1 to 214
    2.
    RI: refractive index
    3.
    Na: Sodium (unit measurement: weight percent in corresponding oxide, as are attributes 4–10)
    4.
    Mg: Magnesium
    5.
    Al: Aluminum
    6.
    Si: Silicon
    7.
    K: Potassium
    8.
    Ca: Calcium
    9.
    Ba: Barium
    10.
    Fe: Iron
    11.
    Type of glass: (class attribute)
    — building_windows_float_processed
    — building_windows_non_float_processed
    — vehicle_windows_float_processed
    — vehicle_windows_non_float_processed (none in this database)
    — containers
    — tableware
    — headlamps

Getting Started on building the webapp

import pandas as pd
import numpy as np
import streamlit as st
from sklearn.ensemble import RandomForestClassifier
import pickle

Add Text Data

st.title is suitable for the main title. We can use specific text functions to add content to your app, or you can use st.write() and add our own markdown.

st.write("""
# Simple Glass Identification Prediction App
This app predicts the **Type of Glass** type !
""")

For section titles, use st.header or st.subheader.

st.sidebar.header('User Input Parameters')

Sample Code for the webapp:

st.write("""
# Simple Glass Identification Prediction App
This app predicts the **Type of Glass** type !
""")
st.sidebar.header('User Input Parameters')
columns = ['Id','RI','Na','Mg','Ai','Si','K','Ca','Ba','Fe','Label']
def user_input_features():
RI = st.sidebar.slider('Refractive Index',1.51,1.53,1.51)
Na = st.sidebar.slider('Sodium',2.0,4.4,3.4)
Mg = st.sidebar.slider('Magnesium',10.0,17.6,10.730)
Ai = st.sidebar.slider('Aluminium',2.0,4.9,2.115000)
Si = st.sidebar.slider('Sillicon',72.0,75.0,72.280)
K = st.sidebar.slider('Potassium',0.12,6.2,0.122500)
Ca = st.sidebar.slider('Calcium',8.24,16.19,8.24)
Ba = st.sidebar.slider('Barium',0.0,3.15,0.0)
Fe = st.sidebar.slider('Iron',0.0,0.51,0.2)
data = {'RI': RI,
'Na': Na,
'Mg':Mg,
'Ai':Ai,
'Si':Si,
'K':K,
'Ca':Ca,
'Ba':Ba,
'Fe':Fe
}
features = pd.DataFrame(data,index=[0])
return features
df = user_input_features()
st.subheader('User Input Parameters')
st.write(df)
test = df.copy()#load the model
filename = 'rf.pkl'
loaded_model = pickle.load(open(filename, 'rb'))
prediction = loaded_model.predict(test)
prediction_proba = loaded_model.predict_proba(test)
st.subheader('Class Labels and their corresponding index number')label_name = np.array(['building_windows_float_processed',
'building_windows_non_float_processed','vehicle_windows_float_',
'vehicle_windows_non_float_processed','containers',
'tableware','headlamps'])
st.write(label_name)
# labels -dictionarynames ={1:'building_windows_float_processed',
2: 'building_windows_non_float_processed',
3: 'vehicle_windows_float_processed',
4: 'vehicle_windows_non_float_processed',
5:'containers',
6: 'tableware',
7:'headlamps'}
st.subheader('Prediction')st.write(names[prediction[0]])st.subheader('Prediction Probability')
st.write(prediction_proba)

To run your Streamlit app:

streamlit run Glass_Webapp.py
Snapshot of Streamlit Webapp

Note : This is an short attempt to build a simple web app using streamlit

Connect with me

--

--

Plaban Nayak
Plaban Nayak

Written by Plaban Nayak

Machine Learning and Deep Learning enthusiast

No responses yet