Python
Streamlit
#PythonNotes
Streamlit
What is Streamlit?
▪ Streamlit is an open-source Python library that makes it easy to create and
share beautiful, custom web apps for machine learning and data science.
▪ Streamlit is very user-friendly.
Akash Technolabs [Link]
3
Why one should use Streamlit?
▪ Streamlit is the easiest way especially for people with no front-end knowledge
to put their code into a web application:
▪ No front-end (html, js, css) experience or knowledge is required.
▪ You don't need to spend days or months to create a web app, you can create
a really beautiful machine learning or data science app in only a few hours
or even minutes.
▪ It is compatible with the majority of Python libraries (e.g. pandas,
matplotlib, seaborn).
▪ Less code is needed to create amazing web apps.
Akash Technolabs [Link]
4
Setting Up Your Environment
▪ First, you need to install Streamlit. It's as simple as running this command in
your Python environment:
pip install streamlit
Akash Technolabs [Link]
5
How to run Streamlit file?
▪ Open command prompt or Any type of terminal
streamlit run [Link]
Akash Technolabs [Link]
6
Streamlit basic functions
▪ Title:
# import module
import streamlit as st
# Title
[Link]("Hello World !!!")
Akash Technolabs [Link]
7
2. Header and Subheader:
# import module
import streamlit as st
# Title
[Link]("Hello World !!!")
# Header
[Link]("This is a header")
# Subheader
[Link]("This is a subheader")
Akash Technolabs [Link]
8
3. Text:
# import module
import streamlit as st
# Text
[Link]("Welcome to Akash Technolabs!!!")
Akash Technolabs [Link]
9
4. Success, Info, Warning, Error, Exception:
# import module
import streamlit as st
# success
[Link]("Success")
# success
[Link]("Information")
# success
[Link]("Warning")
# success
[Link]("Error")
# Exception - This has been added later
exp = ZeroDivisionError("Trying to divide by Zero")
[Link](exp)
Akash Technolabs [Link]
10
5. Write:
▪ Using write function, we can also display code in coding format. This is not
possible using [Link](”).
# import module
import streamlit as st
# Write text
[Link]("Text with write")
# Writing python inbuilt function range()
[Link](range(10))
Akash Technolabs [Link]
11
6. Display Images:
# import module
import streamlit as st
# Display Images
# import Image from pillow to open images
from PIL import Image
img = [Link]("[Link]")
# display image using streamlit
# width is used to set the width of an image
[Link](img, width=200)
Akash Technolabs [Link]
12
7. Checkbox:
▪ A checkbox returns a boolean value. When the box is checked, it returns a
True value else returns a False value.
# import module
import streamlit as st
# checkbox
# check if the checkbox is checked
# title of the checkbox is 'Show/Hide'
if [Link]("Show/Hide"):
# display the text if the checkbox returns True value
[Link]("Showing the widget")
Akash Technolabs [Link]
13
8. Radio Button:
# import module
import streamlit as st
# radio button
# first argument is the title of the radio button
# second argument is the options for the radio button
status = [Link]("Select Gender: ", ('Male', 'Female'))
# conditional statement to print
# Male if male is selected else print female
# show the result using the success function
if (status == 'Male'):
[Link]("Male")
else:
[Link]("Female")
Akash Technolabs [Link]
14
9. Selection Box:
▪ You can select any one option from the select box.
# import module
import streamlit as st
# Selection box
# first argument takes the title of the selection box
# second argument takes options
hobby = [Link]("Hobbies: ",
['Dancing', 'Reading', 'Sports'])
# print the selected hobby
[Link]("Your hobby is: ", hobby)
Akash Technolabs [Link]
15
10. Multi-Selectbox:
▪ The multi-select box returns the output in the form of a list. You can select
multiple options.
# import module
import streamlit as st
# multi select box
# first argument takes the box title
# second argument takes the options to show
hobbies = [Link]("Hobbies: ",
['Dancing', 'Reading', 'Sports'])
# write the selected options
[Link]("You selected", len(hobbies), 'hobbies')
Akash Technolabs [Link]
16
11. Button:
▪ [Link]() returns a boolean value. It returns a True value when clicked else
returns False.
# import module
import streamlit as st
# Create a simple button that does nothing
[Link]("Click me for no reason")
# Create a button, that when clicked, shows a text
if([Link]("About")):
[Link]("Welcome To Akash Technolabs...")
Akash Technolabs [Link]
17
12. Text Input:
# import module
import streamlit as st
# Text Input
# save the input text in the variable 'name'
# first argument shows the title of the text input box
# second argument displays a default text inside the text input area
name = st.text_input("Enter Your name", "Type Here ...")
# display the name when the submit button is clicked
# .title() is used to get the input text string
if([Link]('Submit')):
result = [Link]()
[Link](result)
Akash Technolabs [Link]
18
Akash Technolabs [Link]
19
Akash Technolabs [Link]
20
13. Slider:
# import module
import streamlit as st
# slider
# first argument takes the title of the slider
# second argument takes the starting of the slider
# last argument takes the end number
level = [Link]("Select the level", 1, 5)
# print the level
# format() is used to print value
# of a variable at a specific position
[Link]('Selected: {}'.format(level))
Akash Technolabs [Link]
21
[Link]/Audio/Video
# import module
import streamlit as st
# Image
[Link]("[Link]")
#Audio
[Link]("[Link]")
#Video
[Link]("Video.mp4")
Akash Technolabs [Link]
22
15:Input widgets
▪ st.number_input(): This function is used to display a numeric input widget.
▪ st.text_input(): This function is used to display a text input widget.
▪ st.date_input(): This function is used to display a date input widget to choose a date.
▪ st.time_input(): This function is used to display a time input widget to choose a
time.
▪ st.text_area(): This function is used to display a text input widget with more than a
line text.
▪ st.file_uploader(): This function is used to display a file uploader widget.
▪ st.color_picker(): This function is used to display color picker widget to choose a
color.
Akash Technolabs [Link]
23
Example
# import module
import streamlit as st
st.number_input('Pick a number', 0,10)
st.text_input('Email address')
st.date_input('Date of Birth')
st.time_input('School time')
st.text_area('Description')
st.file_uploader('Upload a photo')
st.color_picker('Choose your favorite color')
Akash Technolabs [Link]
24
16:Display progress and status with Streamlit
▪ [Link](): This function is used to display balloons for celebration.
▪ [Link](): This function is used to display a progress bar.
▪ [Link](): This function is used to display a temporary waiting message
during execution.
Akash Technolabs [Link]
25
Example
# import module
import streamlit as st
import time
[Link]()
[Link]("Progress Bar")
[Link](10)
[Link]("Wait the execution")
with [Link]('Wait for it...'):
[Link](10)
Akash Technolabs [Link]
26
Sum of 2 Numbers
Akash Technolabs [Link]
27
Code
# import the streamlit library
import streamlit as st
# give a title to our app
[Link]('Sum Of 2 Numbers')
# TAKE INPUT
n1 = st.number_input("Enter n1")
n2 = st.number_input("Enter n2")
ans = n1 + n2
# check if the button is pressed or not
if ([Link]('Calculate Sum')):
# print the Sum
[Link]("Sum is {}.".format(ans))
Akash Technolabs [Link]
28
Mini Caculator
Akash Technolabs [Link]
29
Code
# import the streamlit library
import streamlit as st
# give a title to our app
[Link]('Mini Calculator')
# TAKE INPUT
n1 = st.number_input("Enter n1")
n2 = st.number_input("Enter n2")
col1, col2, col3, col4 = [Link](4)
with col1:
Sum = [Link]('Sum')
with col2:
Sub = [Link]('Sub')
with col3:
Mul = [Link]('Mul')
with col4:
Div = [Link]('Div')
if Sum:
ans = n1 + n2
[Link]("Sum is {}.".format(ans))
if Sub:
ans = n1 - n2
[Link]("Subtraction is {}.".format(ans))
if Mul:
ans = n1 * n2
[Link]("Multiplication is {}.".format(ans))
if Div:
ans = n1 / n2
[Link]("Division is {}.".format(ans))
Akash Technolabs [Link]
30
Mini Calculator Using Radio Button
Akash Technolabs [Link]
31
Code
# import the streamlit library
import streamlit as st
# give a title to our app
[Link]('Mini Calculator')
# TAKE INPUT
n1 = st.number_input("Enter n1")
n2 = st.number_input("Enter n2")
action = [Link]('Select your action: ',
('Sum', 'Sub', 'Mul', 'Div'), horizontal=True)
if (action == 'Sum'):
ans = n1 + n2
[Link]("Sum is {}.".format(ans))
if (action == 'Sub'):
ans = n1 - n2
[Link]("Subtraction is {}.".format(ans))
if (action == 'Mul'):
ans = n1 * n2
[Link]("Multiplication is {}.".format(ans))
if (action == 'Div'):
ans = n1 / n2
[Link]("Division is {}.".format(ans))
Akash Technolabs [Link]
32
Sample Example
▪ [Link]
▪ [Link]
Akash Technolabs [Link]
33
Akash Technolabs [Link]
34
Akash Technolabs [Link]
35
Akash Technolabs [Link]
36
Akash Technolabs [Link]
37
Akash Technolabs [Link]
38
Akash Technolabs [Link]
39
Akash Technolabs [Link]
40
Akash Technolabs [Link]
41
Akash Technolabs [Link]
42
Let's Connect SOCIAL MEDIA
#DailyUpdates #FreeCode
@akashsirdotcom @akashpadhiyar
#TechUpdates #FreeTutorials
@akashpadhiyar @akashtechnolabs
Akash Padhiyar #HelpDesk
Tech Educator • Consultant Tech
Mentor • Founder +91 99786 21654
On a Mission to Bridge the
Gap Between Campus & Corporate WEBSITES
[Link] [Link]
akash@[Link]
202, Aarya Arcade, Mithakhali Six Road, Navrangpura, Ahmedabad, Gujarat 380009