Using Flask with SQLAlchemy and Dash

Viewed 3461

I am trying to build a web app to display some data. So far I have set up a Flask framework using an SQL database. I am now trying to set up a Dash dashboard. I create the 2 apps and database as follows:

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

However this throws up the error:

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'

Does anyone know how to move past this and successfully create the SQLAlchemy database from the Dash app?

Thanks!

1 Answers

Initialise db using the Flask app and not Dash app.

Your code should look like:

db = SQLAlchemy(server)

Hope this helps.

Related