mirror of
https://github.com/mikeckennedy/fastapi-chameleon.git
synced 2025-08-16 04:10:38 +08:00
Add example app using FastAPI and this project.
This commit is contained in:
2
.idea/fastapi-chameleon.iml
generated
2
.idea/fastapi-chameleon.iml
generated
@ -2,6 +2,7 @@
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/example" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="Python 3.13 (fastapi-chameleon)" jdkType="Python SDK" />
|
||||
@ -16,6 +17,7 @@
|
||||
<option name="TEMPLATE_FOLDERS">
|
||||
<list>
|
||||
<option value="$MODULE_DIR$/tests/templates" />
|
||||
<option value="$MODULE_DIR$/example/templates" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
|
39
example/example_app.py
Normal file
39
example/example_app.py
Normal file
@ -0,0 +1,39 @@
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
|
||||
import fastapi
|
||||
import uvicorn
|
||||
|
||||
import fastapi_chameleon
|
||||
|
||||
app = fastapi.FastAPI()
|
||||
|
||||
|
||||
@app.get("/")
|
||||
@fastapi_chameleon.template('index.pt')
|
||||
def hello_world():
|
||||
return {'message': "Let's go Chameleon and FastAPI!"}
|
||||
|
||||
|
||||
@app.get('/async')
|
||||
@fastapi_chameleon.template('async.pt')
|
||||
async def async_world():
|
||||
await asyncio.sleep(.01)
|
||||
return {'message': "Let's go async Chameleon and FastAPI!"}
|
||||
|
||||
|
||||
def add_chameleon():
|
||||
dev_mode = True
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parent
|
||||
template_folder = (BASE_DIR / 'templates').as_posix()
|
||||
fastapi_chameleon.global_init(template_folder, auto_reload=dev_mode)
|
||||
|
||||
|
||||
def main():
|
||||
add_chameleon()
|
||||
uvicorn.run(app, host='127.0.0.1', port=8000)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
8
example/static/site.css
Normal file
8
example/static/site.css
Normal file
@ -0,0 +1,8 @@
|
||||
body {
|
||||
padding: 20px;
|
||||
background-color: #fafafa;
|
||||
}
|
||||
|
||||
h1, p {
|
||||
text-align: center;
|
||||
}
|
12
example/templates/async.pt
Normal file
12
example/templates/async.pt
Normal file
@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
<link rel="stylesheet" href="/static/site.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hello async world</h1>
|
||||
<p>Your async message is <strong>${message}</strong></p>
|
||||
</body>
|
||||
</html>
|
13
example/templates/index.pt
Normal file
13
example/templates/index.pt
Normal file
@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
<link rel="stylesheet" href="/static/site.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hello world</h1>
|
||||
<p>Your message is <strong>${message}</strong>
|
||||
<a href="/async">See the async example</a> too.</p>
|
||||
</body>
|
||||
</html>
|
@ -5,3 +5,4 @@ pytest-clarity
|
||||
twine
|
||||
hatchling
|
||||
hatch-vcs>=0.3.0
|
||||
uvicorn
|
||||
|
Reference in New Issue
Block a user