Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

app.shell_context_processor decorator does not register the function as a shell context function

Tags:

shell

flask

I created the following function in a microblog.py file in my ~/Programing/Rasa/myflaskapp/app folder. It creates a shell context that adds a database instance and models to the shell session:

from app import app, db
from app.models import User, Post

@app.shell_context_processor
def make_shell_context():
    return {'db': db, 'User': User, 'Post': Post}

The app.shell_context_processor decoder registers the function as a shell context function. But when the flask shell command is executed, in ~/Programing/Rasa/myflaskapp/ it does not invoke this function and records the elements returned by it in the shell session as expected.

So I get this:

(MyFlaskAppEnv) mike@mike-thinks:~/Programing/Rasa/myflaskapp$ flask shell
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
App: app [production]
Instance: /home/mike/Programing/Rasa/myflaskapp/instance
>>> db
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'db' is not defined

Rather than :

(venv) $ flask shell
>>> db
<SQLAlchemy engine=sqlite:////Users/migu7781/Documents/dev/flask/microblog2/app.db>

Update : I tried to check if the function was well saved

But it seems not :

>>> print(app.shell_context_processors[0]())
Traceback (most recent call last):
  File "<console>", line 1, in <module>
IndexError: list index out of range

I changed microblog.py only with importing app and db

from app import app, db

@app.shell_context_processor
def make_shell_context():
    return {'db': db}

I tried to put microblog.py it in the app folder or even remove it, it's always the same error : I am not able to register functions as a shell context function. In the same time when I call for >>> app in the Flask context I do have an answer.

like image 866
Alice Antoine Avatar asked Jul 18 '26 14:07

Alice Antoine


1 Answers

I told Flask how to import the application, by setting the FLASK_APP environment variable:

export FLASK_APP=microblog.py

It seems to make it !

like image 116
Alice Antoine Avatar answered Jul 21 '26 20:07

Alice Antoine