-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
43 lines (35 loc) · 1.47 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import uvicorn
from fastapi import FastAPI, Request, Form
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import cangjie
app = FastAPI()
templates = Jinja2Templates(directory='templates')
@app.get('/', response_class=HTMLResponse)
async def read_index(request: Request):
return templates.TemplateResponse('index.html',
context={
'request': request,
'result': ''
})
@app.post('/')
async def form_post(request: Request, search_query: str = Form(' ')):
print(search_query)
if search_query == ' ':
print('No search query')
return templates.TemplateResponse('index.html', {
'request': request,
'search_query': ' ',
'result': ''
})
else:
result = cangjie.cangjie(search_query)
return templates.TemplateResponse('index.html',
context={
'request': request,
'search_query': search_query,
'result': result
})
if __name__ == '__main__':
uvicorn.run('main:app', host='127.0.0.1', port=8000, reload=True)