Creating custom commands in flask needs access to the app, which is generally created in app.py
like this:
import click
from flask import Flask
app = Flask(__name__)
@app.cli.command("create-user")
@click.argument("name")
def create_user(name):
...
However, in order not to bloat my app.py, I want to put my custom commands in a separate file e.g. commands.py
, but this doesn't work because the entrypoint to my project is app.py
, so I'll have to import app in commands.py
and import my commands in app.py
which results in a circular import error.
How can I create custom commands in separate files ?
with app.app_context():
, check this – Waldonwith app.app_context():
. So the same problem remains. – Seiglerdef register_cli(app: Flask):
– Sabah