我正在尝试构建一个Web应用程序以显示一些数据。到目前为止,我已经使用SQL数据库建立了Flask框架。我现在正在尝试设置Dash仪表板。我创建两个应用程序和数据库,如下所示:

server = Flask(__name__)
app = Dash(__name__, server=server, url_base_pathname='/dashboard/')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app) # create database


但这引发了错误:

Traceback (most recent call last):
  File "/Users/annabernbaum/OneDrive - Imperial College London/Documents/Work/Year 4/Sensing and IoT/Project/SIOT_Project_DE4/Coursework_2/run.py", line 1, in <module>
    from LightDogs import app
  File "/Users/annabernbaum/OneDrive - Imperial College London/Documents/Work/Year 4/Sensing and IoT/Project/SIOT_Project_DE4/Coursework_2/LightDogs/__init__.py", line 12, in <module>
    db = SQLAlchemy(app) # create database
  File "/Users/annabernbaum/anaconda3/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 683, in __init__
    self.init_app(app)
  File "/Users/annabernbaum/anaconda3/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 799, in init_app
    app.extensions['sqlalchemy'] = _SQLAlchemyState(self)
AttributeError: 'Dash' object has no attribute 'extensions'


有谁知道如何超越这个范围,并从Dash应用程序成功创建SQLAlchemy数据库?

谢谢!

最佳答案

使用Flask应用而非Dash应用初始化数据库。

您的代码应如下所示:

db = SQLAlchemy(server)


希望这可以帮助。

关于python - 将Flask与SQLAlchemy和Dash结合使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53999681/

10-13 00:47