I have a temp.py file and I have used FastAPI to return string or a dictionary with 2 get methods one for string another for dictionary.
I also have a temp.html file inside templates folder.
I am using Jinja2Templates as the template engine in HTML as the frontend view.
If the output result from FastAPI is string, I just want to display, as it is. But if the output variable is dictionary, I want to iterate over it and print each key and value pair in a new line.
I have tried this piece of code but I am getting Internal Server error while calling the get method for printing dictionary output.
Backend
from fastapi.templating import Jinja2Templates
from fastapi import FastAPI, Request
import uvicorn
app = FastAPI()
templates = Jinja2Templates(directory="templates")
@app.get("/hello")
async def form_get(request: Request):
    output = "HELLO"
    return templates.TemplateResponse('temp.html', context={'request': request, 'result': output})
@app.get("/dic")
async def form_post(request: Request):
    test = {1: 56, 2: 45, 3: 46, 4: 35, 5: 69}
    return templates.TemplateResponse('temp.html', context={'request': request, 'result': test})
if __name__ == "__main__":
    uvicorn.run("temp:app", reload=True)
Frontend
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
</head>
<body>
<p>
    {% if result == "HELLO" %}
        {{ result }}
    {% else %}
        {% for key, value in result.item() %}
            {{ key }} : {{ value }}
        {% endfor %}
    {% endif %}
</p>
</body>
</html>
 
    