Create your first Flask (Python) Hello World App in 5 minutes

1 minute read

In this post, we will install Flask and will create our first app.

Prepare Environment

Create Directory

First, let’s create a different directory for our django project.

$ mkdir first_app
$ cd first_app/

Create and Activate Virtual Environment

Now, let’s create a virtual environment using Miniconda/Anaconda. You can also use pipenv

If you do not know, how to install Miniconda/Anaconda, visit this post first:

Data Science Work Environment setup on Linux/Mac

$ conda create --name flask python=3.6
$ source activate flask

Now, let’s install flask using pip.

$ pip install flask

Create First App

Just copy the first code from Flask Quickstart, and paste in a file. Here, I create the file named hello.py, and paste the following code.

from flask import Flask
app = Flask(__name__)

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

Now, let’s run the following two commands and the app will be launched. N.B. use set instead of export if you are using Windows opearating system.

$ export FLASK_APP=hello.py
$ flask run

* Serving Flask app "hello.py"
* 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://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [01/Jul/2020 12:39:56] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [01/Jul/2020 12:39:57] "GET /favicon.ico HTTP/1.1" 404 -

Now open your browser and paste http://127.0.0.1:5000/ in the URL bar. You will see the hello world message printed on the window.

Note that, you can use HTML tags within the code. For example, if you want the message to be appeared as H1 tag, change the code as follows:

from flask import Flask
app = Flask(__name__)

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

Now, run the flask again and reload the browser page.

$ flask run

Leave a Comment