Skip to content

Commit bb06f17

Browse files
committed
Rename folders
Python does not allow using numbers at the start of a module name it seems. Also tidy up comments in the current api files.
1 parent 9e78cd7 commit bb06f17

File tree

7 files changed

+51
-14
lines changed

7 files changed

+51
-14
lines changed

building-with-fast-api/01-hello-world/api.py

-12
This file was deleted.

building-with-fast-api/README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ Whereas Elm has a central `Model` (generally) to work from and uses modules and
1010

1111
> These are annoying and compared to Elm (where everything just works), not particularly user-friendly. You'd think you could just run commands once you're set up with a `venv` (virtual environment).
1212
13-
1. `uvicorn` command [won't run](https://stackoverflow.com/a/69322150)
13+
1. Module naming clashes and `venv`:
14+
- `uv` commands require calling from the `venv` parent directory
15+
- `01` numbers are invalid for module names
16+
2. `uvicorn` command [won't run](https://stackoverflow.com/a/69322150)
1417
- Preface it with `uv run` (equivalent to `python -m`)
15-
2. Using Thonny as an IDE
18+
3. Using Thonny as an IDE
1619
- I can get the version of Python running but the other stuff is harder
1720

1821
## Tools
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from fastapi import FastAPI
2+
3+
# ------------------------------------------------------------------------------
4+
# Hello World!
5+
# ==============================================================================
6+
# The most basic route we can make. `app` uses an instance of the `FastAPI` class
7+
# and we return a dictionary. The problem with using the `FastAPI()` instance is
8+
# that it can only run ONE route.
9+
#
10+
# For multiple routes, use the `APIRouter` class.
11+
#
12+
# Running the server
13+
# ------------------
14+
# `uv run uvicorn 01-hello-world.api:app --port 8000 --reload` (from parent dir)
15+
16+
app = FastAPI()
17+
18+
@app.get("/")
19+
async def welcome() -> dict:
20+
return { "message": "Hello World" }
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from fastapi import FastAPI
2+
from chapter_02.todo import router
3+
4+
# ------------------------------------------------------------------------------
5+
# A to do app
6+
# ==============================================================================
7+
# Uvicorn cannot use the `APIRouter()` instance directly to serve the application,
8+
# like we did with `uv run uvicorn api:app --port 8000 --reload`. We've got to
9+
# add these routes to the `FastAPI()` instance to enable their visibility.
10+
#
11+
# Include router
12+
# --------------
13+
# `include_router(router, ...)` method is responsible for adding routes defined
14+
# with `APIRouter` class to the main application's instance.
15+
#
16+
# - `app` variable now has an instance of the `FastAPI` class.
17+
# - `.include_router` is a method of this class
18+
19+
app = FastAPI()
20+
21+
@app.get("/")
22+
async def welcome() -> dict:
23+
return { "message": "Hello Buddy!" }
24+
25+
app.include_router(router) # includes TO DO routes (instance of `APIRouter()`)

building-with-fast-api/notes/Anki.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ You can rip a lot of this stuff from the book, but put it in very simple terms.
1212
- Basic syntax differences
1313
- `function()` calls and arguments
1414
- Typing (just a taste)
15+
- Python [comments](https://realpython.com/python-comments-guide/)
1516
- The `app` variable (I guess this holds the state)
1617
- Models (Elm has one master model)
1718
- Decorators

0 commit comments

Comments
 (0)