Working on streamlit

Ponshriharini
featurepreneur
Published in
3 min readMar 7, 2022

Have you ever searched for a simple way to deploy your machine learning model? Well, streamlit is one of the best solutions to this problem.

Here, in this article, we’ll be seeing the basics on how to use streamlit before jumping to using streamlit with our ML model.

INSTALLATION:

First you’ll have to activate your environment and install streamlit using the following command

pip install streamlit

Now, use the following command in your terminal.

streamlit run <filename.py>

This will start streamlit in your local host. You can navigate to your local host to check it once.

GETTING STARTED:

Import the required modules

import streamlit as stimport pandas as pdimport matplotlib.pyplot as pltfrom sklearn.datasets import load_bostonimport warningsimport numpy as np

Now, we’ll create a dataframe using randome numbers and plot them using line and are graph.

data = np.random.randn(25,2)df = pd.DataFrame(data, columns=["Variation A", "Variation B"])st.line_chart(df)st.area_chart(df)

Now, run your streamlit and go to your localhost. You should be able to see something like this

Plotting is really important when it comes to machine learning. Be it to remove the outliers or for some other process, plotting is necessary. Now, visualising them using streamlit made the process a lot more easier as we are able to see the plot in the deployment as well — it gives a better understanding of the model. You can also zoom in or zoom out of this graph.

Now, we’ll try getting the boston dataset and finding its shape.

with warnings.catch_warnings():warnings.filterwarnings("ignore")X, y = load_boston(return_X_y=True)st.header("Shape of the dataset")st.code(X.shape)

We can also view the dataset in streamlit using the below command

st.dataframe(X)

And in case your ML model requires an image to be uploaded in order to process it, we can use the file uploader

uploaded_file = st.file_uploader("Choose an image file", type="jpg")

Lastly, we’ll see a fun command to make your model a lot more fun, use the below command

st.balloons()

Therefore, streamlit can used to perform various tasks and it makes deploying your model so much more easier than it used to be.

Happy coding!

--

--