Share
Streamlit is an open source web application framework in Python. It is helpful in creating machine learning web applications in a given mean time. It is compatible with many Python libraries, namely:
The command used to run a Streamlit web application is as follows:
streamlit run <yourscript.py>
Streamlit also has a cloud, where we can deploy our machine learning web applications. Anyone can access our application with the provided link.
There are many widgets available in Streamlit that we can use to develop web applications. We will discuss some of these below.
Note: Streamlit encounters some issues when it is run with the Safari browser. So, we recommend the use of Google Chrome or Firefox.
import streamlit as stream value = stream.slider('val') stream.write(value, 'cube is', value * value * value)
streamlit
.slider()
function, and we assign the value of the slider to the variable value
.import pandas as pd import streamlit as stream stream.title("Welcome!") stream.write("Our first DataFrame is") stream.write( pd.DataFrame({ 'Ali': [99, 87, 66, 54], 'Usman': [55, 96, 77, 98] }) )
Lines 1–2: We import pandas
and streamlit
.
Line 4: We set the title of our Streamlit web application using the title()
method.
Lines 8–13: We use the pandas Dataframe()
function, along with the Streamlit write()
function, to display the dataframe in a table.
import streamlit as stream stream.title("Welcome!") select = stream.selectbox( "Select Developer or Engineer", ["Developer", "Engineer"] ) stream.write(f"You selected {select}")
Line 1: We import streamlit
.
Line 3: We set the title of our Streamlit web application using the title()
method.
Lines 5–8: We use the selectbox()
function. The first argument that we pass to the function is a string to display and the second argument is a list of options to select from.
Line 9: We display the selected value using the write()
function.
import streamlit as stream stream.title("Select your skills!") checkbox = stream.checkbox("C++") if checkbox: val = "C++" else: val = "No value selected" stream.write(f"You selected: {val}")
Line 1: We import streamlit
.
Line 2: We set the title of our Streamlit web application using the title()
method.
Lines 3: We create a variable called checkbox
that contains a boolean value.
Lines 5–8: We write conditions for the values that we selected from the checkbox.
Line 10: We display the selected value using the write()
function.
import streamlit as stream select = stream.radio( "What's your favorite IT job", ('Developer', 'Designer', 'Engineer')) if select == 'Developer': stream.write('You selected Developer.') else: stream.write("You didn't select Developer.")
Line 1: We import streamlit
.
Lines 2–4: We use the radio()
function. The first argument that we pass to this function is a string to display and the second argument is a list of options to select from.
Lines 6-9: We use conditions to display the value that is selected from the checkbox.
import streamlit as stream stream.title("Welcome!") selectbox = stream.sidebar.selectbox( "Select Male or Female", ["Male", "Female"] ) stream.write(f"You selected {selectbox}")
Line 1: We import streamlit
.
Line 3: We set the title of our Streamlit web application using the title()
method.
Lines 5–8: We use the sidebar.selectbox()
function. The first argument that we pass to the function is a string to display and the second argument is a list of options to select from.
Line 9: We display the value that is selected from the sidebar selectbox.
import streamlit as stream color = stream.color_picker('Choose any Color from dialog', '#00f900') stream.write('The current selected color is', color)
streamlit
.color_picker()
function to choose the color from the colors dialogue box.