2

Hi I'm trying to link to an json file from html code using fastapi and jinja2 but the link is not working. I guess i have to tell fastapi how to find the file or something like that.. I get an json (api) answer instad of the file..

{"detail":"Not Found"}

The python code:

from fastapi import FastAPI, Form, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates

app = FastAPI()

templates = Jinja2Templates(directory='templates')


@app.get('/data', response_class=HTMLResponse)
async def data(request: Request):
    return templates.TemplateResponse('data.html', {
        'request': request
    })

And the HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.css">
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.js"></script>
        <title>Data</title>
    </head>
    <body>
        <div class="ui container">
            <h1>
                Data downloader:
            </h1>
            <a href="../json/data.json" target="_blank">
                Data
            </a>
        </div>

    </body>
</html>
3
  • I would bet the href is wrong. If it's a path to a route, you don't need the .., the same if the data.json file is in the same folder Commented Jan 28, 2021 at 14:03
  • @lsabi No... The path is correct and the url that opens in the new tab is the path where the data.json is located.. If I run the html directly without using fastapi and uvicorn the lik works... So it must be in the fastapi module i have missed something.. Commented Jan 28, 2021 at 21:05
  • 1
    Try sharing your folder structure. Fastapi may be serving only a certain folder as static and the rest pass it through the internal routers Commented Jan 29, 2021 at 8:22

1 Answer 1

4

This code did it..

python:

from fastapi import FastAPI, Form, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
app = FastAPI()
app.mount("/json", StaticFiles(directory="json"), name="json")

templates = Jinja2Templates(directory='templates')


@app.get('/data', response_class=HTMLResponse)
async def data(request: Request):
    return templates.TemplateResponse('data.html', {
        'request': request
    })

HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.css">
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.js"></script>
        <title>Data</title>
    </head>
    <body>
        <div class="ui container">
            <h1>
                Data downloader:
            </h1>
            <a href="{{ url_for('json', path='/data.json') }}" target="_blank">
                Data
            </a>
        </div>

    </body>
</html>

So it was the import and use of StaticFiles that first code dint have

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.