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

Dash callback stops working when interacting with sqlite

Hi,
I have a relatively simple app as follows:

import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output
from layout_constructor import create_header
import dao
import sqlite3
from sqlalchemy import create_engine
import pandas as pd 

conn = create_engine('sqlite:///G:/pyplicate/attunity.db')


def query(q):
    df = pd.read_sql_query(sql=q, con=conn)
    return df 

def get_summary_server_details():
    server_details_query = f'''SELECT cdc_inserts_count, cdc_update_count, cdc_delete_count, cdc_ddl_count 
        FROM taskDetails ORDER BY status_timestamp DESC LIMIT 1'''

    return query(server_details_query)

external_stylesheets = [
    'https://codepen.io/chriddyp/pen/bWLwgP.css', dbc.themes.SUPERHERO]

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div([
    create_header(),
    html.Div(
        [
            dcc.Interval(
                id='interval-component',
                interval=1*1000,
                n_intervals=0
            )
        ]
    )
])


@app.callback(
    Output(component_id='agg-running-task', component_property='children'),
    [Input(component_id='interval-component', component_property='n_intervals')]
)
def update_output_div(n):
    #sql_result = pd.read_sql_query('SELECT cdc_inserts_count, cdc_update_count, cdc_delete_count, cdc_ddl_count FROM taskDetails ORDER BY status_timestamp DESC LIMIT 1', con=conn)

    return 'Ok!'
    # return 'You\'ve entered "{}"'.format(input_value)


if __name__ == '__main__':
    app.run_server(debug=True)

For now the callback returns ‘Ok’. The moment i uncomment the “sql_result” line i cant get the callback to return anything. Not even “Ok”. Does anyone have a clue why uncommenting this one line breaks the whole app?

Any help is highly appreciated.

Best Regards,
Simon

Do you get any error messages either in the terminal or the JavaScript console? If you run the sql_result like outside of a callback does it work as expected? How long does it take? If it’s running for a long time the callback could time out (we’re talking 30-60 seconds before that starts to be a problem which is unlikely for a select query unless you have a lot of data).

Hi,
No errors messages in either terminal nor console. If i add a print(sql_result) my pandas dataframe is printed with the correct data every second as expected.

Too add to this, the “return ‘ok!’” breaks (and never returns anything to my H5) even tho data is comming throgh in the callback every second.

Try reducing the rate that interval fires at? It’s possible that you’re just permanently stuck in an updating state because the query takes about a second and by the time its complete the callback has already triggered again.

2 Likes

Jesus. That worked :man_facepalming: Time to change database / optimize the database :slight_smile:

Thanks a lot!

2 Likes