Contents
Setting Up the Development Environment
Getting started with Flask involves a few key steps to set up your development environment.
Installing Python and pip
First things first, you need Python installed on your system. If you haven’t done that yet, head over to the Python website and download the latest version. When you install Python, you’ll also get pip
, which is Python’s package manager and will come in handy for installing Flask and other packages.
Creating a Virtual Environment
With Python and pip ready, the next step is to create a virtual environment. Think of a virtual environment as a dedicated space for your project, where you can install packages without affecting the rest of your system.
To create one, open your terminal or command prompt, navigate to your project folder, and run:
python -m venv venv
This command creates a virtual environment named venv
in your project directory.
To activate the virtual environment:
- On Windows:
venv\Scripts\activate
- On macOS/Linux:
source venv/bin/activate
You’ll know it’s activated when you see (venv)
at the beginning of your terminal prompt.
Installing Flask using pip
Now that your virtual environment is activated, installing Flask is super easy. Just run:
pip install Flask
This command will install Flask and any other dependencies it needs. Now, you’re all set to start using Flask in your project.
Setting Up a Basic Flask Project Structure
Let’s set up a basic structure for your Flask project. In your project folder, create the following files and directories:
/your-project
/venv
/static
/templates
app.py
static/
: This is where you’ll store static files like CSS, JavaScript, and images.templates/
: This folder is for your HTML templates.app.py
: This is your main Python file where you’ll write your Flask application code.
Here’s a simple example of what app.py
might look like:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, Flask!"
if __name__ == '__main__':
app.run(debug=True)
To run your Flask app, just execute:
python app.py
This will start a local server, and you can view your app by going to http://127.0.0.1:5000/
in your web browser.
Related Chapters
- What is Flask?
- Setting Up the Development Environment
- Your First Flask Application
- Understanding Flask Routing
- Rendering Templates
- Working with Forms
- Working with Databases
- User Authentication
- File Uploads and Handling
- RESTful APIs with Flask
- Application Configuration and Deployment
- Testing in Flask
- Flask Extensions
- Handling Error Pages
- Planning the Project
- Developing the Backend with Flask
- Developing the Frontend
- Deployment and Scaling