Python + Flask - Part 1 - Web API

Oct 19, 2019

titulo

This is the first part of a series about the Flask framework, a common tool used to create web applications with Python.

Objectives

In this series you will learn how to create a web API in Python, make requests and connect it with local and remote databases.

The part 1 will focus on the virtual environment to install Flask and how to run a web API.
The full example is available here: Python-Flask.

Before Start

If you are new to Python, read these articles to get started: Python for Beginners.

Topics

venv

Create a folder for your project, open VSCode and execute the command below to create a virtual environment (venv):

python -m venv venv

Confirm any popup that shows up and run the command below to use the venv:

.\venv\Scripts\Activate.ps1

Create a file named ‘requirements.txt’ containing the word flask.

venv01

Install the flask package inside the venv with the following command:

pip install -r requirements.txt

The result will be:

venv02

API

Create a folder called api with a file called api.py. Inside the file, write this code:

flask01

Run the following command:

python .\api\api.py

The result will be:

flask02

If you follow the URL displayed you will see the web page running:

http://127.0.0.1:5000/

flask03

Next

We will create the HTTP methods to turn our web application into a web API.

References


Categories:
Tags: