Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit df74992

Browse files
Views organized by category for larger app.
1 parent f71a7e4 commit df74992

File tree

6 files changed

+71
-11
lines changed

6 files changed

+71
-11
lines changed

‎code/ch4-templates/.idea/ch4-templates.iml‎

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎code/ch4-templates/main.py‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
import fastapi
2-
import uvicorn
32
import fastapi_chameleon
4-
from fastapi_chameleon import template
3+
import uvicorn
4+
5+
from views import account
6+
from views import home
7+
from views import packages
58

69
app = fastapi.FastAPI()
710

8-
fastapi_chameleon.global_init('templates')
11+
12+
def main():
13+
configure()
14+
uvicorn.run(app, host='127.0.0.1', port=8000)
15+
16+
17+
def configure():
18+
configure_templates()
19+
configure_routes()
20+
21+
22+
def configure_templates():
23+
fastapi_chameleon.global_init('templates')
924

1025

11-
@app.get('/')
12-
@template(template_file='index.html')
13-
def index(user: str = 'anon'):
14-
return {
15-
'user_name': user
16-
}
26+
def configure_routes():
27+
app.include_router(home.router)
28+
app.include_router(account.router)
29+
app.include_router(packages.router)
1730

1831

1932
if __name__ == '__main__':
20-
uvicorn.run(app)
33+
main()
34+
else:
35+
configure()
File renamed without changes.

‎code/ch4-templates/views/account.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import fastapi
2+
from fastapi_chameleon import template
3+
4+
router = fastapi.APIRouter()
5+
6+
7+
@router.get('/account')
8+
def index():
9+
return {}
10+
11+
12+
@router.get('/account/register')
13+
def register():
14+
return {}
15+
16+
17+
@router.get('/account/login')
18+
def login():
19+
return {}
20+
21+
22+
@router.get('/account/logout')
23+
def logout():
24+
return {}

‎code/ch4-templates/views/home.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import fastapi
2+
from fastapi_chameleon import template
3+
4+
router = fastapi.APIRouter()
5+
6+
7+
@router.get('/')
8+
@template()
9+
def index(user: str = 'anon'):
10+
return {
11+
'user_name': user
12+
}
13+
14+
15+
@router.get('/about')
16+
def about():
17+
return {}

‎code/ch4-templates/views/packages.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import fastapi
2+
3+
router = fastapi.APIRouter()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /