Introduction to Streamlit
Streamlit is a game-changer in the world of Python web development. It allows data scientists and developers to quickly turn data scripts into shareable web apps without the need for extensive web development knowledge. In this guide, we'll walk through the process of creating your first Streamlit app, setting you on the path to building more complex and interactive data applications.
Setting Up Your Environment
Before we dive into coding, let's set up our development environment:
-
Install Streamlit using pip:
pip install streamlit
-
Create a new Python file, let's call it
first_app.py
.
Building Your First Streamlit App
Let's create a simple app that allows users to input their name and displays a personalized greeting. Here's the code:
import streamlit as st def main(): st.title("My First Streamlit App") # Get user input user_name = st.text_input("Enter your name") # Display greeting if user_name: st.write(f"Hello, {user_name}! Welcome to Streamlit!") if __name__ == "__main__": main()
Let's break down this code:
- We import the Streamlit library as
st
. - We define a
main()
function to contain our app's logic. st.title()
sets the main title of our app.st.text_input()
creates a text box for user input.st.write()
displays text on the app.
Running Your App
To run your Streamlit app, open a terminal, navigate to the directory containing your Python file, and run:
streamlit run first_app.py
Your default web browser should open automatically, displaying your app.
Adding More Elements
Streamlit offers a wide range of elements to make your app more interactive. Let's enhance our app with a few more features:
import streamlit as st import random def main(): st.title("My Enhanced Streamlit App") # Get user input user_name = st.text_input("Enter your name") # Display greeting if user_name: st.write(f"Hello, {user_name}! Welcome to Streamlit!") # Add a button if st.button("Get a random number"): st.write(f"Here's your random number: {random.randint(1, 100)}") # Add a slider age = st.slider("How old are you?", 0, 120, 25) st.write(f"You're {age} years old!") # Add a selectbox color = st.selectbox("What's your favorite color?", ["Red", "Green", "Blue"]) st.write(f"Your favorite color is {color}") if __name__ == "__main__": main()
This enhanced version includes:
- A button that generates a random number
- A slider for selecting age
- A dropdown (selectbox) for choosing a favorite color
Styling Your App
Streamlit allows for easy styling of your app. Here's how you can add some basic styling:
import streamlit as st st.set_page_config(page_title="My Awesome App", page_icon=":smiley:") # Custom CSS st.markdown(""" <style> .big-font { font-size:20px !important; } </style> """, unsafe_allow_html=True) st.title("Styled Streamlit App") st.markdown('<p class="big-font">This is a paragraph with a custom style!</p>', unsafe_allow_html=True)
This example demonstrates:
- Setting a custom page title and icon
- Adding custom CSS
- Applying custom styles to specific elements
Deploying Your App
Once you're happy with your app, you might want to share it with others. Streamlit offers a free hosting service called Streamlit Cloud. Here's a quick guide to deploying your app:
- Push your code to a GitHub repository.
- Sign up for a free account at streamlit.io.
- Connect your GitHub account and select the repository containing your Streamlit app.
- Choose the Python file you want to deploy.
- Click 'Deploy', and your app will be live in minutes!
Conclusion
Congratulations! You've just created your first Streamlit app. This is just the beginning of what you can achieve with Streamlit. As you continue to explore, you'll discover how to integrate data visualization, machine learning models, and more complex interactions into your apps.
Remember, the key to mastering Streamlit is practice and experimentation. Don't be afraid to try out different elements and see how they work together. Happy coding!