File size: 1,317 Bytes
42ee455
 
 
e41c0e1
42ee455
 
64e7b89
42ee455
64e7b89
b699ae9
42ee455
 
64e7b89
42ee455
 
 
b699ae9
64e7b89
42ee455
 
64e7b89
b699ae9
 
 
 
 
 
 
64e7b89
b699ae9
 
 
42ee455
b699ae9
64e7b89
 
 
 
42ee455
b699ae9
42ee455
 
 
b699ae9
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
"""Contains custom components used inside the dashboard."""

from typing import Literal
from urllib.parse import quote

import dash_bootstrap_components as dbc
import dash_mantine_components as dmc
import vizro.models as vm
from dash import html


class CodeClipboard(vm.VizroBaseModel):
    """Code clipboard based on `dmc.CodeHighlight` with optional PyCafe link."""

    type: Literal["code_clipboard"] = "code_clipboard"
    code: str
    mode: Literal["vizro", "plotly"]
    language: str = "python"

    def build(self):
        """Build and return the complete code clipboard component."""
        pycafe_link = dbc.Button(
            [
                "Edit code live on PyCafe",
                html.Span("open_in_new", className="material-symbols-outlined open-in-new"),
            ],
            href=f"https://py.cafe/snippet/vizro/v1#code={quote(self.code)}",
            target="_blank",
            class_name="pycafe-link",
        )

        return html.Div(
            [
                pycafe_link if self.mode == "vizro" else None,
                dmc.CodeHighlight(
                    code=self.code,
                    language=self.language,
                ),
            ],
            className="code-clipboard-container",
        )


vm.Container.add_type("components", CodeClipboard)