Deployed plotly dash not updating

Good day community,

I have published my plotly dash app to render as in the latest communications that heroku switched off free tier.

My app is using mongodb as database.

The app is running fine on deployment, however it does not update the data.

Running the app on my local machine I can see the changes that was made on mongodb but it does not update on the deployed version.

Desperately need help to use this in production.

Thank you

Hello @EhanGreens133,

It’s really hard to say without seeing your code. Are you using n_intervals to update or by refresh?

Do you define your layouts via a function reference:

def layout():
    return html.Div()

app.layout = layout

or via the Dash components directly?

app.layout = html.Div()

If you’re doing the latter, then the layout and any data within it is computed upon deployment rather than on each page load.

I am doing the latter.

App.layout = however the mongodb connection and data pull is defined above that.

If that makes a difference?

see below sample code:

dash.register_page(name, name=‘Monthly Tax’) # ‘/’ is home page

------------------------------------------------------------------------------------------------------------------------------------

client = MongoClient()

#point the client at mongo URI
client = MongoClient(‘mongodb+srv://xxxxxxxx.530fwyt.mongodb.net’)
#select database
db = client[‘TestDB’]

branches = db.branches
#convert entire collection to Pandas dataframe
branches = pd.DataFrame(list(branches.find()))
branches_opt =
for branches in branches[‘Branch’].unique():
branches_opt.append({‘label’:str(branches),‘value’:branches})

layout = html.Div(
[
dbc.Row([
dbc.Col(
dcc.Dropdown(
id = ‘branch_drop’,
options = branches_opt,
searchable= True,
style={‘width’: ‘180px’}
# multi=True
),
style={‘margin’: ‘16px’}
)

            ]),

Correct, you’ll need to turn your layout into a function:

dash.register_page(__name__, name='Monthly Tax') # ‘/’ is home page

------------------------------------------------------------------------------------------------------------------------------------
client = MongoClient()

#point the client at mongo URI
client = MongoClient('mongodb+srv://xxxxxxxx.530fwyt.mongodb.net')

def layout():
    # select database
    db = client['TestDB']
    
    branches = db.branches
    #convert entire collection to Pandas dataframe
    branches = pd.DataFrame(list(branches.find()))
    branches_opt = 
    for branches in branches[‘Branch’].unique():
        branches_opt.append('label':str(branches),'value':branches})
    return html.Div(
    [
    dbc.Row([
    dbc.Col(
    dcc.Dropdown(
    id = 'branch_drop',
    options = branches_opt,
    searchable= True,
    style={'width': '180px'}
    # multi=True
    ),
    style={'margin': '16px'}
    )])])

This will update based upon refresh

1 Like

Hi @jinnyzor , thank you for this.

Just a question.

I have a callback using the branches dataframe defined inside the function, that filters based on the drop down.

Because the callback is outside of the function, therefore it get a “not defined”

1 Like

If you can, I’d use a dcc.Store to access the updated dataframe.

Then to convert it back to a dataframe:

pd.DataFrame(storedData)

Thank you, let me give that a try

1 Like

Good day @jinnyzor ,

I have changed thing around a bit.

I render my page with an empty table and a button.

Inside the button callback I have my mongodb connection setup that return a df.

This works perfectly on my local machine, however the deployed version does not return data, It looks to me that it does not connect to my mongodb once deployed, do you have any ideas experience with this?

dash.register_page(__name__, path='/',  name='Monthly Depreciation') # '/' is home page

# -------------------------------------------------------------------------------------------------------------------------------------------------------
layout = html.Div(
    [   dbc.Row([
                    dbc.Col(
                                dbc.Button(
                                            "Load Data",
                                            id='btn',
                                            color='info'
                                        )
                            ),
                    dbc.Col(
                            dcc.Loading(html.Div(id='last-update'))
                            )
                ]),
     
        html.Br(),
        dbc.Row([
                    dbc.Container([
                                     dash_table.DataTable(
                                                                    columns=[
                                                                            {'name': 'Branch_Allocation', 'id': 'Branch_Allocation', 'type': 'text'},
                                                                            {'name': 'Date', 'id': 'Date', 'type': 'datetime'},
                                                                            {'name': 'GL_Account_Accumalative_Depreciation'}],
                                                                    id ='tbl',
                                                                    filter_action='native',
                                                                    export_format="csv",
                                                                    page_size=10,
                                                                    style_data={
                                                                                'width': '150px', 'minWidth': '150px', 'maxWidth': '150px',
                                                                                'overflow': 'hidden',
                                                                                'textOverflow': 'ellipsis',
                                                                                }
                                                                    )
                                ])
                ]),
        
    ]
)
@callback(
    Output('tbl', 'data'),
    Input('btn','n_clicks'),
    prevent_initial_call=True)

def refresh_data(value):
    global df_AS 

    client = MongoClient()

    #point the client at mongo URI
    client = MongoClient('mongodb+srv://xxxxxxx.530fwyt.mongodb.net')
    #select database
    db = client['test']
    #select the collection within the database
    df_AS = db.newassets
    df_AS = pd.DataFrame(list(df_AS .find()))
    client.close()
    return df_AS .to_dict('records')

Is there a reason you are declaring df_as as global?

The return statement needs to be on the level on indent as the rest of the function.

Can you run it in debug=True?

Apologies, that is just a quickly typed out example.

I can run the app no errors no sign of anything going wrong.

On the click of the button, the code executes and then display a date time in the html.div and the table fills as it should.

I just deployed the app to Render, and it deployed successfully, however on click of button, it loads but nothing happens after loading.

Hope that makes sense

What happens if you do this?

def refresh_data(value):
    #global df_AS

    client = MongoClient()

    #point the client at mongo URI
    client = MongoClient('mongodb+srv://xxxxxxx.530fwyt.mongodb.net')
    #select database
    db = client['test']
    #select the collection within the database
    #df_AS = db.newassets
    df_AS = pd.DataFrame(list(db.newassets.find()))
    client.close()
    return df_AS.to_dict('records')

Let me try removing the global, thank you I will revert shortly

Honestly, working and deploying plotly dash has been so frustrating so far

Deploying to something you don’t have much control over is, I normally deploy to servers that I can see and manipulate.

any recommendation on where to deploy?

Unfortunately removing the global variable did not work.

I think it might be time to cut my losses with this

Do you have any old computers lying around?

Do you need access to it external of your local network?

Unfortunately not and yes I need it external and embedded on a website

Bummer.

Check this out and see if it is helpful.

Thanks for all your assistance so far.

I’m trying to find what this means:

Worker with pid 93 was terminated due to signal 9

Its the error I receive on render.com

Signal 9 is just the code to terminate the worker.

You need to find why that is happening. Made use a try except wrapped around the mongodb that will print what is happening to the process.