Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Dash on Google Cloud with database call

I’ve been trying to deploy my dash application which has been working fine on my local machine to work in Google Cloud. I finally seem to have some success but have a question on the best way to get data from my Cloud MySQL database. I’m new to Flask so may be missing something obvious.

With Flash (but without out dash) I can do this

from flask import Flask
import pandas as pd
import os
from flask_sqlalchemy import SQLAlchemy
import sqlalchemy

app = Flask(__name__)

# Environment variables are defined in app.yaml.
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['SQLALCHEMY_DATABASE_URI']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)
con=db.engine.connect().connection
dfp=pd.read_sql_query("SELECT * FROM v_pref", con=con)

However with a Dash app…
app=dash.Dash(__name__)
How can I initialize my db using SQLAlchemy in a Dash application?

I ended up doing this and it works but I’m not sure if this is the best way… Any tips anyone can share would be much appreciated.

from flask import Flask
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import os
import pandas as pd
import plotly.plotly as py
import plotly.graph_objs as go
from flask_sqlalchemy import SQLAlchemy
import sqlalchemy

app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['SQLALCHEMY_DATABASE_URI']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
con=db.engine.connect().connection
df=pd.read_sql("SELECT * FROM v_unitssold", con=con)
dfv=pd.read_sql("SELECT * FROM v_dailyvisitors", con=con)
dfp=pd.read_sql("SELECT * FROM v_pref", con=con)
dfnu=pd.read_sql("SELECT * FROM v_newused", con=con)

app= dash.Dash(__name__)
server = app.server
1 Like

Slight improvement is that you can pass a Flask server instance into the creation of the Dash instance to support exactly this context:

app = dash.Dash(__name__, server=server)

Thanks for your reply. My concern is that the program needs to set ‘app=Flask(name)’ so I can read from the database using SQLALchemy. However I then need to set ‘app=dash.Dash(name)’ to be able to run Dash code that follows later. If my program needs to do further database calls later on, how do I do that?

How do I use SQLALchemy to interact with the database in a Dash app? How do I do something like the following I were to set ‘app=dash.Dash(name)’ instead of ‘app=Flask(name)’?

app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['SQLALCHEMY_DATABASE_URI']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

There’s nothing special about the variable name app, it’s just a convention that Flask uses, and Dash has adopted, perhaps a little confusingly, as a Dash instance has its own Flask instance. In the case of a Dash instance, and also a Flask instance (not associated with a Dash application) they could both be named something else, so long as you consistently use the new name elsewhere.

That means you can just do this:

server = Flask(__name__)
server.config['SQLALCHEMY_DATABASE_URI'] = os.environ['SQLALCHEMY_DATABASE_URI']
server.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

app = dash.Dash(__name__, server=server)
1 Like

@nedned Thank you very much for your response! I get it now. This makes sense.

1 Like