The initial repository for PyLadies Hamburg's "Introduction to FastAPI" workshop.
The setup instructions assumes you already have the following:
- Git
- Python 3.8 (if you have an existing installation of 3.9, that works too)
- The code editor you are most comfortable using
Instructions are based on MacOS terminal but if you use Git Bash in Windows, the instructions should be fairly the same.
β are instructions or "checklist" to make sure that the environment is setup properly.π‘ are notes to explain what the step is about.
-
Clone this repository
a) In your terminal, executegit clone https://github.com/paxcodes/grocery_api
b) Still in the terminal, change your working directory:cd grocery_api
-
Create the virtual environment
-
Activate your virtual environment:
source .venv/bin/activate
1- β
Your terminal prompt will be prefixed with
(grocery_api)
once the virtual environment is activated. - β
Running
which pip
andwhich python
should output the.venv
directory inside yourgrocery_api
folder. (e.g./grocery_api/.venv/bin/pip
and/grocery_api/.venv/bin/python
) - β
Running
python --version
should output 3.8/3.9. If it isn't, delete your.venv
folder and repeat Step #2 onwards.
- β
Your terminal prompt will be prefixed with
-
Install dependencies:
pip install -r requirements.txt
- π‘ This installs fastapi, uvicorn, pytest, requests, etc. and their dependencies. When creating your own FastAPI project, you'd do
pip install fastapi
etc. - β
It should output something like,
Successfully installed ... fastapi-0.65.2 ... uvicorn-0.14.0 ... pytest-6.2.4 ... requests-2.25.1
- β
Running
which uvicorn
orwhich pytest
should output the.venv
directory inside yourgrocery_api
folder. (e.g./grocery_api/.venv/bin/uvicorn
and/grocery_api/.venv/bin/pytest
)
- π‘ This installs fastapi, uvicorn, pytest, requests, etc. and their dependencies. When creating your own FastAPI project, you'd do
-
Install the project:
pip install -e .
- π‘ This will install the grocery_api package locally. This is necessary to do for
pytest
to work. - β
It should output something like,
Successfully installed grocery-api-1.0
- π‘ This will install the grocery_api package locally. This is necessary to do for
-
Run tests in the terminal by typing,
pytest
- β
It should output,
... collected 25 items tests\test_endpoints.py . [ 4%] tests\crud\test_item.py ........... [ 48%] tests\crud\test_store.py ........... [ 92%] tests\crud\test_user.py .. [100%] ============== 25 passed in 0.XXs ===============
- β
It should output,
-
Run the server:
uvicorn grocery_api.main:app
- β
It should say,
INFO: Application startup complete. INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
- β
It should say,
-
Go to your browser and visit, http://127.0.0.1:8000
- β
It should say,
{ "message": "Hello World!" }
- β
It should say,
-
Exit the server by pressing
Ctrl+C
in the terminal.
If you have made it all the way to the end and everything is as expected (β ), you have successfully setup your environment and are ready for the workshop!
If you are having trouble with any of the steps, reach out in PyLadies' Slack channel #city-hamburg.
1: In Windows, this would be source .venv/Scripts/activate
I will show how to implement 3 paths or endpoints in the FastAPI framework. A path to:
- Get all items
- Get a specific item given an Item ID (with some data validation using type-hinting)
- Create an item resource (with some data validation using Pydantic's
BaseModel
class + type-hinting)
Attendees will have a chance after every section to create similar endpoints for the store data (e.g. Get all stores, get a specific store, and create a store). There is a cheatsheet available that may help when you go through the exercises / workshop and need a quick review of a concept.
If time allows, I will demonstrate:
- Validating "outgoing" data (e.g. automatically strip sensitive data before sending data back)
- Testing an endpoint