Black Lives Matter. Please consider donating to Black Girls Code today.

Integrate Dash into Flask***Plot is not getting rendered***

Hi,
I’m trying to integrate a dash app into flask. I’m able to see that layout is available on flask but no plot is getting rendered. Following is my code:

Please let me what am I missing. I’ve spent appx. 40 Hrs. but still no success. Any help will be much appreciated.

Flask_App.py

server = Flask(name)
####################Dash Code###################
dash_app = dash.Dash(name, server=server, url_base_pathname=’/dashboard/’)
dash_app.config.supress_callback_exceptions = True
dash_app.layout = html.Div(
[
dcc.Input(id=‘search_term’, value=‘car’, type=‘text’),
dcc.Graph(id=‘live-graph’, animate=False),
dcc.Interval(
id=‘graph-update’,
interval=1*1000
),
]
)

@server.route(’/dashboard/’, methods = [‘POST’, “GET”])
@dash_app.callback(Output(‘live-graph’, ‘figure’),
[Input(component_id=‘search_term’, component_property=‘value’)],
events=[Event(‘graph-update’, ‘interval’)])
def update_graph_scatter(search_term):
try:
conn = sqlite3.connect(‘t.db’)
c = conn.cursor()
df = pd.read_sql(“SELECT * FROM xyztable WHERE text LIKE ? ORDER BY unix DESC LIMIT 1000”, conn ,params=(’%’ + search_term + ‘%’,))
df.sort_values(‘unix’, inplace=True)
df[‘date’] = pd.to_datetime(df[‘unix’], unit=‘ms’)
df.set_index(‘date’, inplace=True)
df[‘text_smoothed’] = df[‘text’].rolling(int(len(df)/4)).mean()
df.dropna(inplace=True)
df = df.resample(‘7000ms’).mean()
X = df.index[-100:]
Y = df.text_smoothed.values[-100:]
data = go.Scatter(
x=X,
y=Y,
name=‘Scatter’,
mode= ‘lines+markers’
)
data = [data]
layout = go.Layout(xaxis=dict(range=[min(X),max(X)]), yaxis=dict(range=[min(Y),max(Y)]), title=‘Term: {}’.format(search_term))
# yaxis=dict(range=[min(Y),max(Y)]),
# title=‘Term: {}’.format(search_term))
# {‘data’: [data],‘layout’ : go.Layout(xaxis=dict(range=[min(X),max(X)]),
# yaxis=dict(range=[min(Y),max(Y)]),
# title=‘Term: {}’.format(search_term))}
return render_template(‘live.html’, data = data, layout=layout)

except Exception as e:
    with open('errors.txt','a') as f:
        f.write(str(e))
        f.write('\n')

if name == “main”:
init()

app = DispatcherMiddleware(server, {
'live.html': dash_app.server,
})
server.run(debug=True)
run_simple('127.0.0.1', 5000, app, use_reloader=True, use_debugger=True)

Hi nitin, did you figure this out? I’m about to try to use a dash app as one of the pages my flask app goes to through the routes.

I found this and it looks promising. Maybe it will work for you.