Creating Your First Flask Application

Tutorial 2 of 5

1. Introduction

Welcome to the Flask tutorial for beginners. In this tutorial, our main goal is to create a simple Flask web application from scratch. Flask is a powerful micro-framework for Python that allows for quick and easy web application development.

At the end of this tutorial, you will have a basic understanding of how to:

  • Set up a Flask project
  • Create routes and views in Flask
  • Use templates in Flask

Prerequisites:

Before we begin, ensure you have the following:

  • A basic understanding of Python programming language
  • Python installed on your system (Version 3.6 or later recommended)
  • A text editor or IDE (Integrated Development Environment) such as VS Code, PyCharm, or Sublime Text.

2. Step-by-Step Guide

Here is a detailed guide on how to create your first Flask application.

Installing Flask

The first thing we need is Flask itself. You can install Flask using pip:

pip install flask

Creating a new Flask Application

After you've installed Flask, you can create a new Flask application. Start by creating a new folder named "flask_app". Inside this folder, create a new Python file named "app.py". This is where we will write our Flask application.

Here's a simple Flask application:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, Flask!"

if __name__ == '__main__':
    app.run(debug=True)

3. Code Examples

Let's dive deeper into the Flask application we just created:

# Import the Flask class from the flask module
from flask import Flask

# Create an instance of the Flask class
app = Flask(__name__)

# Define a route for the URL "/"
@app.route('/')
# Define a function that is triggered when the user visits the URL "/"
def home():
    # Return a string to be displayed on the webpage
    return "Hello, Flask!"

# Check if the script is running directly
if __name__ == '__main__':
    # Run the Flask application with debug mode enabled
    app.run(debug=True)

When you run this script and navigate to http://localhost:5000/ in your browser, you should see the text "Hello, Flask!".

4. Summary

In this tutorial, we've learned the basics of Flask including how to set up a Flask project, how to create routes and views, and how to use templates. Flask is a powerful tool for creating web applications, and we've only just scratched the surface of what it can do.

As next steps, consider exploring the following topics:

  • Flask's official documentation
  • How to use databases in Flask
  • How to handle forms in Flask
  • How to use Flask's session management

5. Practice Exercises

To solidify your understanding of Flask, try the following exercises:

  1. Create a new route that displays your name when visited.
  2. Create a new route that accepts a name as a parameter and displays a personalized greeting.
  3. Add a new route that makes use of a template to display information.

Solution

  1. Adding a new route is as simple as defining a new function and using the @app.route() decorator:

python @app.route('/name') def name(): return "Your Name"

  1. To accept a parameter in a route, you can specify it in the @app.route() decorator:

python @app.route('/greet/<name>') def greet(name): return f"Hello, {name}!"

  1. To use a template, first create a new folder named "templates" in your project directory. Inside this folder, create a new file named "info.html". Then, you can use the render_template() function to render this template:

```python
from flask import render_template

@app.route('/info')
def info():
return render_template('info.html')
```

In "info.html", you can write HTML code to display whatever information you want. For more complex data, you can pass variables to the template from your route function.