Py.Cafe

stichbury/

vizro-example-gallery

Vizro Example Gallery

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""Vizro Example Gallery"""

from typing import Literal, Optional

import dash
from dash import get_asset_url
import dash_bootstrap_components as dbc
import flask_talisman
import vizro.models as vm
from add_security_headers import set_security_headers_config
from dash import dcc, html
from vizro import Vizro
from werkzeug.middleware.proxy_fix import ProxyFix

try:
    from pydantic.v1 import Field
except ImportError:  # pragma: no cov
    from pydantic import Field


class Image(vm.VizroBaseModel):
    type: Literal["image"] = "image"
    src: str
    href: Optional[str] = None

    def build(self):
        if self.href:
            return html.A(html.Img(id=self.id, src=dash.get_asset_url(self.src), className="image"), href=self.href, target="_blank")
        else:
            return html.Img(id=self.id, src=dash.get_asset_url(self.src), className="image")


class Markdown(vm.VizroBaseModel):
    """Markdown component."""

    type: Literal["markdown"] = "markdown"
    text: str = Field(
        ...,
        description="Markdown string to create card title/text that should adhere to the CommonMark Spec.",
    )
    classname: str = ""

    def build(self):
        """Returns a markdown component with an optional classname."""
        return dcc.Markdown(
            id=self.id,
            children=self.text,
            dangerously_allow_html=False,
            className=self.classname,
        )


class FlexContainer(vm.Container):
    """Custom flex `Container`."""

    type: Literal["flex_container"] = "flex_container"
    title: str = None  # Title exists in vm.Container but we don't want to use it here.

    def build(self):
        """Returns a flex container."""
        return html.Div(
            id=self.id,
            children=[component.build() for component in self.components],
            className="flex-container",
        )


vm.Page.add_type("components", FlexContainer)
vm.Container.add_type("components", Image)
FlexContainer.add_type("components", Markdown)

## PAGE
page = vm.Page(
    title="A toolkit for creating modular data visualization applications",
    components=[
        FlexContainer(
            components=[
                Markdown(
                    text="""
                        ![](assets/images/general/VizroLogo_Full_colour_White_transparentBG.svg#logo-img)
                        # Welcome to the Vizro examples gallery
                        ### where we showcase a range of Vizro dashboards for you to try out
                        
                         
                        
                        #### What is Vizro? It's an open source Python framework for creating modular data visualization applications.
                        """,
                    classname="info-text",
                ),
                vm.Container(
                    title="",
                    layout=vm.Layout(grid=[[0, 1], [0, 2], [0, 3]], col_gap="80px"),
                    components=[
                        Image(src="images/general/vizro_spash_teaser.gif"),
                        vm.Card(
                            text="""
                                ![](assets/logo.svg#card-img)

                                ### Live Demo

                                A Vizro example dashboard in practice.
                            """,
                            href="https://huggingface.co/spaces/vizro/demo-kpi",
                        ),
                        vm.Card(
                            text="""
                                ![](assets/images/general/hf-logo.svg#card-img)

                                ### Gallery

                                Check out all our other demos on Hugging Face.
                            """,
                            href="https://huggingface.co/vizro",
                        ),
                        vm.Card(
                            text="""
                                ![](assets/images/general/pycafe-cropped.svg#card-img)

                                ### py.cafe

                                Try out Vizro live - without the need to install anything!
                            """,
                            href="https://py.cafe/snippet/vizro/v1",
                        ),
                    ],
                ),
                Markdown(
                    text="""## And now there's Vizro-AI: Vizro enhanced with genAI.""",
                    classname="info-text",
                ),
                vm.Container(
                    title="",
                    layout=vm.Layout(grid=[[1, 0], [2, 0], [-1, 0]], col_gap="80px"),
                    components=[
                        Image(src="images/general/readme_animation.gif"),
                        vm.Card(
                            text="""
                                ![](assets/images/general/pycafe-cropped.svg#card-img)

                                ### Live Chart Creation

                                Try out Vizro-AI charts live.
                            """,
                            href="https://py.cafe/app/vizro-official/vizro-ai-charts",
                        ),
                        vm.Card(
                            text="""
                                ![](assets/images/general/colab.png#card-img)

                                ### Live Dashboard Creation

                                Try out Vizro-AI dashboards live.
                            """,
                            href="https://colab.research.google.com/github/mckinsey/vizro/blob/main/vizro-ai/examples/dashboard_by_vizro_ai.ipynb",
                        ),
                    ],
                ),
                Markdown(
                    text="""
                    ## Introducing our visual vocabulary: A collection of beautiful charts 
                    
                    #### learn when to use them and get sample code to create them in Plotly and Vizro
                    """,
                    classname="info-text",
                ),
                vm.Container(
                    title="",
                    components=[
                        Image(src="images/general/vizro-visual-vocabulary.gif", href="https://huggingface.co/spaces/vizro/demo-visual-vocabulary"),
                    ],
                ),
            ]
        ),
    ],
)

dashboard = vm.Dashboard(pages=[page], title="Vizro")
app = Vizro(assets_folder="./assets").build(dashboard)
app.dash.layout.children.append(
    html.Div(
        [
            dbc.NavLink("Contact us", href="https://github.com/mckinsey/vizro/issues"),
            dbc.NavLink("GitHub", href="https://github.com/mckinsey/vizro"),
            dbc.NavLink("Docs", href="https://vizro.readthedocs.io/en/stable/"),
            html.Div(
                [
                    "Made using ",
                    html.Img(
                        src=get_asset_url("logo.svg"), id="banner", alt="Vizro logo"
                    ),
                    "vizro",
                ],
            ),
        ],
        className="anchor-container",
    )
)
server = app.dash.server

server.wsgi_app = ProxyFix(
    server.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1, x_port=1
)

# ADD SECURITY HEADERS
csp = {
    "default-src": ["'self'", "https://cdn.plot.ly/world_110m.json", "data:"],
    "img-src": ["'self'", "data:", "blob:"],
    "font-src": [
        "'self'",
        "fonts.gstatic.com",
        "https://vizro.alpha.mckinsey.digital",
        "https://vizro.mckinsey.com/",
        "data:",
    ],
    "style-src": [
        "'self'",
        "'unsafe-inline'",
        "https://fonts.googleapis.com",
        "https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css",
    ],
    "script-src": [
        "'self'",
        "'unsafe-inline'",
        "'unsafe-eval'",
        "'sha256-PLASfTOXCNC/zzYs5pfSve2C3jtMetNBZ+OTvwQg4ME='",
    ],
}

http_security_headers_config = {
    "force_https": True,
    "content_security_policy": csp,
}

http_security_headers_config = set_security_headers_config(
    headers_config=http_security_headers_config, app=app.dash
)
flask_talisman.Talisman(server, **http_security_headers_config)  # type: ignore



if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7065)