Py.Cafe

panel-org/

plotly-style-dashboard

Plotly Interactive Visualization

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import pandas as pd
import param
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio

import panel as pn

pn.extension("plotly", sizing_mode="stretch_width")

ACCENT = "#F08080"

DEFAULT_TEMPLATE = "plotly_dark" if pn.config.theme == "dark" else "plotly_white"
DEFAULT_TRANSPARENT_BACKGROUND = pn.config.theme == "dark"
TEMPLATES = sorted(pio.templates)

ORDERS_DATA = pd.DataFrame(
    [
        ("Monday", 7),
        ("Tuesday", 4),
        ("Wednesday", 9),
        ("Thursday", 4),
        ("Friday", 4),
        ("Saturday", 4),
        ("Sunday", 4),
    ],
    columns=["Day", "Orders"],
)

Z_DATA_URL = "https://raw.githubusercontent.com/plotly/datasets/master/api_docs/mt_bruno_elevation.csv"
Z_DATA = pn.state.as_cached("z_data", pd.read_csv, filepath_or_buffer=Z_DATA_URL)


class PlotlyStyleDashboard(param.Parameterized):
    template = param.Selector(
        default=DEFAULT_TEMPLATE,
        objects=TEMPLATES,
        doc="The name of the plotly template to use.",
    )
    transparent_background = param.Boolean(
        default=DEFAULT_TRANSPARENT_BACKGROUND,
        doc="If True the Plot plot background will be transparent.",
    )
    color = param.Color(default=ACCENT)

    @param.depends("template", "color", "transparent_background")
    def orders_plot(self):
        fig = px.line(
            ORDERS_DATA,
            x="Day",
            y="Orders",
            template=self.template,
            color_discrete_sequence=[self.color],
            title=f"Orders in a '{self.template}' template",
        )
        fig.update_traces(
            mode="lines+markers", marker=dict(size=10), line=dict(width=4)
        )

        fig.layout.autosize = True
        if self.transparent_background:
            fig.update_layout(
                paper_bgcolor="rgba(0,0,0,0)",
                plot_bgcolor="rgba(0,0,0,0)",
            )
        return fig

    @param.depends("template", "transparent_background")
    def z_plot(self):
        fig = go.Figure(
            data=go.Surface(z=Z_DATA.values),
            layout=go.Layout(
                title="Mt Bruno Elevation",
            ),
        )
        fig.update_layout(
            template=self.template,
            title=f"Mt Bruno Elevation in a '{self.template}' template",
        )

        fig.layout.autosize = True
        if self.transparent_background:
            fig.update_layout(
                paper_bgcolor="rgba(0,0,0,0)",
                plot_bgcolor="rgba(0,0,0,0)",
            )
        return fig

    def controls(self):
        return pn.Column(
            self.param.template,
            self.param.transparent_background,
            self.param.color,
        )

    def components(self):
        with pn.config.set(sizing_mode="stretch_both"):
            return pn.Column(self.orders_plot, self.z_plot)

    def servable(self):
        pn.template.FastListTemplate(
            title="Plotly Style Dashboard",
            sidebar=[self.controls()],
            main=[self.components()],
            main_layout=None,
            accent=ACCENT,
        ).servable()


PlotlyStyleDashboard().servable()