Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StaticFiles support for directories other than "statics" #1265

Closed
bram2000 opened this issue Aug 13, 2021 · 8 comments · Fixed by #1350
Closed

StaticFiles support for directories other than "statics" #1265

bram2000 opened this issue Aug 13, 2021 · 8 comments · Fixed by #1350
Labels
staticfiles Static file serving

Comments

@bram2000
Copy link

bram2000 commented Aug 13, 2021

Checklist

  • [/] There are no similar issues or pull requests for this yet.
  • [X - tried to get feedback but no replies] I discussed this idea on the community chat and feedback is positive.

Is your feature related to a problem? Please describe.

I want to be able to serve static files from arbitrary locations within a python package but I can't because currently it looks in a statics folder which is hardcoded.

Describe the solution you would like.

I'd like to be able to pass as a parameter the path within the python package to look in for the static files.

Describe alternatives you considered

I've considered changing the location of my packaged files to statics but this will have knock on effects across other systems, and statics itself is non-standard as far as I can tell.

Additional context

@JayH5 JayH5 added the staticfiles Static file serving label Aug 16, 2021
@BeatSlayer
Copy link

That's already supported.

Check the docs here: https://www.starlette.io/staticfiles/

@bram2000
Copy link
Author

That's already supported.

Check the docs here: https://www.starlette.io/staticfiles/

I don’t think it is. I want to use the package argument but specify a directory within that package instead of being forced to use statics

@stephane
Copy link

stephane commented Aug 24, 2021

@bram2000 yes, I don't know why this directory is hard-coded.
https://github.com/encode/starlette/blob/master/starlette/staticfiles.py#L76
Do you think the directory argument could be used?

There is also a copy/paste issue in the following assert message:
https://github.com/stephane/starlette/commit/4795244701298033169bcf1c0840e2badbd07dda

The line https://github.com/encode/starlette/blob/master/starlette/staticfiles.py#L56 prevents me to use a symbolic link for my static files... Thank you@HanDDol

@HanDDol
Copy link

HanDDol commented Sep 2, 2021

The line https://github.com/encode/starlette/blob/master/starlette/staticfiles.py#L56 prevents me to use a symbolic link for my static files...

@stephane
In my case, i added check_dir=FALSE in StaticFiles constructor and it works.

@stephane
Copy link

stephane commented Sep 8, 2021

@HanDDol oh, you're right! How could I have missed it? 🤣

@paulmsmith
Copy link

I've just hit this issue. I've tried the check_dir=False suggestion from @HanDDol but that didn't work for me. Do you have an example @HanDDol?

I think @bram2000's suggestion would be idea to account for packages that don't follow the 'statics' convention. (if it is a convention).

@paulmsmith
Copy link

I found that by simply creating an empty directory called statics inside the dependancy this then worked and seemed to allow me to reference things in static within the dependancy. This feels like bug to me and as @bram2000 said, would be helpful if you could pass a parameter specifying the directory within a dependancy you need to include things from.

@florimondmanca
Copy link
Member

Hi folks, we're considering #1350 and the following API to address this use case…

routes=[
    ...
    Mount('/static', app=StaticFiles(packages=['bootstrap4', ('mypackage', 'dist')]), name="static"),
]

That is, we'll be able to specify a root directory to look into as a 2-tuple (<package_name>, <dir>) form, where the single string "<package_name>" uses the current default, "statics".

Any thoughts or comments welcome on the PR before we move on and merge. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
staticfiles Static file serving
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants