Hello Everyone,
A Dash and Plotly newbie here. Following the Dash tutorials I was able to produce graphs dynamically based on dropdown menus input.
In that case I used a csv file on my machine as a data source.
After that I tried to use a MySQL database as data source with the help of SQLalchemy and Pandas, and that worked for a static plot. Nevertheless, when I tried to make dynamic plots based on queries constructed with dropdown inputs, the plot never renders in the server.
This is what I tried so far:
import pandas as pd
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import sqlalchemy as sql
engine_string = (âmysql+pymysql://username:PASSWORDâ+
â127.0.0.1/DBNAMEâ)
engine = sql.create_engine(engine_string)
query = "SELECT * FROM Table WHERE Product_id ={} "
app.layout = html.Div([
#some other dropdowns here
dcc.Dropdown(id=âprod_dropâ),
dcc.Graph(id=âstore_salesâ)
])
@app.callback(
dash.dependencies.Output(âstore_salesâ, âfigureâ),
[dash.dependencies.Input(âprod_dropâ, âvalueâ)]
)
def update_graph3(selected_product):
sql_qry = sales_query.format(selected_product)
df = pd.read_sql_query(sql_qry, engine,
parse_dates=[âRecord_dateâ])
traces = []
for i in df.Type_name.unique():
traces.append(
go.Scatter(
x=df[df[âType_nameâ]==i][âRecord_dateâ],
y=df[df[âType_nameâ]==i][âUnits_soldâ],
mode=âmarkersâ
)
)
return {
âdataâ:traces,
âlayoutâ: go.Layout(
xaxis={âtitleâ: uâAĂąoâ},
yaxis={âtitleâ: âUnidadesâ},
hovermode=âclosestâ
)
}
Thanks in advance for your attention, any guidance would be truly appreciated.
IvĂĄn
pd: Sorry for identation issues, don't know how to insert code yet :)