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