0
votes

I know how to set a favicon for a html page. I was wondering if it is possible to set a favicon for all "pages" from my fastapi project? So when I do post and get requests in a browser, a favicon shows. I hope that makes sense?

Thanks, Chris

1
That would depend on how you serve your HTML; that would have a reference to the favicon and the browser would load that file. If you need FastAPI to serve the static resource, look at the static file support of FastAPI; otherwise - please elaborate what you're missing and how you're servering your HTML now.MatsLindh
Do I need to serve any sort of html at all for it to work? I thought there might be a way to make a favicon appear when doing plain url based post and get requests in a browser.Chris
A browser would by default make a request for /favicon.ico in that case; you'll need to serve a file under that path if that's what you're looking for (this will assume that your endpoint returns the actual answer to the request). You could add a @router.get('/favicon.ico') and return a FileResponse in that view: fastapi.tiangolo.com/advanced/custom-response/#fileresponseMatsLindh

1 Answers

0
votes

just return a FileResponse in the get request to '/favicon.ico'

tree

root
|-main.py
|-favicon.ico

main.py

from fastapi import FastAPI
from starlette.responses import FileResponse

app = FastAPI()
favicon_path = 'favicon.ico'

@app.get('/favicon.ico')
async def favicon():
    return FileResponse(favicon_path)