question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dash Multi-Page App URLs do not always execute in the same order

See original GitHub issue

Link/Location/pathname + callbacks issues

It appears that the callbacks driven by pathname from a Location component do not always execute in the same order, leading to behavior like on the https://plot.ly/dash/urls page. Refreshing the urls page quickly and repeatedly leads to one of three cases:

  1. The home Plotly Dash page is displayed
  2. The home Plotly Dash page is flickered and then the URLs page is displayed
  3. The URLs page is displayed.

As a result, trying to have multi-page apps driven by the URL does not work, since callbacks either

  1. Receive the pathname as None (assuming that would map to case 1 above), or
  2. Do not update at all and pathname keeps the old value (assuming that would also map to case 1 above), or
  3. It updates after other callbacks have fired, leading to more requests than necessary (assuming that would map to case 2 above), or
  4. Execute as expected, but less than all of the time.

Example

The Multi-Page App URLs demo app confirms this behavior, as the initial page load keeps a ‘404’ on the page regardless if I directly go to /apps/app1 in the URL bar in Chrome.

I updated the display_page callback in the example to

@app.callback(Output('page-content', 'children'),
              [Input('url', 'pathname')])
def display_page(pathname):
    print(pathname)
    
    if pathname == '/pages/app1':
        return app1.layout
    elif pathname == '/pages/app2':
        return 'hi!'
    else:
        return '404'

This creates this stack trace:

 * Running on http://127.0.0.1:8889/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 246-948-826
127.0.0.1 - - [13/Sep/2017 15:14:49] "GET /apps/app1 HTTP/1.1" 200 -
127.0.0.1 - - [13/Sep/2017 15:14:50] "GET /_dash-layout HTTP/1.1" 200 -
127.0.0.1 - - [13/Sep/2017 15:14:50] "GET /_dash-dependencies HTTP/1.1" 200 -
127.0.0.1 - - [13/Sep/2017 15:14:50] "GET /favicon.ico HTTP/1.1" 200 -
None
127.0.0.1 - - [13/Sep/2017 15:14:50] "POST /_dash-update-component HTTP/1.1" 200 -
/apps/app1
127.0.0.1 - - [13/Sep/2017 15:14:50] "POST /_dash-update-component HTTP/1.1" 200 -

The page just renders 404. When I include a dcc.Link('CHANGE URL', href='/pages/app1') component on the page, and click on that, then it changes to the app1 content.

Am I doing something wrong? I copied the exact project structure for this demo, am also seeing it in other apps I’m working on, and there seems to be a clear issue with the https://plot.ly/dash/urls page.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
chriddypcommented, Sep 26, 2017

Nevermind, I’m able to reproduce the issue now:

import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html

import time

app = dash.Dash()
server = app.server
server.secret_key = 'test'


app.layout = html.Div([
    dcc.Location(id='location'),
    html.Div(id='content')
])


@app.callback(
    Output('content', 'children'),
    [Input('location', 'pathname')])
def display(path):
    print(path)
    if path is None:
        time.sleep(3)
        return ''
    elif path == '/':
        return 'index'
    else:
        return html.H3(path)


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

The issue occurs when the intial request with pathname=None takes longer than the follow up request with pathname='/page-1'. While dash initiates the requests in order, it looks like we aren’t doing a good job of dealing with request responses that come back out of order

1reaction
anthonymobilecommented, Mar 29, 2020

i just had this exact problem trying to build a similar url-based routing. this solution worked for me after trying every possible if-then, and try-except combination possible to trap the NoneType error. this bug appears to still be there. (active_route refers to a bus route, not an url route)

@app.callback(
        Output("page-content", "children"),
        [Input("url", "pathname")])
def display_page(pathname):

    if pathname is None:
        return 'Loading...'
    elif pathname == '/':
        active_route='87'
        return create_layout(app, routes, active_route)
    else:
        active_route=(pathname[1:])
        return create_layout(app, routes, active_route)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Multi-Page Apps and URL Support - Dash Plotly
Dash renders web applications as a "single-page app". When using dcc.Link , the application does not completely reload when navigating, making browsing very ......
Read more >
Multi-Page Dash Application - python - Stack Overflow
When I run the following code, everything works except it won't route to my /dash_1 or /dash_2 urls. Was wondering if someone could...
Read more >
Create Multipage Web App with Dash Pages - YouTube
In this video, you will receive a demo of a multipage app with Dash Pages, by the author of Dash and Plotly's Community...
Read more >
Beginner's Guide to Building a Multi-Page App using Dash ...
We are not health professionals or epidemiologists, and the… ... we can start working on the multi-page app — after all, a multi-page...
Read more >
Web Apps | Apps Script - Google Developers
For example, if the URL path ends in /exec/hello , the path info is hello . e.contextPath, Not used, always the empty string....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found