Add example app using FastAPI and this project.

This commit is contained in:
Michael Kennedy
2024-11-10 09:54:31 -08:00
parent 6446c07d99
commit c6f9747d3d
6 changed files with 75 additions and 0 deletions

View File

@ -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
View 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
View File

@ -0,0 +1,8 @@
body {
padding: 20px;
background-color: #fafafa;
}
h1, p {
text-align: center;
}

View 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>

View 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>

View File

@ -5,3 +5,4 @@ pytest-clarity
twine
hatchling
hatch-vcs>=0.3.0
uvicorn