FastAPI Assets


1. Serve Static Files:

from fastapi import FastAPI, staticfiles
from fastapi.staticfiles import StaticFiles

app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")

2. Serve JavaScript (JS):

from fastapi import FastAPI
from fastapi.responses import FileResponse

app = FastAPI()

@app.get("/js/{file_name}")
async def get_js(file_name: str):
    return FileResponse(f"js/{file_name}")

3. Host a CSS File:

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()

@app.get("/css/{file_name}")
async def get_css(file_name: str):
    with open(f"css/{file_name}", "r") as f:
        return HTMLResponse(f.read())

4. Serve Images:

5. Host a JSON File:

6. Serve HTML Files:

7. Host a Markdown File:

8. Serve PDFs:

9. Host a Video:

10. Serve Audio:

11. Host a Font:

12. Serve a ZIP File:

13. Host a 7z Archive:

14. Serve a RAR Archive:

15. Host a TAR File:

16. Serve a GZ File:

17. Host a BZ2 File:

18. Serve a XZ File:

19. Host a CSV File:

20. Serve a TSV File:

21. Host a Text File:

22. Serve an XML File:

23. Host a YAML File:

24. Serve a TOML File:

25. Host a JSON5 File:

26. Serve a LICENSE File:

27. Host a README File:

28. Serve a CONTRIBUTORS File:

29. Host a Changelog File:

30. Serve a Dockerfile File:

31. Host a Makefile File:

32. Serve a Setup File:

33. Host a Requirements File:

34. Serve a Pipfile File:

35. Host a Poetry File:

36. Serve a Git Ignore File: