6

Is there a flask function or a simple way to convert a static file path to it's absolute file path on disk? For example "/static/css/style.css" needs to return the absolute path for style.css based on the static folder defined in the app or blueprint.

To clarify, I'm currently using Flask-Asset.

{% assets "all_js" %}
    <script type="text/javascript" src="{{ ASSET_URL }}"></script>
{% endassets %}

The above section in the template would generate this on the production side. The main reason to not use relative path is so I can serve the static from a cookieless domain.

<script type="text/javascript" src="/static/public/all.580e5dae.js"></script>
kefeizhou
  • 6,234
  • 10
  • 42
  • 55
  • How are you expecting to handle the fact that when deployed, Flask probably won't know about where to find the static folders, since the exact location is not set in Flask but rather in some Apache or other web server configuration? – Mark Hildreth Nov 04 '13 at 19:42
  • As Miguel said check https://github.com/miracle2k/flask-assets/blob/master/src/flask_assets.py check get_static_folder function. Seems to be a good starting point. – jab Jul 24 '14 at 10:42

3 Answers3

2

It is not clear to me why you need to obtain the path to static files, but in any case, I think you can get some ideas if you look at the implementation for Flask-Assets, which has code to locate static files from the application and the blueprints and process them in different ways.

Maybe you will find that Flask-Assets does exactly what you want.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
1

simple way of solving is using 'static/css/name_of_css.css' instead of url_for('static', filename='name_of_css.css')

OGURA Daiki
  • 177
  • 5
  • 1
    the css folder needs to be included in the filen ame, since static only resolves to the actual static folder. `"url_for('static', filename='/css/font-awesome.min.css')"` – Daniel Hitzel Feb 04 '16 at 08:48
0
import os
base_path = os.getcwd() # Replace this appropriately with your static folder path
# We shouldn't use 'static' twice if it is already there in the base_path
# as correctly pointed out by mark-hildreth
if os.path.basename(base_path) == 'static':
    file_path = os.path.normpath('/css/style.css')
else:
    file_path = os.path.normpath('/static/css/style.css')
abs_path = os.path.join(base_path+file_path)
if os.path.exists(abs_path): # Verifies existence of given path
    print abs_path
shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90
  • This is some of it, but not all of it. First, you are using `'/style/css'`, but the question specifically used `'/static/css/style.css'` as part of the example. Your code magically did the right thing by stripping the `/static` part off, but on some configurations, it might not `/media` instead of `/static`. Additionally, why are you using string concatenation for the paths if you aim to pass it to `join` anyway? – Mark Hildreth Nov 04 '13 at 19:55
  • This still doesn't solve the problem. You are assuming that if `/static` is part of the URL then `/static` is part of the filename, which is not guaranteed. – Mark Hildreth Nov 04 '13 at 19:58