In part of a jinja2 template I am including pages from an old static website.
template.html
{{% old_page | safe %}}
Now, old_page.html can contain images
<!DOCTYPE html>
<html>
<head></head>
<body>
<img src="pictures/image1.jpg" alt="Image 1">
</body>
</html>
Since the old website is in some other location, the paths in src are not going to work.
In the FastAPI app I can check when the browser is asking for an image and resolve the correct path.
Question: What is the right way to make the response that sends the image such that the browser receives them as if they are the embedded images and can render them in the right places of the old page?
run.py
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
templates = Jinja2Templates(directory='templates')
app = FastAPI()
@app.get("{rest_of_path:path}")
def serve_my_app(request: Request, rest_of_path: str):
if asking_for_image(rest_of_path):
correct_path = find_correct_path(rest_of_path)
return None # What is the right way to make this response?
elif asking_for_old_page(rest_of_path):
old_page = get_old_page(rest_of_path)
return templates.TemplateResponse(
"template.html",
{
"old_page": old_page,
}
)
if __name__ == '__main__':
import os
import uvicorn
uvicorn.run(
"run:app",
host=os.getenv('HOST', '127.0.0.1'),
port=os.getenv('PORT', 45678),
reload=True,
)