A plugin to simplify creating multi-page Dash apps

Overview

Multi-Page Dash App Plugin

A plugin to simplify creating multi-page Dash apps. This is a preview of functionality that will of Dash 2.1.

Background

The goal of this plugin is to remove as much boilerplate as possible when creating multi-page Dash apps.

This plugin allows users to simply place their layouts in pages/ and call dash.register_page with the desired URL path of that page.

This plugin will automatically:

  • Create the URL routing callback
  • Add page information to dash.page_registry that can be used when creating navigation bars
  • Set validate_layout accordingly so that you don't need to suppress_callback_exceptions for simple multi-page layouts
  • Set the order of dash.page_registry based off order and the filename
  • Set </code> and and their social media equivalents accordingly in the index_string of the HTML that is served on page-load
  • Set a clientside callback to update the </code> as you navigate pages with dcc.Link
  • Set the social media meta image accordingly based off of images available in assets

Usage

Option 1 - In this project

Clone this repo and then run python app.py. pages_plugin.py is the functionality that will become part of the dash library. The pages/ folder demonstrates examples of how to use dash.register_page.

Option 2 - In your own projects

  1. Copy pages_plugin.py into your project folder. In the future, this will be part of dash and you won't need to copy this file.
  2. In app.py, pass the plugin into Dash:
import pages_plugin

app = Dash(__name__, plugins=[pages_plugin])
  1. Create a folder called pages/ and place your app layouts in files within that folder. Each file needs to:
  • Define layout. This can be a variable or function that returns a component
  • Call dash.register_page(__name__) to tell pages_plugin that this page should be part of the multi-page framework

For example: pages/historical_outlook.py

import dash
from dash import html

dash.register_page(__name__)

def layout():
    return html.Div('This page is the historical outlook')

dash.register_page will can accept various arguments to customize aspects about the page like path (the URL of the page), title (the browser tab's title of the page), and more. See the API reference below for details.

pages/home.py

import dash
from dash import html

dash.register_page(
    __name__,
    path='/',
    title='Analytics App'
)

def layout():
    return html.Div('This is the home page')
  1. Modify app.layout to display the URLs for page navigation and include the container that displays the page's content.
  • dash.page_registry: The page URLs can be found in dash.page_registry. This is an OrderedDict with keys being the page's module name (e.g. pages.historical_outlook) and values being a dictionary containing keys path, name, order, title, description, image, and layout. This page_registry is populated from calling dash.register_page within pages/.
  • pages_plugin.page_container: This component defines where the page's content will render on page navigation.

app.py

import pages_plugin

app = Dash(__name__, plugins=[pages_plugin])

app.layout = html.Div([
    # Display the URLs by looping through `dash.page_registry`
    # In practice, this might be a `ddk.Header` or a `dbc.NavbarSimple`
    html.Div([dcc.Link(page['name'], href=page['path']) for page in dash.page_registry),
    
    html.Hr()
    
    # Set the container where the page content will be rendered into on page navigation
    pages_plugin.page_container
])

Refrence

dash.register_page

def register_page(
    module,
    path=None,
    name=None,
    order=None,
    title=None,
    description=None,
    image=None,
    layout=None,
    **kwargs
):

Assigns the variables to dash.page_registry as an OrderedDict (ordered by order).

dash.page_registry is used by pages_plugin to set up the layouts as a multi-page Dash app. This includes the URL routing callbacks (using dcc.Location) and the HTML templates to include title, meta description, and the meta description image.

dash.page_registry can also be used by Dash developers to create the page navigation links or by template authors.

  • module: The module path where this page's layout is defined. Often __name__.

  • path: URL Path, e.g. / or /home-page. If not supplied, will be inferred from module, e.g. pages.weekly_analytics to /weekly-analytics

  • name: The name of the link. If not supplied, will be inferred from module, e.g. pages.weekly_analytics to Weekly analytics

  • order: The order of the pages in page_registry. If not supplied, then the filename is used and the page with path / has order 0

  • title: The name of the page . That is, what appears in the browser title. If not supplied, will use the supplied name or will be inferred by module, e.g. pages.weekly_analytics to Weekly analytics

  • description: The . If not supplied, then nothing is supplied.

  • image: The meta description image used by social media platforms. If not supplied, then it looks for the following images in assets/:

    • A page specific image: assets/. is used, e.g. assets/weekly_analytics.png
    • A generic app image at assets/app.
    • A logo at assets/logo.
  • layout: The layout function or component for this page. If not supplied, then looks for layout from within the supplied module.

  • **kwargs: Arbitrary keyword arguments that can be stored

page_registry stores the original property that was passed in under supplied_ and the coerced property under . For example, if this was called:

register_page(
    'pages.historical_outlook',
    name='Our historical view',
    custom_key='custom value'
)

Then this will appear in page_registry:

OrderedDict([
    (
        'pages.historical_outlook', 
        dict(
            module='pages.historical_outlook',
            
            supplied_path=None,
            path='/historical-outlook',
            
            supplied_name='Our historical view',
            name='Our historical view',
            
            supplied_title=None,
            title='Our historical view'
            
            supplied_description=None,
            description='Our historical view',
            
            supplied_order=None,
            order=1,
            
            supplied_layout=None,
            layout=<function pages.historical_outlook.layout>,
            
            custom_key='custom value'
        )
    ),
])
Owner
Plotly
Plotly
More routines for operating on iterables, beyond itertools

More Itertools Python's itertools library is a gem - you can compose elegant solutions for a variety of problems with the functions it provides. In mo

2.9k Jan 06, 2023
A hashtag from string extract python module

A hashtag from string extract python module

Fayas Noushad 3 Aug 10, 2022
A collection of custom scripts for working with Quake assets.

Custom Quake Tools A collection of custom scripts for working with Quake assets. Features Script to list all BSP files in a Quake mod

Jason Brownlee 3 Jul 05, 2022
Extends the pyranges module with operations on joined genomic intervals

tiedpyranges Extends the pyranges module with operations on joined genomic intervals (e.g. exons of same transcript) Install with: pip install tiedpyr

Marco Mariotti 4 Aug 05, 2022
MongoDB utility to inflate the contents of small collection to a new larger collection

MongoDB Data Inflater ("data-inflater") The data-inflater tool is a MongoDB utility to automate the creation of a new large database collection using

Paul Done 3 Nov 28, 2021
Dynamic key remapper for Wayland Window System, especially for Sway

wayremap Dynamic keyboard remapper for Wayland. It works on both X Window Manager and Wayland, but focused on Wayland as it intercepts evdev input and

Kay Gosho 50 Nov 29, 2022
Create C bindings for python automatically with the help of libclang

Python C Import Dynamic library + header + ctypes = Module like object! Create C bindings for python automatically with the help of libclang. Examples

1 Jul 25, 2022
Export watched content from Tautulli to the Letterboxd CSV Import Format

Export watched content from Tautulli to the Letterboxd CSV Import Format

Evan J 5 Aug 31, 2022
Format Norminette Output!

Format Norminette Output!

7 Apr 19, 2022
Retrying library for Python

Tenacity Tenacity is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simplify the task of adding retry behavior to just

Julien Danjou 4.3k Jan 05, 2023
PyResToolbox - A collection of Reservoir Engineering Utilities

pyrestoolbox A collection of Reservoir Engineering Utilities This set of functio

Mark W. Burgoyne 39 Oct 17, 2022
Python humanize functions

humanize This modest package contains various common humanization utilities, like turning a number into a fuzzy human-readable duration ("3 minutes ag

Jason Moiron 1.6k Jan 01, 2023
This is Cool Utility tools that you can use in python.

This is Cool Utility tools that you can use in python. There are a few tools that you might find very useful, you can use this on pretty much any project and some utils might help you a lot and save

Senarc Studios 6 Apr 18, 2022
cpp20.py is a Python script to compile C++20 code using modules.

cpp20.py is a Python script to compile C++20 code using modules. It browses the source files to determine their dependencies. Then, it compiles then in order using the correct flags.

Julien VERNAY 6 Aug 26, 2022
Password generator

Password generator technologies used What is? It is Password generator How to Download? Download on releases Clone repo git clone https://github.com/m

Miek 1 Nov 02, 2021
A random cats photos python module

A random cats photos python module

Fayas Noushad 6 Dec 01, 2021
python script to generate color coded resistor images

Resistor image generator I got nerdsniped into making this. It's not finished at all, and the code is messy. The end goal it generate a whole E-series

MichD 1 Nov 12, 2021
Python based tool to extract forensic info from EventTranscript.db (Windows Diagnostic Data)

EventTranscriptParser EventTranscriptParser is python based tool to extract forensically useful details from EventTranscript.db (Windows Diagnostic Da

P. Abhiram Kumar 24 Nov 18, 2022
Experimental python optimistic rollup fraud-proof generation

Macula Experimental python optimistic rollup fraud-proof generation tech by @protolambda. Working on a python version for brevity and simplicity. See

Diederik Loerakker 30 Sep 01, 2022
Similar looking domain detection using python fuzzywuzzy

Major cause of phishing and BEC incident is similar looking domain, if you detect it early, you can prevent incidents early, python fuzzywuzzy module let you do that

2 Nov 07, 2021