question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Static files broken

See original GitHub issue

Describe the bug I want to expose api endpoint from which I can download static directory. I get 2 unexpected behaviors:

  1. When I want to reach my endpoint 0.0.0.0/static I see No operations defined in spec!
  2. From tutorials(mentioned below) I understood it takes file from my app directory? Unless I copy it to root of my OS I get error raise RuntimeError(f"Directory '{directory}' does not exist").

This is really important for my home project to make sense so any input is really appreciated because I’m stuck on this.

To Reproduce Code:

from starlette.staticfiles import StaticFiles
from fastapi import FastAPI

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

Dockerfile:

FROM python:3.7
RUN pip install aiofiles fastapi uvicorn python-multipart 
EXPOSE 80
COPY ./app /app
COPY ./static /static

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]

I use docker build -t myimage . and docker run -p 80:80 myimage.

Expected behavior Download static directory(or at least any file from this dir) from 0.0.0.0/static

Environment: Docker above.

Already viewed and tried issues/tutorials https://fastapi.tiangolo.com/tutorial/static-files/ https://www.starlette.io/staticfiles/ https://github.com/tiangolo/fastapi/issues/130

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
dmontagucommented, Jan 30, 2020

@yaronv you might be able to pass absolute paths without needing to actually hard code an absolute path by doing something like:

from pathlib import Path

current_file = Path(__file__)
current_file_dir = current_file.parent
project_root = current_file_dir.parent
project_root_absolute = project_root.resolve()
static_root_absolute = project_root_absolute / "static"  # or wherever the static folder actually is

Obviously you can reduce the number of lines involved, I just made it more verbose to make it clear what was going on.

Or you could also use an environment variable/config as you noted.

7reactions
mschorchtcommented, Dec 5, 2019

Even if this answer comes a little late, maybe it helps others, because I got into the same problem in the beginning.

I am not sure if this is a bug either? Because it’s called static file and not static directory.

So you won’t be able to request a folder, only a file, which has to be explicit.

0.0.0.0/static/existing_file

if you want to get files as folder you could zip your files to one file

0.0.0.0/static/existing_files.zip

If the error result is that the folder was not found, then a wrong specification must have happened. The following should run, if you want static inside you app-folder:

from starlette.staticfiles import StaticFiles
from fastapi import FastAPI

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

So you don’t need to copy the static folder in dockerfile additionaly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

my template is broken and cannot get static files - Stack Overflow
You need to add both debug-time url patterns for static and media: urlpatterns += static(settings.MEDIA_URL, document_root=settings.
Read more >
Trouble debugging static files not working with the local server ...
My issue is fairly simple: when doing ./manage.py runserver localhost:8080 none of the files in my static directory are being served, which is ......
Read more >
53331 – Broken http response for 200k+ static files
This is ASF Bugzilla: the Apache Software Foundation bug system. In case of problems with the functioning of ASF Bugzilla, please contact ...
Read more >
Django can't see my static files - Google Groups
from django.contrib.staticfiles.urls import staticfiles_urlpatterns ... But it is still not working. Do I have a syntax error somewhere?
Read more >
Django static files not working - Render community
Edit: i got it to “work”, it shows images or sometimes only the css. But constantly gives 500 server error. Static config: STATIC_URL...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found