italomaia/flask-empty

how to use flask-bootstrap in flask-empty framework?

jackywu opened this issue · 6 comments

I have read the usage: https://pythonhosted.org/Flask-Bootstrap/basic-usage.html.
Should I do somethings in main.py or manage.py? It looks like both is ok, but which one is best ?
and, do you knwo is there anythings else not compatible?

Actually, with flask-empty, you have to define your bootstrap instance in the extensions.py module and let your configuration file know that it should be loaded. (EXTENSIONS option) Tell me if it worked. There is no need to change main.py or manage.py.

after read the source code ,I understand the logic. I find there are two problem

  1. unknow exception

contents of extentions.py

try:
    # only works in debug mode
    from flask_debugtoolbar import DebugToolbarExtension
    from flask_bootstrap import Bootstrap

    toolbar = DebugToolbarExtension()
    bootstrap = Bootstrap()
except ImportError:
    print('debugtoolbar extension not available.')

snippet contents of config.py

    EXTENSIONS = [

        'extensions.toolbar',
        'extensions.bootstrap',
    ]

and trigger exception

» python2 manage.py runserver -h 0.0.0.0 -p 5000                                                        1 ↵
--------------------------------------------------------------------------------
INFO in empty [/Volumes/edisk/work/codebase/rpi/service_controller/empty.py:81]:
Logger started
--------------------------------------------------------------------------------
Traceback (most recent call last):
  File "manage.py", line 18, in <module>
    manager.run()
  File "/usr/local/lib/python2.7/site-packages/flask_script/__init__.py", line 412, in run
    result = self.handle(sys.argv[0], sys.argv[1:])
  File "/usr/local/lib/python2.7/site-packages/flask_script/__init__.py", line 383, in handle
    res = handle(*args, **config)
  File "/usr/local/lib/python2.7/site-packages/flask_script/commands.py", line 410, in __call__
    use_debugger = app.debug
AttributeError: 'NoneType' object has no attribute 'debug'
  1. there is no place to set attribute of app after setup Bootstrap

this is what I finally wanna do.

# Install our Bootstrap extension
    Bootstrap(app)
    app.config['BOOTSTRAP_SERVE_LOCAL'] = True

after set Bootstrap, I wanna set a BOOTSTRAP_SERVE_LOCAL into app.config.

from app_factory function, we know I can not put BOOTSTRAP_SERVE_LOCAL in config.py, because Bootstrap() will overwrite it in app.set() in configure_extensions(self) this method. so the right logic is: at somewhere first call configure_extensions and then set the BOOTSTRAP_SERVE_LOCAL by someway, but I can not find the way.

def app_factory(config, app_name, blueprints=None):
    # you can use Empty directly if you wish
    app = App(app_name, template_folder=os.path.join(PROJECT_PATH, 'templates'))
    config = config_str_to_obj(config)

    app.configure(config)
    app.add_blueprint_list(blueprints or config.BLUEPRINTS)
    app.setup()

configure_extensions() exist in set().

and I have another question.

class App(Empty):
    def configure_views(self):
        @self.route('/')
        def index_view():
            return render_template("index.html")

this is the origin code in main.py. I hava a blueprint named "HelloWorld", I want a http request to root "/" to call to the view in HelloWord instead of the App.configure_views. so should i delete the configure_views in main.py?is it the recommend way?

thanks a lot.

Try it like this:

try:
    # only works in debug mode
    from flask_debugtoolbar import DebugToolbarExtension
    
    toolbar = DebugToolbarExtension()
except ImportError:
    print('debugtoolbar extension not available.')

from flask_bootstrap import Bootstrap    
bootstrap = Bootstrap()

And make sure flask-debugtoolbar is installed.

Also, set this in config:

BOOTSTRAP_SERVE_LOCAL = True

so should i delete the configure_views in main.py?

Override it with a empty implementation.

@jackywu was your problem solved?

thanks