Table of Contents
Introduction
Flask is a lightweight web framework in Python that allows you to build web applications quickly and easily. In this tutorial, we will build a simple web app using Flask. By the end of this tutorial, you will have a basic understanding of Flask and be able to create a web app that can handle different routes and display content.
Prerequisites
Before you start this tutorial, you should have a basic understanding of Python programming and how web applications work. It would be helpful if you have some knowledge of HTML and CSS, but it’s not required.
Setup
To get started with Flask, you need to have Python installed on your machine. You can download Python from the official website and follow the installation instructions for your operating system.
Once Python is installed, open a terminal or command prompt and verify that it’s correctly installed by running the following command:
python
python --version
You should see the version number of Python printed on the screen.
Next, you need to install Flask. Open your terminal or command prompt and enter the following command:
python
pip install flask
This will install Flask and its dependencies.
Creating a Flask App
Step 1 - Installing Flask
Before we can start building our Flask app, we need to install Flask. As mentioned before, you can use pip to install Flask by running the following command:
python
pip install flask
Step 2 - Importing Flask
Once Flask is installed, we need to import it into our Python script. Create a new file called app.py
and open it in a text editor. Add the following line at the top of the file to import Flask:
python
from flask import Flask
Step 3 - Creating the Flask App
After importing Flask, we need to create an instance of the Flask class. This instance will represent our web app. Add the following code below the import statement:
python
app = Flask(__name__)
The __name__
attribute is a special attribute in Python that represents the name of the current module. This is necessary for Flask to know where to look for resources such as templates and static files.
Defining Routes
In Flask, routes are used to map URLs to functions that handle the requests. We can define routes using decorators provided by Flask.
Step 1 - The Main Route
Let’s start by defining a route for the main page of our web app. After creating the Flask app, add the following code:
python
@app.route('/')
def index():
return "Hello, Flask!"
In this code, we’re using the @app.route()
decorator to specify that any requests to the root URL (“/”) should be handled by the index()
function. This function will return the string “Hello, Flask!” as the response.
Step 2 - Adding More Routes
To demonstrate how to handle multiple routes, let’s add a couple more routes to our app. Add the following code after the index()
function:
```python
@app.route(‘/about’)
def about():
return “This is the About page.”
@app.route('/contact')
def contact():
return "This is the Contact page."
``` In this code, we're defining two additional routes: "/about" and "/contact". The `about()` function will return the string "This is the About page." as the response, and the `contact()` function will return the string "This is the Contact page.".
Running the App
Now that we have our Flask app and routes defined, we can run our web app and see it in action.
To run the app, open your terminal or command prompt, navigate to the directory where your app.py
file is located, and enter the following command:
python
python app.py
Flask will start the development server and display the URL on which it’s running. By default, it will be something like http://127.0.0.1:5000/
.
Open your web browser and enter the URL http://127.0.0.1:5000/
(or the URL printed by Flask) to access your web app. You should see the message “Hello, Flask!” displayed on the page. You can also try accessing the “/about” and “/contact” routes to see the corresponding messages.
To stop the app, press Ctrl + C
in the terminal or command prompt.
Conclusion
In this tutorial, we learned how to get started with Flask by building a simple web app. We covered the basic steps of installing Flask, importing it into our Python script, creating a Flask app, defining routes, and running the app. By following this tutorial, you should now have a basic understanding of how Flask works and be able to build your own web apps using Flask.
Remember that this is just a starting point, and Flask has many more features and capabilities that you can explore. Check out the Flask documentation for more information and tutorials on advanced topics. Happy Flask coding!