Introduction
Python frameworks such as Streamlit enable beginners and newbies to build and deploy highly interactive web applications spanning fields such as Data Science and Machine Learning. Creating Web applications has never been easier when beginners are not required to have front-end development experience.
Installation
Ensure Python is installed on your system before proceeding with the installation of "Streamlit". To do so execute the following in the command line terminal.
python -h
Now once you have confirmed Python is installed execute this command using PIP to install the streamlit python package.
To verify the installation of Streamlit run :
streamlit hello
Once this has been executed a new window should open up in your browser on the local port:
Local URL: http://localhost:8501
This is your default Streamlit application running on port 8501 once successful installation has been achieved.
Now let's create your first streamlit app.
Your First Streamlit App
- Create any new python file with a name ,eg., app.py.
- Lets import the Streamlit Library>>
Import streamlit as st
- Name a title and welcome message text to your app.
st.title("My App")
st.write("Welcome to my Streamlit Application")
- Now run the streamlit app by executing the command>>
streamlit run app.py
Upon execution the default browser should open your app on the port 8501 and you should see your message:
Some key features
Streamlit allows users to build several interactive widgets which enable building dynamic applications.
Some of these widgets are :
- Text Input
name = st.text_input("Enter your name:")
st.write(f"Hello, {name}!")
- Slider Input
number = st.slider("Select a number", 0, 100, 50)
st.write(f"You selected: {number}")
- Button
if st.button("Click Me"):
st.write("Button clicked!")
Data Use cases
Streamlit allows to make depictions of tabular data in chart formats>>
- DataFrame Display:
import pandas as pd
df = pd.DataFrame({
"Column 1": [10, 20, 30, 40],
"Column 2": [50, 60, 70, 80]
})
st.dataframe(df)
This is a basic data frame and it would show up as >>
- Charts:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
st.pyplot(fig)
A basic sine wave with X and Y co-ordinates is created in the app >>
A sidebar can be added for better UI organization :
- Sidebar
st.sidebar.header("Sidebar Header")
option = st.sidebar.selectbox("Choose an option:", ["Option 1", "Option 2"])
st.sidebar.write(f"You selected: {option}")
Side bars are great for managing several different tabs for a large application.
Deployment of Streamlit Apps
Once your app is completed, you can deploy it using:
Streamlit Community Cloud (free hosting for Streamlit apps)
Heroku, AWS, or Google Cloud
For deployment into the Streamlit Cloud, push your code to GitHub and link it to Streamlit Cloud.
read more about streamlit docs here.
Conclusion
Streamlit is a potent tool for easy development of interactive web apps for data science and machine learning. The elegantly simple syntax and rich feature set make it an ideal choice for all developers and data scientists who wish to quickly convert their ideas into real-life applications.
Top comments (0)