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

Callback inside Functions

Hi Guys,

I working in a project based on @nedned github project.

ned code:

my code:

However i not sure how can i make my callbacks work inside a routes(functions)

The update_columns callback functions are not working as expected to perform calculations in dash table.

This is the structure.

def dispatcher(request):
    Main function
    @param request: Request object

    app = _create_app()
    params = {
        'data': request.body,
        'method': request.method,
        'content_type': request.content_type
    with app.server.test_request_context(request.path, **params):
            response = app.server.full_dispatch_request()
        except Exception as e:
            response = app.server.make_response(app.server.handle_exception(e))
        return response.get_data()

def _create_app():

    ''' Creates dash application '''

    app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
    app.layout = dhc.Div(children=[])

        dash.dependencies.Output('content', 'children'),
        [dash.dependencies.Input('url', 'pathname')]
    def display_page(pathname):

        ''' '''
        if not pathname:
            return ''
        if pathname == '/':
            return dash_index()
        method = pathname[1:].replace('-', '_')
        func = getattr(sys.modules[__name__], method, None)
        if func:
            return func()
        return 'Unknown link'

    return app

def dash_fig1():
    pandas code
    return dash core components

        Output('computed-table', 'data'),
        [Input('computed-table', 'data_timestamp')],
        [State('computed-table', 'data')])
    def update_columns(timestamp, rows):
        for row in rows:
                if row['Unidades Pendentes'] != 0:
                    row['UPH_BPI_vs_Head'] = float(row['UPH_BPI_vs_Perfil']) * float(row['Head_Disponível'])
                    row['ETA_Geral'] = float(row['Unidades Pendentes']) / float(row['UPH_BPI_vs_Head'])
                    row['Delta_Hora'] = float(row['Horas_Disp']) - float(row['ETA_Geral'])
                    row['Risco_Aging'] = float(row['Delta_Hora']) * float(row['UPH_BPI_vs_Head'])
                    row['UPH_BPI_vs_Head'] = "Completed"
                    row['ETA_Geral'] = "Completed"
                    row['Delta_Hora'] = "Completed"
                    row['Risco_Aging'] = "Completed"
                    row['UPH_BPI_vs_Perfil'] = "Completed" 
                    row['Head_Disponível'] = "Completed"
                    row['Horas_Disp'] = "Completed" 
                row['ETA_Geral'] = 'NA'
        return rows


        Output('table-backlog', 'data'),
        [Input('table-backlog', 'data_timestamp')],
        [State('table-backlog', 'data')])
    def update_columns(timestamp, rows):
        for row in rows:
                if row['Unidades Pendentes'] != 0:
                    row['Delta Hora'] = float(row['Horas Disp']) - float(row['ETA'])
                    row['Risco Aging'] = float(row['Delta Hora']) * float(row['UPH'])
                    row['ETA'] = "Completed"
                row['ETA'] = row['ETA']
        return rows

It looks like you’re trying to create your Dash app inside the dispatcher function. This means you’re creating a fresh Dash app every time Django is handling a response. You want to have a Dash app that’s already initialised (in another module) that you can just reference when needed. This is the whole point of in that code example, then you just do from .server import server or from .server import app

Also, all Dash callbacks need to have already been created before the app starts running. So you would need to call dash_fig1 before starting the app.