etjkai
January 27, 2020, 3:54am
1
Making reference to the tutorial: https://dash.plot.ly/urls
With the following code for index.py
, when the below is run, there is no pathname
for dcc.Location
, and the code always yields 404.
How is it intended to work?
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from app import app
from apps import app1, app2
app.layout = html.Div([
dcc.Location(id='url', refresh=False),
html.Div(id='page-content')
])
@app.callback(Output('page-content', 'children'),
[Input('url', 'pathname')])
def display_page(pathname):
if pathname == '/apps/app1':
return app1.layout
elif pathname == '/apps/app2':
return app2.layout
else:
return '404'
if __name__ == '__main__':
app.run_server(debug=True)```
Emil
January 27, 2020, 6:02am
2
The code that you pasted seems to work as intended. If you go to
http://127.0.0.1:8050/apps/app1
you will get the layout of app1. If you just open the base url,
http://127.0.0.1:8050/
you will get 404. The “pathnam” is set by the url in your browser.
etjkai
January 27, 2020, 6:17am
3
That’s right, if I manually key in http://127.0.0.1:8050/apps/app1 , it does work.
Is it possible to set the landing page as http://127.0.0.1:8050/apps/app1 , instead of just http://127.0.0.1:8050/ ?
This is as I’m thinking that if I host the web application, the very first page would be http://127.0.0.1:8050/ right?
Emil
January 27, 2020, 10:01am
4
Yes, you just add routes to the if statement. The base url is no different,
if pathname == '/': # this is the base url
return app1.layout # here you put what you want do show for this url (in this case, the base url)
if pathname == '/apps/app1':
return app1.layout
elif pathname == '/apps/app2':
return app2.layout
else:
return '404'
1 Like
etjkai
January 27, 2020, 3:11pm
5
Thanks Emil, understood clearly!