Contents
Your First Flask Application
Getting started with Flask is super easy. Let’s dive into creating your very first Flask app—a simple “Hello, World!”—and explore some of the basics.
Writing a “Hello, World!” Flask Application
First, let’s write a small Flask application that displays “Hello, World!” when you visit it in your web browser.
Here’s what you’ll do:
Open your text editor and create a new file called
app.py
in your project directory.Write the following code in
app.py
:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
Let’s break this down:
from flask import Flask
: This imports the Flask class, which you use to create your Flask application.app = Flask(__name__)
: Here, you create an instance of the Flask class. This is your app.@app.route('/')
: This line is a decorator that tells Flask what URL should trigger thehello_world
function. In this case, it’s the root URL (/
), meaning the homepage.def hello_world():
: This is the function that runs when someone visits the homepage. It simply returns the string “Hello, World!” which is displayed in the browser.
Running the Flask Development Server
Now that you’ve written your first Flask application, it’s time to see it in action!
1. Make sure your virtual environment is activated (if you’re using one).
2. Run the Flask application by typing the following command in your terminal:
python app.py
You should see something like this:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
This means your Flask development server is up and running. It’s now serving your app at http://127.0.0.1:5000/
.
3. Open your web browser and go to
http://127.0.0.1:5000/
. You should see “Hello, World!” displayed on the page.
Understanding the @app.route Decorator and URL Routing
The @app.route
decorator is one of the core features of Flask. It’s used to bind a function to a specific URL route. Here’s how it works:
Decorators in Python: The
@app.route
is a Python decorator. A decorator is a way to modify or enhance functions without changing their actual code. In Flask,@app.route
modifies the function to handle web requests at a specific URL.URL Routing: When you visit a URL in your Flask app, Flask checks which function is associated with that URL using the
@app.route
decorator. For example:
@app.route('/')
def home():
return 'Welcome to the Homepage!'
In this example, when someone goes to http://127.0.0.1:5000/
, the home()
function runs, and “Welcome to the Homepage!” is displayed.
- Dynamic Routes: Flask also allows you to create dynamic routes that can accept variables. For example:
@app.route('/user/')
def show_user_profile(username):
return f'User {username}'
Here, if someone visits http://127.0.0.1:5000/user/John
, Flask will call show_user_profile()
with username
set to John
, and display “User John” in the browser.
By now, you’ve created and run your first Flask application, and you’ve got a basic understanding of how routing works. This sets the foundation for building more complex and interactive web applications with Flask.
Related Chapters
- What is Flask?
- Setting Up the Development Environment
- Your First Flask Application
- Understanding Flask Routing
- Rendering Templates
- Working with Forms
- Working with Databases
- User Authentication
- File Uploads and Handling
- RESTful APIs with Flask
- Application Configuration and Deployment
- Testing in Flask
- Flask Extensions
- Handling Error Pages
- Planning the Project
- Developing the Backend with Flask
- Developing the Frontend
- Deployment and Scaling