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.