Streamlit is an open-source app framework designed specifically for machine learning and data science projects. With Streamlit, you can create interactive, beautiful web apps with minimal effort. This guide will walk you through the basics of setting up and using Streamlit to build your first web app.
1. Introduction to Streamlit
Streamlit allows you to transform your data scripts into shareable web apps in minutes. It is particularly popular among data scientists and machine learning engineers for its simplicity and efficiency. With Streamlit, you don’t need any front-end development skills to create interactive UIs for your models and data analyses.
2. Setting Up Your Environment
Before you start building with Streamlit, you need to set up your development environment. Follow these steps:
- Install Python: Ensure you have Python 3.6 or later installed on your system.
- Create a Virtual Environment: It’s a good practice to create a virtual environment for your project. You can do this with the following commands:shCopier le code
python -m venv myenv source myenv/bin/activate # On Windows, use `myenv\Scripts\activate`
- Install Streamlit: Use pip to install Streamlit.shCopier le code
pip install streamlit
3. Creating Your First Streamlit App
Now that your environment is set up, you can create your first Streamlit app.
- Create a Python Script: Create a new Python file (e.g., app.py).
- Write Your Streamlit App: Add the following code to your script to create a simple
import streamlit as st
st.title('Hello, Streamlit!')
st.write('This is my first Streamlit app.') - Run Your App: In your terminal, navigate to the directory containing
app.py
and run:shCopier le codestreamlit run app.py
Open the URL provided in the terminal to see your app in action.
4. Adding Interactivity
Streamlit makes it easy to add interactive widgets to your app. Here are a few examples:
- Text Input
name = st.text_input('Enter your name:')
st.write(f'Hello, {name}!')
- Slider:pythonCopier le code
age = st.slider('Select your age:', 0, 100) st.write(f'You are {age} years old.')
- Selectbox:pythonCopier le code
favorite_color = st.selectbox('What is your favorite color?', ['Red', 'Green', 'Blue']) st.write(f'Your favorite color is {favorite_color}.')
5. Customizing Your App
You can further customize your Streamlit app by adding more components, such as charts and images.
- Displaying Charts:pythonCopier le code
import pandas as pd import numpy as np data = pd.DataFrame(np.random.randn(100, 3), columns=['a', 'b', 'c']) st.line_chart(data)
- Displaying Images:pythonCopier le code
from PIL import Image image = Image.open('path_to_your_image.jpg') st.image(image, caption='Sample Image')
6. Deploying Your Streamlit App
Once your app is ready, you can deploy it to share with others. Streamlit provides a platform called Streamlit Sharing for free deployments.
- Push Your Code to GitHub: Ensure your app is in a GitHub repository.
- Deploy on Streamlit Sharing: Sign in to Streamlit Sharing and link your GitHub repository.
Follow the prompts to deploy your app, and you will receive a shareable link.
7. Conclusion
Streamlit is a powerful tool for creating interactive web applications with ease. By following this guide, you should have a basic understanding of how to set up and build your first Streamlit app. With practice, you can create more complex and useful applications to showcase your data science and machine learning projects.