How To Create Routes In Flask

How To Create Routes In Flask

Flask is a Python framework for web development where you can get up and running with a single file and five lines of code! Flask is also a great way to create an API and you can create a JSON API in one line of code. Flask has many powerful features to help you develop websites quickly. There is a templating system, message flashing, URL routing, support for serving static files, and more. In this Flask tutorial, we’ll get Flask installed and learn how to create some routes in Flask.


Install Flask Web Framework

Installing Flask is a simple one liner using the Python pip tool.

flask $pip install flask

Create Directory For Project

Let’s create a simple directory to hold the Python files that we’ll use to test Flask.

flask $mkdir flask-tutorial
flask $cd flask-tutorial

Open Directory In PyCharm

We can open that directory and add a hello.py file to get started writing Flask code.
hello world flask

Creating A Flask Application And Route

This code imports the Flask library, creates an app and defines the homepage for a simple web server.

from flask import Flask

app = Flask(__name__)


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

To run this application, we need to visit this same directory at the command prompt and use the command flask run and here is the result we get:

flask-tutorial $flask run
 * 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
Usage: flask run [OPTIONS]

Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.

Interesting. Flask needs an environment variable in order to run. This is easily fixed with the following command at the terminal.

flask-tutorial $export FLASK_APP=hello

Once this is in place, lets try to run the server again.

flask-tutorial $flask run
 * Serving Flask app 'hello' (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://127.0.0.1:5000/ (Press CTRL+C to quit)

This looks a little better! In addition, visiting the given URL in a web browser shows us that the route is working!

home page route python Flask


Using The Development Environment

When using the approach above, if you make a change or add new features to the Flask application, they will not be reflected in the running app. You would need to shut down the server, and then relaunch in order to import any changes or updates into the application. An easier approach is to use the development environment which automatically detects any changes or updates to the software, and reloads the server for you. We can do this via an environment variable like so.

flask-tutorial $export FLASK_ENV=development

Now we can run the server again, and notice that the output is different. The app is now being served as a development environment.

flask-tutorial $flask run
 * Serving Flask app 'hello' (lazy loading)
 * Environment: development
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 743-600-024
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

When you make a change to your application, you will see in the terminal that Flask detects that change and reloads accordingly.

 * Detected change in 'C:\\python\\flask\\flask-tutorial\\hello.py', reloading
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 743-600-024
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Checking the result in the browser shows we did in fact make a change to the code.

Flask Dev Env


Creating More Flask Routes

Adding routes is quite easy, all we need to do is use the Python decorator again followed by a new function. An ‘about’ route is shown highlighted here.

from flask import Flask

app = Flask(__name__)


@app.route('/')
def home():
    return 'Hello Wonderful People!'


@app.route('/about')
def about():
    return 'This is a tutorial Flask app on serving routes'

After adding this code, the development server expectedly notices the change and reloads the server for us. Also notice that when you visit the application in a web browser, you see the HTTP request in the command line.

 * Detected change in 'C:\\python\\flask\\flask-tutorial\\hello.py', reloading
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 743-600-024
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [20/May/2021 10:10:01] "GET /about HTTP/1.1" 200 -

adding more flask routes


app.py

As we saw above when running the flask application we needed to set an environment variable to specify which file is actually the root of the Flask application. Since our file is named hello.py, we used export FLASK_APP=hello to let Flask know this is the file to run when using the flask run command. An easier way to do this is to simply name the file app.py. Flask is smart enough to know inside of the directory if there is a file named app.py, then that is the default app that it should be running. So at this point, we should now have an app.py file with the following code that servers two unique routes: The homepage, and the about page.


app.py

from flask import Flask

app = Flask(__name__)


@app.route('/')
def home():
    return 'Hello Wonderful People!'


@app.route('/about')
def about():
    return 'This is a tutorial Flask app on serving routes'

Learn More About Flask Routes

This tutorial covered the basics of getting Python Flask installed and setting up your first few routes. To learn more about routes in Flask, see these additional resources below.