Categories
Flask

Getting Started with Python Web Development with Flask

Spread the love

Flask is a simple web framework written in Python.

In this article, we’ll look at how to develop simple Python web apps with Flask.

Create the Project

First, we need to install Python 3 if it’s not installed already.

It comes with scripts to create a virtual environment so we don’t have to install anything else.

We can create a project by running:

$ mkdir flask-app
$ cd flask-app
$ python3 -m venv venv

on Linux.

We create the project folder and the virtual environment.

On Windows, we create the flask-app project folder and inside it, we run:

$ py -3 -m venv venv

to create the virtual environment.

Activate the Environment

We then activate the virtual environment.

To do that, we run:

$ . venv/bin/activate

in the project folder in Linux.

On Windows, we run:

> venvScriptsactivate

to do the same thing.

Install Flask

Once we install the virtual environment, we run:

$ pip install Flask

to install the Flask package.

A Minimal App

Now in the flask-app project folder, we create app.py and write:

app = Flask(__name__)

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

to create a hello world app.

Flask(__name__) creates the Flask app.

@app.route is a decorator to add the route.

We return our response to as text in the hello_world function.

Then to run our app, we run:

$ export FLASK_APP=app.py
$ flask run

to run our app on Linux.

On Windows, we run:

> set FLASK_APP=app.py
> python -m flask run

to set the FLASK_APP environment variable and run our app.

The FLASK_APP environment variable should be set to the file name of the entry point.

Now we can go to http://127.0.0.1:5000/ in the browser and see ‘hello world’ displayed on the screen.

Debug Mode

We can turn on debug mode by setting the FLASK_END environment variable to development .

We can run:

export FLASK_ENV=development

to do that on Linux.

On Windows, we run:

set FLASK_ENV=development

to do the same thing.

Routing

We can add more than one route into our app.

For example, we can write:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return 'Welcome'

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

Then when we go to http://127.0.0.1:5000/, we see ‘Welcome’.

And when we go to http://127.0.0.1:5000/hello, we see ‘Hello, World’.

Routing with Variables

We can make our routes more useful with variables.

For example, we can write:

from flask import Flask
from markupsafe import escape
app = Flask(__name__)

@app.route('/user/<username>')
def show_user_profile(username):
    return 'User %s' % escape(username)

@app.route('/post/<int:post_id>')
def show_post(post_id):
    return 'Post %d' % post_id

@app.route('/path/<path:subpath>')
def show_subpath(subpath):
    return 'Subpath %s' % escape(subpath)

to create an app with routes that take route parameters.

<username> is the URL parameter placeholder.

We can access it with the username parameter.

escape escapes the URL parameter so we can use it safely.

We specified the data type it takes with the int and path substrings.

int is an integer. path is a path string.

So if we go to http://localhost:5000/path/foo/bar, we get ‘Subpath foo/bar’.

And if we go to http://localhost:5000/post/2, we get ‘Post 2’.

And if we go to http://localhost:5000/user/foo, we get ‘User foo’.

We can specify the following types:

  • string — accepts any text without a slash (default)
  • int — accepts positive integers
  • float — accepts positive floating-point values
  • path — like string but also accepts slashes
  • uuid — accepts UUID strings

Conclusion

We can create simple apps with ease with Flask.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *