How to run your first Hello World in Flask
A Beginner’s Guide to Running Your First Hello World program in Flask
This article will help you to run ‘Hello World!’ with Flask with a step-by-step guide to setting up your Python environment on Windows 10!
Flask Introduction
Before we dive into the installation process, let’s take a quick look at the Flask framework. Flask is a popular Python framework used to build small and medium-sized web applications.
It’s known for being easy to learn, making it an excellent choice for beginners looking to get started with web development.
With Flask, you can quickly create Python-based web apps that are both powerful and flexible.
This article will guide you through every step of the process to print and run the ‘hello world’ app using Flask.
Whether you’re new to Flask or just looking to refresh your skills, you’ll find everything you need to know right here.
Setting up Python Environment
The first thing that you need to do is to install Python. I recommend choosing Python 3 as it is widely used in the tech industry.
Install Python
- Download the latest version of Python from the official docs of Python by following this link.
- If you already have Python installed on your computer, you can skip this step.
- To check your Python version, open the command prompt and type
python --version
.
>>> python --versionp
Python 3.10.4
Install text editor
You can use any text editor you prefer, but for this tutorial, I’ll be using Visual Studio Code. You can download VS Code from its official documentation by clicking this link.
Once you’ve installed your preferred text editor, you can start creating a virtual environment.
Creating the Virtual Environment
Before creating any project in Flask, it is important to create a virtual environment. It will help you to isolate your flask projects from one another, ensuring that each project has its own set of dependencies.
To create a virtual environment, follow these steps:
- Create a folder for your project. Name it whatever you want.
- Open the terminal and navigate to the project directory.
- Install virtual environment by writing
pip install virtualenv
. - Then create a project folder by typing
mkdir <project_name>
in the terminal and navigate to the project directory by typingcd <project_name>
. - Create a virtual environment by typing
python -m venv venv
in the terminal. - Finally, activate the virtual environment by running
venv\Scripts\activate
in the terminal.
Now you have activated a virtual environment within your project directory. It’s time to install Flask.
Install Flask
To install flask on your computer, write in the terminal this command:
>>> pip install flask
Running Hello World in Flask
Now that you have activated your virtual environment and installed Flask on your computer, it’s time to build your first Flask project.
For that, open VS Code from your project directory, create a folder, and name it ‘app.py’.
Within that folder, write the following code:
from flask import Flask
app = Flask(__name__)
@app.route(“/”)
def hello_world():
return “Hello World!”
if __name__ == ‘__main__’:
app.run()
Your Flask setup has been completed. However, before running your Flask app, you need to set up the Flask app.
For this, open the VS Code terminal and type:
>>> set FLASK_APP=hello.py
Note that ‘hello’ should be replaced with your project name, and you should be in your project directory.
Run Flask
You have completed everything that you need to do.
Run your first Flask app by typing the following command in the terminal:
>>> flask run
After typing the flask run
command, you will get the link “http://127.0.0.1:5000".
Copy and paste it into your browser, and you will see “Hello, World!” printed on your browser.
Congratulations! You have successfully run your first Flask project. You are now ready to build more complex and dynamic websites with Python and Flask.
Conclusion
In this article, we learned about the Flask framework and went through every step that is needed to run a ‘Hello, World!’ program in Flask. It was a brief introduction to getting started with the Flask framework. After finishing this tutorial, you will be able to build your first real project with Python and Flask.