Getting Started with Flask: A Quick Python Web Framework Tutorial
Flask is a popular, lightweight Python web framework that's perfect for small applications and APIs. It's easy to get started with Flask, and its simplicity makes it an excellent choice for prototyping and learning web development. In this comprehensive guide, we'll walk you through the process of setting up a Flask project, creating your first application, and exploring some of its core features.
Setting Up Your Flask Environment
Before you start, ensure you have Python (3.6 or later) and pip installed on your system. Then, follow these steps to set up your Flask environment:
- Create a new directory for your project and navigate to it in your terminal.
- Create a new virtual environment to isolate your project's dependencies. You can do this with the following command:
python -m venv venv
- Activate the virtual environment:
- On Windows:
venv\Scripts\activate- On macOS/Linux:source venv/bin/activate - Install Flask using pip:
pip install flask
Creating Your First Flask Application
Now that you have Flask installed, let's create your first application. Create a new file named app.py in your project directory and add the following code:

```python from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Hello, World!' if __name__ == '__main__': app.run(debug=True) ```
This simple Flask application defines a single route ('/') that responds with the message "Hello, World!". Save the file and run your application using the command python app.py. Visit http://127.0.0.1:5000/ in your web browser to see your application in action.
Exploring Flask's Core Features
Now that you have a basic understanding of Flask, let's explore some of its core features.
Routing
Flask uses decorators to map URLs to Python functions. Here's an example of defining multiple routes in your app.py file:

```python @app.route('/') def home(): return 'Hello, World!' @app.route('/about') def about(): return 'This is the about page.' ```
Now, visiting http://127.0.0.1:5000/about will display the message "This is the about page."
Templates
Flask uses templates to render dynamic HTML content. Create a new folder named templates in your project directory, and inside it, create a new file named base.html. Add the following code to base.html:
```html
Next, update your routes in app.py to use templates:

```python from flask import render_template @app.route('/') def home(): return render_template('base.html', title='Home', content='Hello, World!') @app.route('/about') def about(): return render_template('base.html', title='About', content='This is the about page.') ```
Now, your application will display the content within the <main> tag, and the navigation bar will link to your routes.
Static Files
Flask allows you to serve static files like CSS, JavaScript, and images. Create a new folder named static in your project directory, and inside it, create a new file named styles.css. Add some basic styling to it:
```css body { font-family: Arial, sans-serif; } nav { background-color: #f8f9fa; padding: 10px; } nav a { margin-right: 10px; } ```
Update your base.html file to include the CSS file:
```html
Now, your application will have basic styling applied to it.
Debug Mode
Flask's debug mode provides helpful error messages and enables features like automatic reloading when you make changes to your code. To enable debug mode, update the app.run() line in your app.py file:
```python if __name__ == '__main__': app.run(debug=True) ```
With this setting, Flask will automatically reload your application when you make changes to your code and provide detailed error messages in your web browser.
Conclusion
In this comprehensive guide, we've covered the basics of setting up a Flask project, creating your first application, and exploring some of its core features. Flask's simplicity and flexibility make it an excellent choice for small applications and APIs. As you continue your web development journey, you'll find that Flask provides a solid foundation for building more complex web applications.






















