Flask Web Development
Introduction to Flask
What is Flask?
Flask is a tool for building websites and web applications using Python. It's known as a "microframework." This doesn't mean it's lacking features, but rather that it keeps its core simple and lets you add tools as you need them. Think of it like a starter toolkit. It has the essentials to get the job done, but it doesn't weigh you down with specialized tools you might never use.
Flask is a micro web framework written in Python.
This minimalist approach makes Flask very flexible and easy to learn. It's perfect for building smaller projects, creating web APIs, or just getting started with web development in Python without feeling overwhelmed.
Setting Up Your Workspace
Before we write any code, we need to set up a clean workspace. In Python, we do this using a virtual environment. A virtual environment is an isolated space on your computer for a specific project. It ensures that the tools and packages you install for one project don't interfere with your other projects. It's like giving every project its own dedicated workshop.
First, let's create a folder for our new application.
mkdir my_flask_app
cd my_flask_app
Now, from inside your new my_flask_app directory, create the virtual environment. We'll call it venv.
# This command uses Python's built-in venv module
python3 -m venv venv
You've created the environment, but you still need to activate it. The command is slightly different depending on your operating system.
# On macOS and Linux
source venv/bin/activate
# On Windows
.\venv\Scripts\activate
Once activated, you'll see (venv) appear at the beginning of your command prompt. This confirms you are now working inside your project's isolated environment.
Your First Flask App
With the environment active, we can install Flask using pip, Python's package installer.
pip install Flask
That's it! Flask is installed. Now, create a new file named app.py in your project directory and open it in a code editor. Add the following code.
# 1. Import the Flask class
from flask import Flask
# 2. Create an instance of the Flask class
app = Flask(__name__)
# 3. Define a route and a view function
@app.route('/')
def hello_world():
return 'Hello, World!'
Let's break that down:
- We import the main
Flaskclass from theflaskpackage. - We create our web application, giving it the special name
__name__so Flask knows where to find things. - The
@app.route('/')part is called a decorator. It tells Flask that whenever someone visits the main page of our site (the/route), it should run thehello_world()function right below it. - The
hello_world()function is simple. It just returns the text "Hello, World!" which will be displayed in the browser.
To run your new application, go back to your terminal (make sure your virtual environment is still active and you're in the my_flask_app directory) and use the flask run command.
flask run
You should see some output, including a line that looks like this:
Running on http://127.0.0.1:5000
Open that URL in your web browser. You'll see your "Hello, World!" message. Congratulations, you've just built and launched your first web application with Flask.
What is the primary purpose of using a virtual environment in a Python project?
In a basic Flask application, what is the role of the @app.route('/') decorator?
This simple setup is the foundation for every Flask project, big or small.