In this basic Python Flask tutorial, we will see how to install Flask. We also implement a simple Hello World application using Flask. We will also learn how to run the Hello World Python Flask application.
Table of Contents
- Key Takeaways
- Before you begin
- Develop Simple Hello World Flask Application
- Run the Flask Application
- Demo Flask Hello World Application 📺
- Full Source code
- Video Tutorial
Key Takeaways
By the end of this tutorial you will be able to
- Setup an environment to create a basic hello world Flask application.
- Create, activate and set up the virtual environment.
- Write a simple hello world Flask application and understand the code line by line.
- How to Run simple Flask app and Stop the application.
Before you begin
Ensure you have latest Python programming language installed on your PC. If you are on a Windows OS, check this video that details How to Install Python on Windows 10 OS. Flask is a module or library or package implemented in Python programming language to develop web applications that scale. In order to install Flask, you need pip to help you install it. pip is the python package installer that comes default with installation of Python.
Set up Working Directory
To set up the virtual environment run the following commands from the command terminal
mkdir flaskhelloworld
cd flaskhelloworld
Setup the Virtual Environment
To create our flask hello world app, we will setup a virtual environment. To understand why we need a virtual environment you can go through the detailed tutorial that explains Working with Virtual Environments in Python for Beginners.
Create the Virtual Environment
From Windows OS, from the command prompt type the following command to create the virtual environment.
python -m venv env
From Unix based OS, from the command prompt type the following command to create the virtual environment.
python3 -m venv env
Activate the Virtual Environment
Unix OS
source ./env/bin/activate
Windows OS
env/Scripts/activate
Once you have successfully activated the virtual environment, you should see the command line terminal shows the name of the virtual environment in closed parentheses as a prefix to the active terminal input.
This is how the terminal with activated virtual environment looks in Windows OS
(env) c:/Users/navule/helloflask
Ensure that you perform the further steps with virtual environment set active. Do not close the command prompt. If you want to work on the same project, you need to set the current working directory to the helloflask and then activate the environment and work as usual.
Upgrade pip
Before installing Flask with the help of pip, ensure to upgrade the version of pip by running the following command.
python -m pip install --upgrade pip
Install Flask
Now install Flask module with the help of pip
pip install flask
Freeze dependencies to a file
Run the following command to freeze all the Python dependencies for Flask application.
pip freeze > requirements.txt
Develop Simple Hello World Flask Application
As we have successfully setup the environment for developing a basic Python Flask application, we will proceed with the implementation.
Create a python file
From the command line terminal run the following command to create a python file named app.py where our hello world program will be written.
echo '' > app.py
Write Python Flask Hello World App in just 6 lines of code
You can run a simple hello world flask application in just 6 lines of code.
# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
app.run(host='0.0.0.0', port=5000)
All you need to do is copy those 6 lines of code in a file named app.py or file name of your choice with extension py.
You notice that the Hello World! web application is dependent only on Flask framework and python programming language. Nothing Else!.
Detailed explanation of our Hello World Flask Application
Following is the line by line explanation of what is happening in our app.py Hello World application
- Line 2: we are importing flask module’s class named Flask
- Line 3: we are initializing the Flask module with the name of our package or application.
__name__
evaluates to app in our case as the module or file name is app.py - Line 5: @app.route(‘/’) indicates that we have a route in our application that when access with / will invoke the functionality it is decorated with. In our case its a method named hello.
- Line 6: def hello(): is the standard way to declare a method that holds what this routine should do.
def
is the python standard keyword to declare a method and hello is the name of the method. Empty parentheses indicate that the hello method does not require any arguments to be passed in order to invoke it. A method definition should end with colon and hence we havedef hello():
- Line 7: Our application when accessed with route ‘/’ should be returning the text ‘Hello, World!’.
- Line 9: Finally we are configuring our Flask app to run on localhost. 0.0.0.0 defaults to localhost. You can also mention ‘localhost’ or ‘127.0.0.1’ as the value of
host
argument. But it is preferred to set it to ‘0.0.0.0’ as it doesn’t require further setting when we deploy it to the various environments. In addition to setting the value ofhost
, we are also configuring our application to run on a specificport
5000. If port 5000 is not available, you have the option to set it to any other free port available in order to run our Flask app.
Run the Flask Application
Type the following command from your command prompt and your web app should be up and running.
python app.py
You will see the following output from the command terminal.
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
To access the application, from any browser of your choice type in the following url
http://localhost:5000/
And the web page should show you the output as Hello, World!
Stop the Flask Application
Once you are done, stop the application running by hitting the keys Ctrl+C
Deactivate the Virtual Environment
It is good to deactivate the virtual environment after you are done working on the project. Type the following command to deactivate the Python virtual environment
deactivate
Demo Flask Hello World Application 📺
Watch the real demonstration of Flask Hello World Application 📺
Full Source code
Full Source code of this tutorial is available here
Video Tutorial
Congratulations 🎉, you have mastered how to implement a simple Python Flask hello world application and run it on a PC.
Bookmark 🔖 (Ctrl + D) this page as a quick reference on how to Develop a Python🐍 Flask Hello World App from scratch.
Pingback: Setup Visual Studio Code for Python Development – TutLinks
Pingback: Debugging Flask App with VS Code Made Easy – TutLinks