Python using at least 25% CPU power when Dash is running, without any page reloads

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!

Try it with debug=False.

With debug=True it’s constantly re-scanning for any files that are modified + other stuff, I’ve noticed it use at least 7% CPU on my machine, so could be the same.

1 Like

Thanks, that actually helped. Thought my code was just producing some background task, that was creating the load. Any idea regarding the “Bonus question”? Seems weird to me, that on first load the y axis is wrong, although the code is just using the same code for the initial load, as for the callback when changing to that chart…

I’m not a Dash expert, and if I were you and couldn’t figure it out I would ask the specific question as a new thread with screenshots.

However my understanding is the layout gets called before any callbacks get fired, and in the layout your graph doesn’t have any data, perhaps you can set some initial conditions you want in your layout? Either by providing it some data or looking in the the properties of Graph, I’m sure there’s a way to set initial X and Y values.