0

My current working directory is /home/mark/a/b and my image resides in /home/mark/a/b/c/static, ie the absolute path of the image is /home/mark/a/b/c/static/image.gif.

I am using the python function send_static_file to return an image from an endpoint via a GET request.

My question is, why is it that I don't need to specify the full path of where the image is, in fact all I need to do is specify the image file name and it is able to retrieve the image and send it to the browser? Does send_static_file automatically search for the static folder?

app = Flask(__name__, static_url_path='/static')

return app.send_static_file('image.gif')

If I specify anything more than just the image file name itself while I curl the endpoint I result in an error show below.

{
    "message": "The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again. You have requested this URI [/event/email/] but did you mean /event/email/ or /event ?"
}

I saw a post (first answer) somewhat answering my question Flask: How to serve static html?. But I am not quite sure what it means when they say,

static_url_path changes the URL static files are available at, not the location on the filesystem used to load the data from.

What's the difference between location on the file system and the URL static files are available at?

Mark
  • 3,138
  • 5
  • 19
  • 36
  • where is `@app.route()` and function ? What url do you used in browser to get it? What command did you use with curl? – furas Dec 05 '19 at 20:11

1 Answers1

0

What's the difference between location on the file system and the URL static files are available at?

URL routes should not be coincide with paths on the server file system. You can specify both static_url_path and static_folder params at Flask app initialization for the following purposes:

  • static_url_path - can be used to specify a different path for the static files on the web (description from Flask documentation). E.g. you specified some url prefix for your server (e.g. localhost:8000/some_prefix/...), so that all of your server routes will be available at this prefix. In that case static_url_path should be also specified with this prefix: some_prefix/static
  • static_folder - the folder with static files that should be served at static_url_path. Defaults to the 'static' folder in the root path of the application (description from Flask documentation). In other words it is the path to folder with static files on your server file system.

My question is, why is it that I don't need to specify the full path of where the image is, in fact all I need to do is specify the image file name and it is able to retrieve the image and send it to the browser?

Because Flask will search for specified file in static_folder, which is the path to folder with static files on your server file system.

Ihar Yazerski
  • 111
  • 1
  • 7