I want to run some commands from terminal with 'flask' command but it isn't working.
The Following is my project structure-
FlaskUserAuthentication
├── FlaskUserAuthentication
│ ├── API
│ │ ├── __init__.py
│ │ ├── db_models.py
│ │ └── routes.py
│ ├── Site
│ │ ├── __init__.py
│ │ ├── routes.py
│ │ ├── static
│ │ │ └── form_style.css
│ │ └── templates
│ │ └── Site
│ │ ├── base_layout.html
│ │ ├── index.html
│ │ ├── logout.html
│ │ ├── profile.html
│ │ ├── signin.html
│ │ └── signup.html
│ ├── __init__.py
│ └── commands.py
├── run.py
└── venv
As the flask run
command runs the app, I am sure that my environment variable is set properly. However, when I try to use flask-cli-command like-
flask create_db
I get, Error: No such command "create_db".
The following is my FlaskUserAuthentication/commands.py
file-
from FlaskUserAuthentication import app, db
from FlaskUserAuthentication.API.db_models import Group, Member, Project, Milestone
@app.cli.command('create_db')
def createDatabase():
db.create_all()
print('***** Datebase created ****')
#....some more commands
and the FlaskUserAuthentication/__init__.py
module (where the Flask app instance is initiated)-
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SECRET_KEY'] = 'justasamplekey'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db = SQLAlchemy(app)
from FlaskUserAuthentication.API.routes import api
from FlaskUserAuthentication.Site.routes import site
app.register_blueprint(api)
app.register_blueprint(site)
The issue was that the command.py
module was not recognized by the app. The solution, I tried is to initiate a blueprint in the commands
module and then in the __init__.py
module where I initiated the app
instance, I registered the corresponding Blueprint with it. So, the updated commands.py
-
from FlaskUserAuthentication import db
from flask import Blueprint
from FlaskUserAuthentication.API.db_models import Group, Member, Project, Milestone
@cmd = Blueprint('db', __name__) #created a Blueprint for this module
@cmd.cli.command('create_db') #rather than generating the cli command with app,
def createDatabase(): # used the blueprint, cmd
db.create_all()
print('***** Datebase created ****')
and the updated __init__.py
module,
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SECRET_KEY'] = 'justasamplekey'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db = SQLAlchemy(app)
from FlaskUserAuthentication.API.routes import api
from FlaskUserAuthentication.Site.routes import site
from FlaskUserAuthentication.commands import cmd
app.register_blueprint(api)
app.register_blueprint(site)
app.register_blueprint(cmd) # registered the command module as blueprint
Now set the environment variable and development server with (in the terminal) -
export FLASK_APP=FlaskUserAuthentication/__init__.py
export FLASK_DEBUG=1
After that use the flask command like-
flask db create_db
P.S.: now because of the Blueprint rather than flask create_db
, the command will be
flask db create_db
I needed to realise the root of the issue and for that I thank the author of the post-
Where should I implement flask custom commands (cli)
and the author, @emont01 for his response to the post.