Skip to content

Commit 66e46a6

Browse files
committed
ch 21 and 22: updated examples
1 parent 1702717 commit 66e46a6

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
"""
2-
uvicorn main:app --reload
3-
"""
4-
51
import pathlib
62
from unicodedata import name
73

@@ -11,28 +7,30 @@
117

128
from charindex import InvertedIndex
139

14-
app = FastAPI(
10+
app = FastAPI( # <1>
1511
title='Mojifinder Web',
1612
description='Search for Unicode characters by name.',
1713
)
1814

19-
class CharName(BaseModel):
15+
class CharName(BaseModel): # <2>
2016
char: str
2117
name: str
2218

23-
def init(app):
19+
def init(app): # <3>
2420
app.state.index = InvertedIndex()
2521
static = pathlib.Path(__file__).parent.absolute() / 'static'
2622
with open(static / 'form.html') as fp:
2723
app.state.form = fp.read()
2824

29-
init(app)
25+
init(app) # <4>
26+
27+
@app.get('/search', response_model=list[CharName]) # <5>
28+
async def search(q: str): # <6>
29+
chars = app.state.index.search(q)
30+
return ({'char': c, 'name': name(c)} for c in chars) # <7>
3031

31-
@app.get('/', response_class=HTMLResponse, include_in_schema=False)
32+
@app.get('/', # <8>
33+
response_class=HTMLResponse,
34+
include_in_schema=False)
3235
def form():
3336
return app.state.form
34-
35-
@app.get('/search', response_model=list[CharName])
36-
async def search(q: str):
37-
chars = app.state.index.search(q)
38-
return [{'char': c, 'name': name(c)} for c in chars]

0 commit comments

Comments
 (0)