Dash Multi-page Layout
In modern web application development, Single Page Applications (SPA) and Multi Page Applications (MPA) are two common architectural patterns.
A single page application usually provides a smooth user experience by dynamically loading content, while a multi-page application organizes content through multiple independent pages.
Single Page Application vs Multi Page Application
In a single page application, all content is dynamically loaded and updated within one page, and the user switches between different views by clicking links or buttons. The advantage of this architecture is a smooth user experience and fast page loading, but the disadvantage is that as the application grows, the code structure may become complex.
A multi-page application organizes content through multiple independent pages, each with its own URL and layout. The advantage of this architecture is clear code structure and easy maintenance, but the disadvantage is a certain loading delay when switching pages.
Implementation of Dash Multi-page Layout
Implementing a multi-page layout in Dash can be done through the dcc.Location and dcc.Link components.
dcc.Location is used to track the current page's URL, while dcc.Link is used to navigate between pages. By combining with callback functions, different page content can be dynamically loaded based on the URL.
-
Use
dcc.Locationanddcc.Linkcan implement a multi-page layout. -
Dynamically load page content through the callback function based on
pathname. -
Combined with modular layouts and URL parameters, more complex multi-page applications can be built.
Basic Structure of Multi-page Layout
The core idea of a multi-page layout is to dynamically load different page content based on the URL's path (pathname).
The following are the basic steps to implement a multi-page layout:
Define the page layout:
-
The layout of each page can be defined as a function or a variable.
-
For example,
home_layoutrepresents the home page,about_layoutrepresents the about page.
Use dcc.Location:
-
dcc.Locationcomponent is used to track the current page's URL. -
Pass
pathnameattribute gets the current page's path.
Use dcc.Link:
-
dcc.Linkcomponent is used to create page navigation links. -
Pass
hrefattribute specifies the path of the target page.
Dynamically load page content:
-
Use a callback function based on
pathnameto dynamically return the corresponding page layout.
Example
from
dash
import
Dash
,
html
,
dcc
,
Input
,
Output
Create the Dash App
app
=
Dash
(
name
)
app.
layout
=
html.
Div
(
[
dcc.
Location
(
id
=
'url'
,
refresh
=
False
)
,
html.
Div
(
id
=
'page-content'
)
]
)
In the code above,dcc.Location the component's id to url, refresh parameter is set to False, indicating that the page is not refreshed when the URL changes.html.Div(id='page-content') is a placeholder used to display the content of different pages.
Define the page layout
Next, we need to define the layouts of different pages. The layout of each page can be an independent function or variable. For example, we can define the layouts of two pages:index_page and page1.
Example
index_page
=
html.
Div
(
[
html.
H1
(
// Add browsing records
)
,
dcc.
Link
(
'Go to Page 1'
,
href
=
'/page1'
)
]
)
page1
=
html.
Div
(
[
html.
H1
(
"Page 1"
)
,
dcc.
Link
(
'Back to Home'
,
href
=
'/'
)
]
)
In the code above,index_page is the layout of the home page, containing a title and a link; clicking the link navigates to page1.page1 is the layout of Page 1, also containing a title and a link back to the home page.
Dynamically load the page layout
Finally, we need to dynamically load different page layouts through a callback function based on URL changes. The callback function's role is to decide which page layout to display based on dcc.Location the component's pathname the attribute.
Example
@
app.
callback
(
Output
(
'page-content'
,
'children'
)
,
[
Input
(
'url'
,
'pathname'
)
]
)
def
display_page
(
pathname
)
:
if
pathname
==
'/page1'
:
return
page1
else
:
return
index_page
In the code above,display_page The function receives pathname as input, and based on pathname returns the corresponding page layout. If pathname Is /page1, return page1 's layout; otherwise it returns index_page 's layout.
Complete example
The following is a complete Dash multi-page layout example, including the home page, the about page, and a 404 page.
Example
from
dash
import
Dash
,
html
,
dcc
,
Input
,
Output
Create the Dash App
app
=
Dash
(
name
)
Define the home page layout
home_layout
=
html.
Div
(
[
html.
H1
(
"Home"
)
,
html.
P
(
"Welcome to the home page!"
)
,
dcc.
Link
(
'Go to About Page'
,
href
=
'/about'
)
]
)
Define the about page layout
about_layout
=
html.
Div
(
[
html.
H1
(
"About Page"
)
,
html.
P
(
"This is the content of the about page."
)
,
dcc.
Link
(
'Back to Home'
,
href
=
'/'
)
]
)
Define the 404 page layout
not_found_layout
=
html.
Div
(
[
html.
H1
(
"404 - Page Not Found"
)
,
html.
P
(
"The page you visited does not exist."
)
,
dcc.
Link
(
'Back to Home'
,
href
=
'/'
)
]
)
Define the application's layout
app.
layout
=
html.
Div
(
[
dcc.
Location
(
id
=
'url'
,
refresh
=
False
)
,
Used to track the URL
html.
Div
(
id
=
'page-content'
)
Used to dynamically load page content
]
)
Define the Callback Function
@
app.
callback
(
Output
(
'page-content'
,
'children'
)
,
Output to the children property of the Div with id 'page-content'
Input
(
'url'
,
'pathname'
)
The input comes from the pathname property of the Location component with id 'url'
)
def
display_page
(
pathname
)
:
if
pathname
==
'/'
:
return
home_layout
Display the home page
elif
pathname
==
'/about'
:
return
about_layout
Display the about page
else
:
return
not_found_layout
Display the 404 page
Run the application
if
name
==
'main'
:
app.
run
(
debug
=
True
)
After running the application, visit http://127.0.0.1:8050/ to see the home page content; click "Go to About Page" to navigate to the about page; click "Back to Home" to return to the home page.

Code Explanation
Page Layout:
-
home_layout: the layout of the home page, containing a title, a description, and a link to the about page. -
about_layout: the layout of the about page, containing a title, a description, and a link to the home page. -
not_found_layout: the layout of the 404 page, used to handle non-existent paths.
dcc.Location:
-
id='url': used to track the current page's URL. -
refresh=False: disables page refresh, achieving a Single Page Application (SPA) effect.
dcc.Link:
-
is used to create page navigation links,
hrefattribute specifies the path of the target page.
Callback Function:
-
based on
pathname's value, dynamically returns the corresponding page layout. -
If the path is
/, returnhome_layout. -
If the path is
/about, returnabout_layout. -
If the path does not match, return
not_found_layout.
Extend
For more complex multi-page applications, you can combine the following methods to optimize the code structure:
1. Use a modular layout
Define each page's layout in a separate module for easier maintenance and extension.
Create pages/home.py:
Example
from
dash
import
html
,
dcc
layout
=
html.
Div
(
[
html.
H1
(
"Home"
)
,
html.
P
(
"Welcome to the home page!"
)
,
dcc.
Link
(
'Go to About Page'
,
href
=
'/about'
)
]
)
Create pages/about.py:
Example
from
dash
import
html
,
dcc
layout
=
html.
Div
(
[
html.
H1
(
"About Page"
)
,
html.
P
(
"This is the content of the about page."
)
,
dcc.
Link
(
'Back to Home'
,
href
=
'/'
)
]
)
Import the layouts in the main application:
from pages.home import layout as home_layout
from pages.about import layout as about_layout
2. Use URL parameters
Pass URL parameters through the search or pathname property of dcc.Location to achieve more flexible page logic.
Example
@
app.
callback
(
Output
(
'page-content'
,
'children'
)
,
Input
(
'url'
,
'pathname'
)
)
def
display_page
(
pathname
)
:
if
pathname
==
'/'
:
return
home_layout
elif
pathname.
startswith
(
'/about'
)
:
Parse URL parameters
return
about_layout
else
:
return
not_found_layout