Hi all,
I am trying out my first Dash app, which is based on a small sqlite database which is updated hourly with values. Therefore I want to update the data on every page reload. Regarding functionality everything works fine, however I got some problems with my CPU load. I just started the server with “python app.py” and without any reloads at all my cpu load of the python process remains at 25%.
Part of my code:
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
from functions import *
import plotly.graph_objs as go
from dash.dependencies import Input, Output
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
#Generate the Dash App
app.layout = html.Div([
dcc.Dropdown(
id='dropdown',
options=[
{'label': 'Gesamtportfolio', 'value': 'gesamtportfolio'},
{'label': 'Kaufwert vs. Aktueller Wert', 'value': 'kaufwertVsAktuell'},
{'label': 'Test', 'value': 'test'}
],
value='gesamtportfolio'
),
dcc.Graph(id='graph', style={'height': '94vh'})
]
)
@app.callback(
dash.dependencies.Output('graph', 'figure'),
[dash.dependencies.Input('dropdown', 'value')])
def update_figure(selected_graph):
if selected_graph == 'gesamtportfolio':
print(selected_graph)
#Read in the entire depot
depot = serve_df()
depotNow = depot.pivot_table(index='date', columns='position', values='total_value')
traces = []
#Iterate through depot with current values and add them to the trace
for position, values in depotNow.iteritems():
traces.append(go.Scatter(x=values.index, y=values.values, name=position, stackgroup='A'))
return {
'data': traces,
'layout': go.Layout(
legend={'x': 0, 'y': 0, 'bgcolor': 'rgba(255,255,255,.65)'},
margin=go.layout.Margin(
l=40,
r=10,
b=30,
t=10,
pad=4
)
)
}
if selected_graph == 'kaufwertVsAktuell':
#anothergraph
if selected_graph == 'test':
#testing out new plots
if __name__ == '__main__':
app.run_server(debug=True, host='localhost', port=8050)
Any ideas why this is?
Bonus question: On first reload the y axis is showing a value which is twice as high as it should be. Imagine my portfolio with all stacked traces around 10. On first reload the y axis shows 20, although all the traces accumulate only to 10. When I select the plot again in the drop down and the plot reloads the y axis is correct, showing only 10.
Thanks a lot!