This project is a simple demo of how to create a basic REST API with Python 3 using:
Prerequisites
Docker
installed on your computerDocker daemon
running
Steps
- build a Docker image for the REST API service:
docker build . -t my-image
- start a Docker container from the image:
docker run -p 80:80 my-image
Usage
You can now send requests to your REST API at http://localhost:80
. The
documentation of the REST API is available at http://localhost:80/docs
. For example, using curl
command on Windows prompt:
curl -H "Content-Type: application/json" -d @./data/example.json http://localhost:80/api/v0/run
Prerequisites
python==3.9
andpip
are installed on your computer
Steps
-
open a terminal, and move to the project's root folder
-
create and activate a virtual environment for the project (commands for Windows only):
python -m venv myenv
myenv\Scripts\activate
- make sure you have the latest versions of
pip
andsetuptools
libraries installed:
pip install --upgrade pip
pip install --upgrade setuptools
- install Python libraries that are required to run the app':
pip install -r requirements.txt
- install the project sources:
pip install -e .
- launch the REST API on local host with:
uvicorn src.api:app --host 0.0.0.0 --port 80
Usage
You can now send requests to the REST API at http://localhost:80
. The
documentation of the REST API is available at http://localhost:80/docs
. For example, using curl
command on Windows prompt:
curl -H "Content-Type: application/json" -d @./data/example.json http://localhost:80/api/v0/run
You can also pass additional query parameters to the REST API, for example:
curl -H "Content-Type: application/json" -d @./data/example.json http://localhost:80/api/v0/run?some_param=0.2
- publish the Docker image on your Docker Hub registry:
docker image tag my-image nathaliesaintgeours/my-api:latest
docker push nathaliesaintgeours/my-api:latest
The push refers to repository [docker.io/nathaliesaintgeours/my-api]
3e27173a8d9a: Pushed
(...)
latest: digest: sha256:c0264ad47ef1921aec7d456f496cb8e2b3dab4578f028c79ed1576ea10c7a537 size: 2845
That's it! The Docker image of the REST API is now uploaded on Docker Hub. You can now pull this image from a remote server to deploy the REST API wherever you need it.