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.
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
.
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:
st
.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.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.
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:
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:
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:
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!
22/11/2024 | Python
08/11/2024 | Python
05/11/2024 | Python
26/10/2024 | Python
22/11/2024 | Python
26/10/2024 | Python
26/10/2024 | Python
15/10/2024 | Python
25/09/2024 | Python
25/09/2024 | Python
15/01/2025 | Python
14/11/2024 | Python