1

By default, Flask allows one level of nesting within the Static Folder. For instance

->static <BR>
---->css <BR>
------->images<BR>

We can access files within the css directory through the browser.

e.g. http://localhost:5000/static/css/file1.css

How can we access the files within images directory through the browser ?

Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
Neeleshkumar S
  • 746
  • 11
  • 19

2 Answers2

1

Flask sets up the route for static files using your static path (defaults to /static) and any text. From the source:

self.add_url_rule(self.static_url_path + '/<path:filename>',
    endpoint='static',
    view_func=self.send_static_file)

send_to_static passes whatever this route assigns to filename.

return send_from_directory(self.static_folder, filename,
    cache_timeout=cache_timeout)

If you look at the source for send_from_directory you will see that it just uses safe_join(directory, filename) to get the path to the file.

safe_join itself just does some work to normalize the path and watch out for things like .. in the filename.

Going back to the URL route, path matches any text. It's like string except it accepts slashes. This means that you can use any level of nesting within your static folder. URLs like /static/file.txt and /static/p/a/t/h/t/o/file.txt will all work. So long as the URLs begin with your static path, the behavior you want works out of the box:

http://localhost:5000/static/css/images/image1.png
dirn
  • 19,454
  • 5
  • 69
  • 74
0

Not sure, where you find, Flask allows just one level of nesting.

Just try it.

I have directory ./static/html/jobs.html and I get this served on url http://localhost:5000/static/html/jobs.html.

Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98