Skip to content

Instantly share code, notes, and snippets.

@trohit
Created July 6, 2023 02:30
Show Gist options
  • Save trohit/75b132938cccd4324f674cb304ddf087 to your computer and use it in GitHub Desktop.
Save trohit/75b132938cccd4324f674cb304ddf087 to your computer and use it in GitHub Desktop.
FastAPI + serve inline image
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# https://stackoverflow.com/questions/21531061/image-url-downloads-the-image-instead-of-displaying-it
from fastapi import FastAPI
from starlette.responses import FileResponse
app = FastAPI()
@app.get("/image")
async def get_image():
#filename = "boat.jpg" # Replace with the path to your image file
filename = "qr.png" # Replace with the path to your image file
return FileResponse(filename, media_type="image/jpeg")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment