Learn Flask for Python - Full Tutorial
Table of Contents
Introduction
This tutorial provides a comprehensive guide to learning Flask, a lightweight web framework for Python. Flask is known for its simplicity and flexibility, making it ideal for beginners and experienced developers alike. By the end of this tutorial, you will understand how to set up a Flask application, create routes, handle requests, and render templates.
Step 1: Setting Up Your Environment
To start using Flask, you need to set up your development environment.
- Install Python: Ensure you have Python installed on your machine. You can download it from python.org.
- Install Flask: Open your terminal or command prompt and run the following command:
pip install Flask
Step 2: Creating a Basic Flask Application
Now that your environment is ready, it’s time to create a simple Flask application.
-
Create a Project Directory: Make a new folder for your Flask project:
mkdir flask_app cd flask_app
-
Create a Python File: Inside your project directory, create a file named
app.py
. -
Write Basic Flask Code: Open
app.py
and add the following code:from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello, Flask!" if __name__ == '__main__': app.run(debug=True)
-
Run Your Application: In the terminal, navigate to your project directory and run:
python app.py
You should see output indicating the server is running. Open a web browser and go to
http://127.0.0.1:5000
to see "Hello, Flask!"
Step 3: Understanding Routes and Views
Flask uses routing to determine how to respond to web requests.
- Define Routes: Use the
@app.route()
decorator to define the URL where a function will be executed. - Create Multiple Routes: Add more routes to your application:
@app.route('/about') def about(): return "This is the About page"
Step 4: Handling Form Data
Flask can handle incoming data through forms.
-
Import Required Classes:
from flask import request
-
Create a Form: Add an HTML form to your route:
@app.route('/submit', methods=['GET', 'POST']) def submit(): if request.method == 'POST': name = request.form['name'] return f"Hello, {name}!" return ''' <form method="post"> Name: <input type="text" name="name"> <input type="submit" value="Submit"> </form> '''
Step 5: Rendering Templates
Using templates allows you to create dynamic HTML pages.
-
Install Jinja2: Flask uses Jinja2 for templating, which is included by default.
-
Create a Template Folder: Make a folder named
templates
in your project directory. -
Create an HTML File: Inside
templates
, create a file namedindex.html
:<!doctype html> <html> <head><title>My Flask App</title></head> <body><h1>Hello from Flask!</h1></body> </html>
-
Render Template in Flask: Modify your route to render this template:
from flask import render_template @app.route('/') def home(): return render_template('index.html')
Conclusion
You have now set up a basic Flask application, created routes, handled form submissions, and rendered HTML templates. To further your learning, consider exploring Flask’s documentation, building more complex applications, or integrating with databases. For more resources, check the code repository here and continue experimenting with Flask to enhance your web development skills.