Small practical break¶
Objective: Set up an API allowing access to CSV file content
You have https://
And you have https://
The backend API must imperatively respect the routes documented in the README.
tip for copying repositories
to copy the content of these repositories on your machine, rather than using git clone, you can use this
# if necessary (npx command not found)
# conda install conda-forge::nodejs
# download the repo in the 'frontend' folder
npx degit git@github.com:ue22-p25/backend-apitester-frontend.git frontend
# same for the backend
npx degit git@github.com:ue22-p25/backend-apitester-skeleton.git backendwhich has the advantage of not recreating a git repository in the created folder; especially if you place yourself in an already existing repository like backend-homework
(but doesn’t prevent you from git add the result immediately)
Tip #1: auto-reload¶
you notice that FastAPI applications do not contain code to execute directly
(just route definitions)so if you launch the python file with
python my_file.py, it does nothing!that’s why it’s essential to launch the app with
fastapi dev apitester.pyalso and especially, the server restarts by itself at each code modification
and same for the frontend by the way; if you intend to touch it, it’s better to launch it with vite, so it restarts by itself at each modification
Tip #2: typed parameters¶
a route can take a parameter, possibly typed
# untyped parameter
@app.route('/hello/<name>')
def hello(name):
# here name is a simple str
# it's up to you
# to check its content
return f'Hello, {name}!'# here the parameter is typed
@app.route('/hello/<int:id>')
def hello(id):
# so fastapi does the
# control and conversion
# automatically
return f'Hello, {id**2}!'Tip #3: return types¶
to shorten the code, the return type of a route implies automatic processing
we hardly need to convert objects to dict/json
especially if we use Pydantic models (we’ll talk about it again...)
| Route return | HTTP response |
|---|---|
dict or list or int or float | Automatically encoded in JSON. |
str | Sent as raw text (text/plain) |
Pydantic BaseModel | JSON automatically. |
Example: return Item(name="Apple", price=1.5) → {"name":"Apple","price":1.5} |
Tip #4: httpie¶
it’s practical to have a real frontend in HTML/CSS/JS
BUT for development it’s useful to test also the routes in command line in the terminal
for this we can use
httpie(orcurlbut it’s less readable)which installs with
pip install httpieand which is used like this
# a GET http GET http://localhost:8000/hello # or abbreviated http :8000/hello # a POST http POST http://localhost:8000/hello var=value # by the way with an assignment of this type the POST is automatic # which makes the following command equivalent http :8000/hello var=valueand as always, do
http --helpor see the doc for more details...