12

Has anyone tried this snippet flask config based static folder code cnippet?

The code:

import flask
class MyFlask(flask.Flask):
    @property
    def static_folder(self):
        if self.config.get('STATIC_FOLDER') is not None:
            return os.path.join(self.root_path, 
                self.config.get('STATIC_FOLDER'))
    @static_folder.setter
    def static_folder(self, value):
        self.config.get('STATIC_FOLDER') = value

# Now these are equivalent:
app = Flask(__name__, static_folder='foo')

app = MyFlask(__name__)
app.config['STATIC_FOLDER'] = 'foo'

In my case in complains about this line:

self.config.get('STATIC_FOLDER') = value

The error message: Can't assign to function call

Does anyone how to set the static_folder from the config.py file in Flask?

Nurjan
  • 5,889
  • 5
  • 34
  • 54

5 Answers5

16

Okay, I assume you want to use a custom path to the static folder for whatever reason. I wanted to do the same for the sake of better app modularity.

Here's my app folder structure:

instance/
core/
  |_templates/
  |_static/
  |_views.py
run.py
config.py

As you can see, my static folder is inside the core folder.

In run.py, you can do the following:

app = Flask(__name__, static_url_path=None)

if __name__ == '__main__':
    app.config.from_object('config')

    # config file has STATIC_FOLDER='/core/static'
    app.static_url_path=app.config.get('STATIC_FOLDER')

    # set the absolute path to the static folder
    app.static_folder=app.root_path + app.static_url_path

    print(app.static_url_path)
    print(app.static_folder)

    app.run(
        host=app.config.get('HOST'),
        port=app.config.get('PORT'),
        threaded=True
        )

This is what I did, and it works perfectly fine. I'm using flask 0.12.

mania_device
  • 235
  • 3
  • 8
  • app_ = Flask(__name__, static_url_path=None) app_.config.from_object('config') # i fix the bug with down there code .thank you a lot. app_.static_folder = app_.config.get("STATICFILES_DIR") app_.template_folder = app_.config.get("TEMPLATES_DIR") – dream-blue Nov 19 '21 at 01:46
9

I don't know anything about that snippet, but

some_function(...) = some_value

is never valid Python (Python doesn't have l-values). It looks like config has a dict-like interface, so the offending line should probably just be

self.config['STATIC_FOLDER'] = value

Probably a copy-and-paste error from the getter definition above the setter.

Community
  • 1
  • 1
Florian Brucker
  • 9,621
  • 3
  • 48
  • 81
8
app = Flask(__name__, static_url_path="/STATIC_FOLDER", static_folder='STATIC_FOLDER')
Sachindra
  • 6,421
  • 6
  • 29
  • 38
  • 3
    Can you explain which what is the difference between static_url_path, static_folder? – Yasin Farmani Apr 20 '21 at 15:53
  • @YasinFarmani see also the answer below https://stackoverflow.com/a/45623197/735926 – bfontaine Jan 04 '22 at 14:10
  • The question specifically asks about setting the static folder via the Flask config structure as opposed to this line, which sets in in the app creation. – bjg222 Nov 19 '22 at 17:09
2

Yes, In one of my projects I am using/setting a custom path for STATIC_FOLDER. You can set the path to STATIC_FOLDER in config.py like below:

STATIC_PATH = '<project-name>/<path-to-static-folder>/'

ex:

STATIC_PATH = 'myApp/static/'

If you can write your project structure then I can answer it as per your requirements.

Pradeepb
  • 2,564
  • 3
  • 25
  • 46
0

FYI if you want your directory to be outside of the server directory the best solutions I found so far are either to make a copy of your directory into the server directory before startup in your main(), or to create a symlink.

yldm
  • 144
  • 3
  • 14