My folder structure looks like this:
|--/workspace/myapp/src/myapp
| |--__init__.py
| |--web
| |--__init__.py
| |--main.py
| |--templates
| |--index.html
I start script looks like this:
uvicorn --app-dir /workspace/myapp/src/myapp/web main:app --host 0.0.0.0 --port 5000
I have the following in main.py
app = FastAPI()
app.include_router(data_view.router)
app.include_router(data_mgr_view.router)
#app.mount("/static", StaticFiles(directory="static"), name="static")
tmplts = Jinja2Templates(directory="./templates")
@app.get('/', response_class=HTMLResponse)
async def root(request: Request):
context = {"title":"My App", "version":f"[v-{os.environ.get('VERSION','version missing')}]"}
return tmplts.TemplateResponse(request=request, name="index.html", context=context)
When i hit the root url, i get:
File "/usr/local/lib/python3.12/site-packages/jinja2/loaders.py", line 126, in load
source, filename, uptodate = self.get_source(environment, name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/jinja2/loaders.py", line 207, in get_source
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: index.html
Is this because i am specifying the entire path when starting the app and so am not actually in the web folder? (i have had problems running python from command line, it just doesnt seem to pick up PYTHONPATH, which already has that path).
If so, is the solution here to use the full path when creating the Jinja2Templates varaible?
Or should i change to that path in my startup script first?
What is the best practice here?
my concern is the paths in dev vs prod are different – so care would have to be taken to make sure right path is used in right environment – as its another moving ‘part’ to worry about.