Creating Your First Flask Application
Course Title: Mastering Flask Framework: Building Modern Web Applications Section Title: Introduction to Flask and Development Environment Topic: Creating your first Flask application
Objective: By the end of this topic, you will have created and run your first Flask web application. You will understand the basic components of a Flask application and how to structure and initialize it.
Creating your first Flask application
In this topic, we will guide you through the process of creating your first Flask web application. We will cover the basic components of a Flask application and demonstrate how to structure and initialize it.
Step 1: Install Flask
Before we start creating our Flask application, we need to install Flask. If you haven't installed Flask yet, you can do so by running the following command in your terminal:
pip install flask
Step 2: Create a new project directory
Create a new directory for your project and navigate into it:
mkdir my_flask_app
cd my_flask_app
Step 3: Create a new Flask application
Create a new file called app.py
and add the following code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
This code creates a new Flask application and defines a single route for the root URL ('/'). The index()
function returns a simple 'Hello, World!' message.
Step 4: Initialize the Flask application
To initialize the Flask application, we need to set the FLASK_APP
environment variable to the name of our application, and then run the application using the flask run
command. Here's how you can do it:
Windows
set FLASK_APP=app.py
flask run
Mac/Linux
export FLASK_APP=app.py
flask run
Using a virtual environment (recommended)
FLASK_APP=app.py flask run
Step 5: Run your Flask application
Once you've initialized your Flask application, you can run it by navigating to http://localhost:5000
in your web browser. You should see the 'Hello, World!' message displayed on the page.
Debug mode
Flask provides a debug mode that automatically reloads the application when changes are detected in the code. To enable debug mode, you can set the FLASK_DEBUG
environment variable to True
:
export FLASK_DEBUG=True
Alternatively, you can set the debug
parameter to True
when creating the Flask application:
app = Flask(__name__)
app.debug = True
Key Concepts
- Flask application instance: The Flask application instance is the central component of a Flask application. You create it by passing the
__name__
attribute of the current module to theFlask
class. - Route decorator: The
@app.route()
decorator is used to define a route for a specific URL. The function decorated with this decorator is called the view function. - View function: The view function is responsible for handling the request and returning a response to the client.
- Request object: The request object is an instance of the
Request
class and provides information about the incoming request.
Practical Takeaways
- You can create a new Flask application by instantiating the
Flask
class and passing the__name__
attribute of the current module to it. - You can define routes for specific URLs using the
@app.route()
decorator. - You can run the Flask application using the
flask run
command.
External Resources
What's Next?
In the next topic, we will cover 'Defining routes and URL building in Flask.' We will discuss how to define routes for specific URLs, create URLs, and navigate between routes.
Do you have any questions or need help with this topic? Leave a comment below!
Images

Comments