Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Rotating dashboard playlist recipe

We have been using Grafana for some dashboards. One nice feature of Grafana is the ability to define dashboard ‘playlists’, which consist of a series of dashboards that rotate on a timer.

Below is a basic recipe for creating rotating layouts (dashboard or otherwise) using Dash (feedback and improvements are welcome):

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

seconds_per_layout = 5

app = dash.Dash()

app.layout = html.Div(children=[
    dcc.Interval(
        id='interval-component',
        interval=seconds_per_layout * 1000,  # in milliseconds
        n_intervals=0
    ),
    html.Div(id='layout-container'),
])

layout_a = html.Div(children=[
    html.H1('Layout A')
])

layout_b = html.Div(children=[
    html.H1('Layout B')
])

layout_c = html.Div(children=[
    html.H1('Layout C')
])


layout_playlist = [
    layout_a,
    layout_b,
    layout_c,
]

@app.callback(
    Output('layout-container', 'children'),
    [
        Input('interval-component', 'n_intervals'),
    ]
)
def layout_playlist_update(n_intervals):
    # Get layout index
    current_layout_index = n_intervals % len(layout_playlist)

    # Return layout
    return layout_playlist[current_layout_index]


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