Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add namespace only if config value is set? #9

Open
memjr opened this issue Nov 9, 2021 · 0 comments
Open

Add namespace only if config value is set? #9

memjr opened this issue Nov 9, 2021 · 0 comments

Comments

@memjr
Copy link

memjr commented Nov 9, 2021

Is there a way to load a namespace only if some variable is set? For example I tried modifying flask-api-tutorial/src/flask_api_tutorial/api/init.py like this:

from flask import Blueprint, current_app
from flask_restx import Api

from flask_api_tutorial.api.auth.endpoints import auth_ns

...

api.add_namespace(auth_ns, path="/auth")

if current_app.config["ENV"] == "development":
    from flask_api_tutorial.api.widgets.endpoints import widget_ns

    api.add_namespace(widget_ns, path="/widgets")

But get this error

Traceback (most recent call last):
  File "C:\Users\memjr\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\memjr\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\Scripts\flask.exe\__main__.py", line 7, in <module>
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\flask\cli.py", line 967, in main
    cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\flask\cli.py", line 586, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\click\core.py", line 782, in main
    rv = self.invoke(ctx)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\click\core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\click\core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\click\core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\click\decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\flask\cli.py", line 425, in decorator
    with __ctx.ensure_object(ScriptInfo).load_app().app_context():
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\flask\cli.py", line 388, in load_app
    app = locate_app(self, import_name, name)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\flask\cli.py", line 240, in locate_app
    __import__(module_name)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\run.py", line 11, in <module>
    app = create_app(os.getenv("FLASK_ENV", "development"))
  File "c:\users\memjr\pycharmprojects\flask-api-tutorial\src\flask_api_tutorial\__init__.py", line 20, in create_app
    from flask_api_tutorial.api import api_bp
  File "c:\users\memjr\pycharmprojects\flask-api-tutorial\src\flask_api_tutorial\api\__init__.py", line 20, in <module>
    if current_app.config["ENV"] == "development":
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\werkzeug\local.py", line 348, in __getattr__
    return getattr(self._get_current_object(), name)
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\werkzeug\local.py", line 307, in _get_current_object
    return self.__local()
  File "C:\Users\memjr\PycharmProjects\flask-api-tutorial\venv\lib\site-packages\flask\globals.py", line 52, in _find_app
    raise RuntimeError(_app_ctx_err_msg)
RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context().  See the
documentation for more information.

Edit: formatted code blocks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant