52. Flask: Intro and First Application [EN] - MantsSk/CA_PTUA14 GitHub Wiki

Flask: A Micro Web Framework for Python

Flask is an easy-to-learn and flexible micro web framework for Python programming language. It is a popular and often chosen library for creating web applications with minimal code and getting them up and running quickly. Flask is characterized by its simple syntax and powerful tools, allowing developers to create a variety of web applications ranging from simple websites to complex API systems.

Let’s Create Our Own Simple Application

Step 1: Install Flask

Before starting, ensure you have Python installed on your system. You can install Flask using pip, Python's package manager. Open your terminal or command prompt and run the following command:

pip install Flask

Step 2: Set Up Your Project

Create a new directory for your Flask project. Inside this directory, create a Python script file. For this tutorial, let's name it app.py.

Step 3: Write Your Flask Application

Open app.py in a text editor and write the following code:

from flask import Flask

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

# Define a route for the root URL ("/")
@app.route('/')
def hello():
    return 'Hello, World! This is my first Flask application.'

# Run the application
if __name__ == '__main__':
    app.run(debug=True)

In this code:

  • We import the Flask class from the Flask package.
  • We create an instance of the Flask class and assign it to the variable app.
  • We define a route for the root URL ("/") using the @app.route decorator. When a user accesses the root URL, the hello() function is executed.
  • The hello() function returns a simple message.
  • We use app.run() to run the application. Setting debug=True enables debugging mode, which provides helpful error messages in case something goes wrong.

Step 4: Run Your Flask Application

Open your terminal or command prompt, navigate to the directory where your app.py file is located, and run the following command:

python app.py

You should see terminal output similar to this:

 * Serving Flask app 'app'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 955-715-644
127.0.0.1 - - [09/Mar/2024 09:09:36] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [09/Mar/2024 09:09:36] "GET /favicon.ico HTTP/1.1" 404 -

This indicates that your Flask application is running locally on your computer.

Step 5: Access Your Flask Application

Open a web browser and enter the following URL in the address bar:

http://127.0.0.1:5000/

You should see the message "Hello, World! This is my first Flask application." displayed in the browser.

Congratulations! You have successfully created and run your first Flask application.

Exercises:

Note: As we won't include exercises in the next lesson, please create a separate Python file named exercise_app.py specifically for these exercises. This will keep the main app.py file clean and focused on the lesson content.

Exercise 1: Customizing Flask Application

Objective: Customize the Flask application by modifying the existing route to display a personalized message.

Exercise: Modify the existing route ("/") in the given Flask application to display a personalized message including your name. For example, the message could be "Hello, [Your Name]! Welcome to my Flask application."

After making the modification, run the application and test the route in the browser to see the personalized message.

Exercise 2: Flask Debugging Mode

Exercise: Experiment with Flask's debugging mode by intentionally introducing an error in the exercise_app.py file. For example, you can modify the hello() function to return an undefined variable. Run the application with debugging mode enabled (debug=True) and observe the error message provided by Flask.

Once you've observed the error message, fix the introduced error and run the application again to ensure it runs without any issues.

Exercise 3: Understanding Flask Routes (Optional)

Exercise: Modify the given Flask application (app.py) to include two additional routes:

Create a route for the URL "/about" that returns a message saying "This is the About page." Create a route for the URL "/contact" that returns a message saying "Contact us at: [email protected]". After modifying the exercise_app.py file, run the application and test each route in the browser to ensure they are functioning correctly.