Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask static file to absolute path

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>
like image 361
kefeizhou Avatar asked Nov 04 '13 19:11

kefeizhou


People also ask

How do I load a static file into a Flask?

To reference the static files using the url_for() function, pass in the name of the directory – static in this case – and the keyword argument of filename= followed by your static file name. Flask automatically creates a static view that serves static files from a folder named static in your application's directory.

What is static folder in Flask?

Folder structure for a Flask app The static folder contains assets used by the templates, including CSS files, JavaScript files, and images. In the example, we have only one asset file, main. css. Note that it's inside a css folder that's inside the static folder.


2 Answers

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.

like image 57
Miguel Avatar answered Oct 13 '22 01:10

Miguel


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

like image 20
OGURA Daiki Avatar answered Oct 13 '22 00:10

OGURA Daiki