Introduction
Before diving into Streamlit development, it's crucial to set up a proper Python environment. This ensures smooth development, dependency management, and project isolation. Let's walk through the process step-by-step.
Installing Python
First things first, you need Python installed on your system. Streamlit supports Python 3.7 and above.
- Visit the official Python website (https://www.python.org/downloads/)
- Download the latest version for your operating system
- Run the installer and follow the prompts
- Make sure to check the box that says "Add Python to PATH"
To verify the installation, open a terminal and type:
python --version
You should see the Python version displayed.
Setting Up a Virtual Environment
Virtual environments are isolated Python environments that allow you to manage project-specific dependencies. This is especially useful when working on multiple projects with different requirements.
Using venv (built-in)
- Open a terminal and navigate to your project directory
- Create a new virtual environment:
python -m venv streamlit_env
- Activate the virtual environment:
- On Windows:
streamlit_env\Scripts\activate
- On macOS/Linux:
source streamlit_env/bin/activate
- On Windows:
Using Conda (alternative)
If you prefer using Conda:
- Install Anaconda or Miniconda
- Open a terminal and create a new environment:
conda create --name streamlit_env python=3.9
- Activate the environment:
conda activate streamlit_env
Installing Streamlit
With your virtual environment activated, install Streamlit using pip:
pip install streamlit
To verify the installation:
streamlit hello
This should launch a demo Streamlit app in your default web browser.
Choosing an IDE
A good Integrated Development Environment (IDE) can significantly boost your productivity. Here are some popular options:
-
Visual Studio Code (VS Code): Free, lightweight, and highly customizable with a rich extension ecosystem.
-
PyCharm: A powerful IDE specifically designed for Python development, with both free and paid versions available.
-
Jupyter Notebooks: Great for data analysis and experimentation, especially when combined with Streamlit's support for Jupyter widgets.
For this guide, let's focus on setting up VS Code, as it's free and widely used.
Setting Up VS Code for Streamlit Development
- Download and install VS Code from https://code.visualstudio.com/
- Open VS Code and install the Python extension
- Open your project folder in VS Code
- Select your virtual environment as the Python interpreter:
- Press
Ctrl+Shift+P
(orCmd+Shift+P
on macOS) - Type "Python: Select Interpreter"
- Choose the interpreter from your virtual environment
- Press
Useful VS Code Extensions for Streamlit Development
- Python: Essential for Python development
- Streamlit: Provides syntax highlighting and snippets for Streamlit
- Pylance: Offers fast, feature-rich language support for Python
- GitLens: Enhances Git integration within VS Code
Creating Your First Streamlit App
Let's create a simple Streamlit app to test our setup:
- Create a new file called
app.py
- Add the following code:
import streamlit as st st.title("Hello, Streamlit!") st.write("Welcome to your first Streamlit app.") number = st.slider("Select a number", 0, 100) st.write(f"You selected: {number}")
- Save the file and run it using the terminal:
streamlit run app.py
Your default web browser should open, displaying your first Streamlit app!
Managing Dependencies
As your project grows, you'll likely add more dependencies. Keep track of them using a requirements.txt
file:
- Create a
requirements.txt
file in your project root - Add your dependencies, one per line:
streamlit
pandas
matplotlib
- Install dependencies using:
pip install -r requirements.txt
This makes it easy to recreate your environment on other machines or for collaboration.
Version Control with Git
Version control is crucial for any development project. Here's how to set up Git for your Streamlit project:
- Install Git from https://git-scm.com/
- Initialize a Git repository in your project folder:
git init
- Create a
.gitignore
file to exclude unnecessary files:
# .gitignore
streamlit_env/
__pycache__/
*.pyc
- Make your initial commit:
git add . git commit -m "Initial commit"
With these steps completed, you've successfully set up a robust development environment for your Streamlit projects. Happy coding!