In this exercise, you will follow the initial parts of the FastAPI tutorial to get up-to-speed with some of its capabilities. FastAPI is a leading, modern framework for developing backend APIs in Python.
Unlike with Angular, where your work was effectively all loaded into a web browser and the browser was responsible for “running” the client application, with FastAPI you will be implementing server-side functionality. When your server is running, it is within the container. Using your web browser, or other tools such as curl
, you can then make requests to your server and the server will respond. Ultimately, this tutorial is showing you some of the framework’s capabilities but not really following the development of a “real” (fake) application.
When working through this tutorial, you should remember two important things. Ultimately, the functions you implement on the backend will be less trivial and will be responsible for persisting or retrieving data to and from a database. Additionally, more work will need to be done in the front-end of an application, like your Angular client, in order to depend upon a server API. In the next step of our check-in mini app, you will write code that creates requests from Angular to the API server. Then, once this is working, we will add data persistence in a data layer. Building systems typically involves many layers interacting with one another.
Chrome Extension
You will implement a backend API that uses JSON as its request and response body encoding format. To improve the viewing experience of JSON content in your web browser, you are encouraged to install the following popular Chrom JSON Formatter browser extension.
Development Container Setup
- Clone the repository we setup as a sandbox for this exercise: https://github.com/comp423-23f/ex02-sandbox.git
- This is a bare bones setup that includes the latest verison of
python
(3.11) andpip
, Python’s package manager. - Open the repository in a DevContainer. This will build the image.
Project Dependencies Setup
Like node.js
’s package.json
, Python’s requirements.txt
is a standard means for specifying external dependencies in Python. Here we are “pegging” the version on a specific major/minor version using semantic versioning.
Add a requirements.txt
file to the project’s root directory with the contents below:
fastapi[all] >=0.103.1, <0.104.0
The fastapi[all]
pip package has all the dependencies needed to get started with a FastAPI application, such as the performant Python web server that we’ll use to host the backend uvicorn
.
To install the dependencies in this file, we’ll use Python’s de facto package management tool pip
. Start a new terminal in your VSCode instance and run the following command:
pip install -r requirements.txt
The -r
option indicates the installation of dependencies will be guided by a file in the standard requirements.txt
format.
FastAPI Tutorial - User Guide
You are now ready to begin the FastAPI User Guide’s standard tutorial. For this exercise, there are a few things you should know while you are working through the following four sections:
- First Steps
- Path Parameters
- Query Parameters
- Request Body
First, the examples along these parts of the tutorial appear “destructive” in the sense that at each new step it looks like only one function and route remains in the application. For the purposes of this exercise, you should be adding and updating routes and functions where there are new routes demonstrated, not deleting routes (unless the tutorial is iterating on or improving upon the same route example).
Second, you should play around with examples the tutorial gives and modifying them not just mindlessly following along, which is easy to do. As you encounter words, concepts, or ideas you are unfamiliar with, you should search around and try to come to a cursory understanding of what it is referring to. Try asking ChatGPT to explain something to you!
Finally, there are Gradescope questions which check for understanding along the way and a few activity extensions to complete.
Submission
The only submission for this exercise is the Gradescope on-line assignment. You do not need to create a repository or push.