File tree Expand file tree Collapse file tree 4 files changed +50
-0
lines changed
Expand file tree Collapse file tree 4 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ fastapi
Original file line number Diff line number Diff line change 1+ import jinja2
2+ from fastapi import FastAPI , Request
3+
4+ environment = jinja2 .Environment ()
5+ template = environment .from_string ("Hello, {{ name }}!" )
6+
7+
8+ async def on_fetch (request , env ):
9+ import asgi
10+
11+ return await asgi .fetch (app , request , env )
12+
13+
14+ app = FastAPI ()
15+
16+
17+ @app .get ("/" )
18+ async def root ():
19+ message = "This is an example of FastAPI with Jinja2 - go to /hi/<name> to see a template rendered"
20+ return {"message" : message }
21+
22+
23+ @app .get ("/hi/{name}" )
24+ async def say_hi (name : str ):
25+ message = template .render (name = name )
26+ return {"message" : message }
27+
28+
29+ @app .get ("/env" )
30+ async def env (req : Request ):
31+ env = req .scope ["env" ]
32+ return {
33+ "message" : "Here is an example of getting an environment variable: "
34+ + env .MESSAGE
35+ }
Original file line number Diff line number Diff line change 1+ markupsafe
2+ jinja2
Original file line number Diff line number Diff line change 1+ name = " fastapi-worker"
2+ main = " src/worker.py"
3+ compatibility_flags = [" python_workers" ]
4+ compatibility_date = " 2025-03-16"
5+
6+ [vars ]
7+ MESSAGE = " My env var"
8+
9+ [[rules ]]
10+ globs = [" vendor/**" ]
11+ type = " Data"
12+ fallthrough = true
You can’t perform that action at this time.
0 commit comments