How to create a "Hello, world" app using Python Flask

Key takeaways:

  • Flask is a lightweight Python framework for quickly building web applications and APIs.

  • Flask applications require minimal setup, making it easy to get started.

  • Virtual environments help keep your Flask project’s dependencies isolated from other projects.

  • The if __name__ == "__main__" statement ensures code only runs when the file is executed directly, not when it is imported.

  • You need to set the FLASK_APP environment variable to define the entry point of a Flask application.

  • Running flask run command in the terminal starts your Flask application and lets you view it in a browser.

  • Flask is versatile for creating both simple applications and more structured MVC apps and APIs.

Flask is a lightweight Python framework designed for building web applications and APIs. Its simple and flexible architecture allows developers to get started quickly while providing the tools needed to scale and customize applications as needed.

In this Answer, you will learn how to create a basic Flask application that displays “Hello, World!”.

Step 1: Set up the virtual environment

First, we need to set up the virtual environment. A virtual environment will keep the Flask app’s dependencies separate, avoiding conflicts with other projects. To do so, follow the instructions below.

  1. Click the “Run” button at the bottom of the following widget to activate the terminal.

  1. After the default commands in the terminal get executed, run the following commands in the terminal:

python3 -m venv venv
. venv/bin/activate

Step 2: Create your first Flask app

Now that you know how to create a virtual environment, let’s create our first Flask application. Below we have an app.py file that does the following:

  1. Imports the Flask class from the flask package. Flask is used to create instances of web applications.

  2. Initializes the app as an instance of Flask.

  3. Sets a function that responds to the index route, /, and displays Hello, World!.

  4. Runs the app if the current script being executed is app.py.

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == "__main__":
app.run()
The app.py file

What does if __name__ == "__main__" mean?

The conditional check if __name__ == "__main__" is used to determine if the script being executed is the main program file (in this case, app.py).

For example, suppose you have a script named utils.py with a single line print(__name__). When you import utils.py into another file, say app.py, and execute app.py file, the print statement in utils.py will display “utils,” which is the file’s name.

However, if you run app.py directly, the __name__ variable for app.py will be set to "__main__". This allows if __name__ == "__main__": to act as a safeguard, running certain code (like app.run()) only when app.py is executed as the main program and not when it’s imported as a module in another script.

Step 3: Run your Flask 'Hello, World" application

Now that you have created your Flask application, it’s time to run it and see “Hello, World!” displayed in your browser. Click the “Run” button below and do the following in the terminal:

  1. Ensure that Flask is installed in your virtual environment:

pip install Flask
  1. A Flask application has a single entry point. For the terminal to know the entry point of your app, you need to set a FLASK_APP environment variable. Use the following command in the terminal to set the environment variable:

export FLASK_APP=app.py
  1. Execute the following command to start the Flask development server:

flask run --host=0.0.0.0

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == "__main__":
   app.run()

Complete application

In the following code widget, the code and all the commands have already been set, simply run the widget and check the output tab. You should see the message “Hello, World!” displayed in the browser:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == "__main__":
    app.run()

Next steps

In this Answer, you've learned how to set up a web application using the Flask framework. You have also experienced how Flask allows you to set up a web application with minimal configuration. These applications range from Model-View-Controller (MVC) apps to APIs.

Excited to explore how Flask can be used to create impactful applications? Continue your learning of Flask framework with our following projects:

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is Flask used for in Python?

Flask is a lightweight web framework in Python that is used to create web applications and APIs. It provides essential tools for handling routing and requests, giving developers flexibility to add custom features and making it ideal for both small applications and scalable microservices. For a more detailed introduction to Flask, have a look at Python Flask tutorial: Build your first Flask application.


Is Flask a frontend or backend technology?

Flask is a backend technology; it handles the server side of web development.


Is Flask easy to learn?

Yes, Flask is beginner-friendly and relatively easy to learn, especially for those new to web development.


Is Flask better than Django?

Flask is simple and flexible, ideal for smaller projects needing custom setups, as it provides only core features, giving developers control over additional components. Django, with built-in features like an ORM, authentication, and admin tools, is suited for larger, more complex projects, offering a structured, scalable setup that speeds up development. Therefore, we can say that Flask supports lightweight scalability, while Django’s ‘batteries-included’ approach is preferred for more comprehensive applications.

Flask vs Django: Which Python framework is right for you? covers the topic in more detail.


Free Resources