Developing Data Web App Using Streamlit

Karan Jagota 13 Apr, 2021
6 min read
This article was published as a part of the Data Science Blogathon.

Table of Contents

  • Introduction
  • About streamlit
  • Prerequisite, Set up, and Installation
  • Developing your first streamlit app
  • Building blocks of streamlit
  • Advanced Features
  • Conclusion


Data Science is one of the most trending search topics on google these days. Because of its high demand and exhaustive usage in real-world applications, more and more developers in the community are developing new frameworks and libraries that help data scientists and researchers complete everyday tasks. One such library is streamlit. With streamlit, one can build data apps in no time. It seamlessly integrates with other python libraries like NumPy, Pandas, Matplotlib, and many more.


About Streamlit

Streamlit is an open-source python library that is useful to create and share data web apps. It is slowly gaining a lot of momentum in the data science community. Because of the ease with which one can develop a data science web app, many developers use it in their daily workflow. The streamlit GitHub repository has more than 14.1k stars and 1.2k forks. Under the hood, it uses React as a frontend framework to render the data on the screen. So, React Developers can easily manipulate the UI with few changes in the code.


Prerequisite, Setup, and Installation 

To follow the rest of the post, please make sure you are familiarized with Python and meet the following requirements in your local dev environment.

  • Python version >= 3.XXX (3.7 recommended)
  • pip
  • virtualenv
  • Pycharm or any other Code Editor


Create a virtual environment using virtualenv in any directory of your choice. You can skip the below step if you are using pycharm IDE or you already know how to set it up for your project.

virtualenv env
source ./env/bin/activate

Create a requirement.txt file by typing the following command in the terminal

pip freeze > requirements.txt

Create a new python file in the same directory.


To install streamlit in your python application, write the command below in the terminal-

pip install streamlit

Developing your first Streamlit app

Open the file and copy the below code –

# import statements
import streamlit as st
import numpy as np
import pandas as pd
# adding title and text in the app
st.title('Hello World')
st.write("Welcome to your first streamlit application")

Run the app by typing the following command in terminal –

streamlit run

your output should look similar to the screenshot below-

streamlit app hello world

Building Blocks of Streamlit

The complete code and data files can be found on my GitHub repository. 

Link: Medium-Blogs/streamlitBlog at master · karanjagota/Medium-Blogs (

In a nutshell, we develop a streamlit app in the same way as we write any python script. Streamlit provides widgets for user interaction and easy-to-use functions to render data on the screen. In this section, for each question asked, we will implement a suitable answer code –

How to display data on the screen?

Data in any Data Science web app traditionally can be a data frame, texts, charts, code, or media. Let’s look at the code to implement all the above in our app.

Open the file and add the below code –

Code (Github Gist): (

import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from PIL import Image
import requests
#Title and Subheader
st.title("Life Expectancy By Country")
st.write("Life Expectancy Data of India and USA from the year 1960 till 2016.")
# Multicolumn Support
col1, col2 = st.beta_columns(2)
IN_flag ="c:UsersHPDesktopdataappIN.jpg")
col1.image(IN_flag, use_column_width=True)
US_flag ="c:UsersHPDesktopdataappUS.png")
col2.image(US_flag, use_column_width=True)
# reading a csv and displaying the first six rows on the screen.
df = pd.read_csv('lf.csv')
# Display full data base on checkbox.
if st.checkbox('show full data'):
# Display Code
st.write('Displaying Code in Streamlit app')
with st.echo():
    # square function
    def square(x):
    # cube function
    def cube(x):
    square(5) #output 25
    cube(5) # output 125


streamlit app output

Streamlit provides easy-to-use functions to write data on the screen. We use the title and write method from the streamlit library to display it in the app. You can also see in the above code that we first load the dataset in a data frame from the CSV file using the Pandas package, then render the first six rows of the data frame onto the screen. We also display the Image using the pillow library. The echo command of the streamlit library provides us with a way to add code to our app.


How to add Interactive Widgets on the screen?

Widgets allow you to add interactivity to your Data web app. Streamlit provides buttons, checkboxes, input texts, sidebars, and many more as widgets that let users interact with the data through UI directly without implementing code. Let’s look at each in the below code. Add the below code in the file.

Code (Github gist): (

# Display barplot and data on search Input
_input = st.text_input("Enter Year to Search")
if _input:
    _df = df[df['year'] == int(_input)]
    if len(_df['year'])>0:
    if len(_df['year'])>0:
        fig, ax = plt.subplots()
        fig = plt.figure(figsize=(7, 4))
        ax = sns.barplot(data=_df, y="Lf-overall", x="year", hue="country", palette="tab20_r")
        st.write('Data Does Not Exist in the Database')


streamlit app 2016

With the input text widget, we have created a search functionality within the Data Frame through which we can calculate and plot the graph of the Life Expectancy of India and the USA.

Streamlit also provides a way to add information and widgets on the sidebar, just like many modern-day web apps. Let’s add some more interactive widgets to the app on the sidebar. Add the below code in the file.

Code (Github gist): (

# Sidebar Column
st.sidebar.title('Sidebar Widgets')
#radio button 
rating ='Are You Happy with the Example',('Yes','No','Not Sure'))
if rating == 'Yes':
    st.sidebar.success('Thank You for Selecting Yes')
elif rating =='No':'Thank You for Selecting No')
elif rating =='Not Sure':'Thank You for Selecting Not sure')
rating = st.sidebar.selectbox("How much would you rate this App? ",
                     ['5 Stars', '4 Stars', '3 Stars','2 Stars','1 Star'])
st.sidebar.write('Find Square of a Number')
get_number = st.sidebar.slider("Select a Number", 1, 10)
st.sidebar.write('Square of Number',get_number, 'is', get_number*get_number)


life expectancy

How to display API data on the screen?

With streamlit, it is also possible to fetch and display data from the restful APIs. If one knows a little bit of react.js, one can even use it to build web apps. But the relevance and implementation of that topic are beyond the scope of this post.

In this part, we will create a URL shortener app that takes the URL from the user and fetches the response from the API and displays it to the screen. Add the code in the file to get the desired output.

Code (Github gist): (

# displaying API data
API_URL = ''
st.subheader('URL SHORTNER')
_url = st.text_input('Enter URL')
pressed = st.button('Get Short Link')
if pressed:
    if _url !='':
        data = {'url': _url}
        r =, data=data)
        st.write('Please enter the right URL first')

The code is pretty much self-explanatory. We use the requests package to make the post request to the ‘’. Once we get the short URL, we display it as JSON in the app.



Streamlit is one of the fastest ways to develop and deploy machine Learning powered data-driven web apps. In the above post, we learned the following things-

  • Building blocks of streamlit
  • Briefly discussed some of the most common methods and widgets provided by the library.
  • We also created a URL shortener feature using the free API

The media shown in this article are not owned by Analytics Vidhya and is used at the Author’s discretion. 

Karan Jagota 13 Apr, 2021

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers


RK 08 Jun, 2022

I have developed a streamlit application for AI/ML requriement. but i would like to deploy this application on to a linux server using NGINX i could not find any solid examples in the internet. It would be great if you can come up with detailed explanation regarding the same