为什么我使用 Click 库编写的这个 CLI 应用程序不能正常工作?
Why does this CLI app that I've written with the Click library not working properly?
我已经创建了这个 CLI 应用程序来初始化数据库等。我什至已经为数据库创建了一个应用程序上下文,但是我得到了一个错误。
请注意,我使用的是 docker
和 docker-compose
。我知道这个错误和我们有几个应用程序的时候有关,数据库不知道要和哪个应用程序一起工作。但我不知道如何解决这个问题。
from click import group, command, option, pass_context
from sqlalchemy_utils import database_exists, create_database
from call.app import create_app
from call.extensions import db
from call.blueprints.user.models import User
# Create an app context for the database connection.
app = create_app()
db.init_app(app)
@group()
def cli():
""" Run SQLite3 related tasks. """
pass
@command()
@option('--with-testdb/--no-with-testdb', default=False,
help='Create a test db too?')
def init(with_testdb):
"""
Initialize the database.
:param with_testdb: Create a test database
:return: None
"""
with app.app_context():
db.drop_all()
db.create_all()
if with_testdb:
db_uri = '{0}_test'.format(app.config['SQLALCHEMY_DATABASE_URI'])
if not database_exists(db_uri):
create_database(db_uri)
return None
@command()
def seed():
"""
Seed the database with an initial user.
:return: User instance
"""
with app.app_context():
if User.find_by_identity(app.config['SEED_ADMIN_EMAIL']) is not None:
return None
params = {
'role': 'admin',
'email': app.config['SEED_ADMIN_EMAIL'],
'password': app.config['SEED_ADMIN_PASSWORD']
}
return User(**params).save()
@command()
@option('--with-testdb/--no-with-testdb', default=False,
help='Create a test db too?')
@pass_context
def reset(ctx, with_testdb):
"""
Init and seed automatically.
:param with_testdb: Create a test database
:return: None
"""
with app.app_context():
print(ctx)
ctx.invoke(init, with_testdb=with_testdb)
ctx.invoke(seed)
return None
cli.add_command(init)
cli.add_command(seed)
cli.add_command(reset)
这是错误:
/usr/local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py:813: UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:".
'Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. '
/usr/local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
/usr/local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
错误表明 app.config['SQLALCHEMY_DATABASE_URI']
不可用。
我建议在您的 docker 撰写文件中将其设置为环境变量。查看烧瓶文档。例子:
docker-撰写:
web:
environment:
- SECRET_KEY=123
应用程序:
import os
_mail_enabled = os.environ.get("MAIL_ENABLED", default="true")
MAIL_ENABLED = _mail_enabled.lower() in {"1", "t", "true"}
SECRET_KEY = os.environ.get("SECRET_KEY")
if not SECRET_KEY:
raise ValueError("No SECRET_KEY set for Flask application")
我已经创建了这个 CLI 应用程序来初始化数据库等。我什至已经为数据库创建了一个应用程序上下文,但是我得到了一个错误。
请注意,我使用的是 docker
和 docker-compose
。我知道这个错误和我们有几个应用程序的时候有关,数据库不知道要和哪个应用程序一起工作。但我不知道如何解决这个问题。
from click import group, command, option, pass_context
from sqlalchemy_utils import database_exists, create_database
from call.app import create_app
from call.extensions import db
from call.blueprints.user.models import User
# Create an app context for the database connection.
app = create_app()
db.init_app(app)
@group()
def cli():
""" Run SQLite3 related tasks. """
pass
@command()
@option('--with-testdb/--no-with-testdb', default=False,
help='Create a test db too?')
def init(with_testdb):
"""
Initialize the database.
:param with_testdb: Create a test database
:return: None
"""
with app.app_context():
db.drop_all()
db.create_all()
if with_testdb:
db_uri = '{0}_test'.format(app.config['SQLALCHEMY_DATABASE_URI'])
if not database_exists(db_uri):
create_database(db_uri)
return None
@command()
def seed():
"""
Seed the database with an initial user.
:return: User instance
"""
with app.app_context():
if User.find_by_identity(app.config['SEED_ADMIN_EMAIL']) is not None:
return None
params = {
'role': 'admin',
'email': app.config['SEED_ADMIN_EMAIL'],
'password': app.config['SEED_ADMIN_PASSWORD']
}
return User(**params).save()
@command()
@option('--with-testdb/--no-with-testdb', default=False,
help='Create a test db too?')
@pass_context
def reset(ctx, with_testdb):
"""
Init and seed automatically.
:param with_testdb: Create a test database
:return: None
"""
with app.app_context():
print(ctx)
ctx.invoke(init, with_testdb=with_testdb)
ctx.invoke(seed)
return None
cli.add_command(init)
cli.add_command(seed)
cli.add_command(reset)
这是错误:
/usr/local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py:813: UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:".
'Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. '
/usr/local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
/usr/local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
错误表明 app.config['SQLALCHEMY_DATABASE_URI']
不可用。
我建议在您的 docker 撰写文件中将其设置为环境变量。查看烧瓶文档。例子: docker-撰写:
web:
environment:
- SECRET_KEY=123
应用程序:
import os
_mail_enabled = os.environ.get("MAIL_ENABLED", default="true")
MAIL_ENABLED = _mail_enabled.lower() in {"1", "t", "true"}
SECRET_KEY = os.environ.get("SECRET_KEY")
if not SECRET_KEY:
raise ValueError("No SECRET_KEY set for Flask application")