flaskjinja2livereloadflask-script

Change Flask config after app creation


I am using Livereload + Flask + Flask SOcketIO + Flask Script. The problem is that SocketIO needs a gevent-socketio server. Which is all livereload is not. Do not get me wrong, I do not want to solve this issue. I only run livereload to do some css/sass/js work so it's ok.

But the exception throwed at me every time the client tried to connect to the socketIO server bothers me a lot. So I thought I could just disable javascript, but my app relies mainly on angular to asynchronously render directives, so there would be no tags on the page for me to be able to switch styles. Then I realize I could write a config property so when I load the page Jinja2 would provide it as a javascript variable and I would not load the specific socket-io js file in case it is set to False.I have tried this.

Setting it on config.py:

class Config:
    LIVERELOAD = False

I run the livereload server with FLask-Script

class liveReloadServer(Command):
    """prints hello world"""
    def run(self):
        with app.app_context():
            server = Server(app.wsgi_app)
            Config.LIVERELOAD = 'True'
            server.serve(port=8080)

Passing the config property to javascript side

var LIVERELOAD = "{{ config['LIVERELOAD'] }}";

But this does not work ( LIVERELOAD always show True) cause I am setting the property after the Config class has already been inserted into the Flask app object.Is there another way?


Solution

  • You need to set the Flask app's config, which is not tied to any custom objects you created.

    app.config['LIVERELOAD'] = True