• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

docinfosci / canvasxpress-python / 5f9854e2-3ace-4db8-8620-070e901a70eb

04 Apr 2025 03:41AM UTC coverage: 80.946% (+6.1%) from 74.814%
5f9854e2-3ace-4db8-8620-070e901a70eb

push

circleci

web-flow
JS local sources; Better Jupyter; Top level configs; R/JS attribute mapping

1.  Support local / custom CanvasXpress JS and CSS sources.
2.  Support top-level configuration declarations.
3.  Support mapping R/JS configuration key names to Pythonic editions.  For example, renderTo --> render_to.
4.  Improved Jupyter Notebook detection and pre-loading of CanvasXpress JS and CSS.

93 of 117 new or added lines in 10 files covered. (79.49%)

7 existing lines in 3 files now uncovered.

1933 of 2388 relevant lines covered (80.95%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/canvasxpress/render/shiny.py
1
import logging
×
NEW
2
import warnings
×
3

4
from shiny import ui
×
5

6
from canvasxpress.canvas import CanvasXpress
×
7

8
_cx_js_intermixed_template = """
×
9
<script type="text/javascript">
10
    @code@
11
</script>
12
"""
13

14
_cx_html_intermixed_template = """
×
15
<div>
16
    @canvasxpress_license@
17
    @canvase@
18
    @js_functions@
19
</div>
20
"""
21

22

23
def output_canvasxpress(id: str) -> ui.TagList:
×
24
    """
25
    Establishes an output reactive placeholder into which a CanvasXpress chart can be rendered.
26
    """
27
    if id is None:
×
NEW
28
        raise ValueError(
×
29
            "output_canvasxpress requires that id be of type str and not None."
30
        )
31

32
    elif not isinstance(id, str):
×
33
        raise TypeError("output_canvasxpress requires that id be of type str.")
×
34

35
    else:
NEW
36
        css_url = CanvasXpress.css_library_url()
×
NEW
37
        js_url = CanvasXpress.js_library_url()
×
38

39
        return ui.TagList(
×
40
            ui.head_content(
41
                ui.tags.link(href=css_url, rel="stylesheet"),
42
                ui.tags.script(src=js_url),
43
            ),
44
            ui.output_ui(id),
45
        )
46

47

48
class CXShinyWidget(object):
×
49
    """
50
    A Shiny for Python compatible class that can be used with Shiny syntax to establish CanvasXpress charts in the
51
    shiny UI.
52
    """
53

54
    def __init__(self, canvas: CanvasXpress):
×
55
        """
56
        Initializes this object with a valid reference to a CanvasXpress object.  An invalid reference will result in
57
        a ValueError (`None`) or TypeError (an object not of the type `CanvasXpress`) exception.
58
        :param canvas: `CanvasXpress` - A valid CanvasXpress object to be rendered in the shiny UI.
59
        """
60
        if canvas is None:
×
61
            raise ValueError("canvas must be an instance of CanvasXpress")
×
62

63
        elif not isinstance(canvas, CanvasXpress):
×
64
            raise TypeError("canvas must be an instance of CanvasXpress")
×
65

66
        else:
67
            self._canvas = canvas
×
68

69
    def _repr_html_(self):
×
70
        """
71
        Renders the object as Shiny compliant HTML.
72
        """
73
        # Get the HTML and JS assets.
74
        html_parts: dict = self._canvas.render_to_html_parts()
×
75

76
        # Provide the taglist.
77
        components = ui.TagList(
×
78
            [
79
                ui.div(
80
                    ui.HTML(
81
                        _cx_html_intermixed_template.replace(
82
                            "@canvasxpress_license@",
83
                            html_parts.get("cx_license", ""),
84
                        )
85
                        .replace(
86
                            "@canvase@",
87
                            html_parts["cx_canvas"],
88
                        )
89
                        .replace(
90
                            "@js_functions@",
91
                            _cx_js_intermixed_template.replace(
92
                                "@code@",
93
                                html_parts["cx_js"],
94
                            ),
95
                        )
96
                    ),
97
                ),
98
            ]
99
        )
100

101
        # Generate the chart DIV and provide it for rendering.
102
        return components.get_html_string()
×
103

104
    def _repr_rstudio_viewer_(self):
×
105
        """
106
        Renders the object as Shiny compliant HTML.
107
        """
108
        try:
×
109
            warnings.filterwarnings("ignore")
×
110
            logging.getLogger("rpy2.rinterface_lib.embedded").setLevel(logging.ERROR)
×
111

112
            from rpy2 import robjects
×
113
        except:
×
114
            robjects = None
×
115

116
        # Get the header assets.
NEW
117
        css_url = CanvasXpress.css_library_url()
×
NEW
118
        js_url = CanvasXpress.js_library_url()
×
119

120
        # Get the HTML and JS assets.
121
        html_parts: dict = self._canvas.render_to_html_parts()
×
122

123
        # Provide the taglist.
124
        components = [
×
125
            ui.div(
126
                ui.head_content(
127
                    ui.tags.link(href=css_url, rel="stylesheet"),
128
                    ui.tags.script(src=js_url),
129
                ),
130
                ui.HTML(
131
                    _cx_html_intermixed_template.replace(
132
                        "@canvasxpress_license@",
133
                        html_parts.get("cx_license", ""),
134
                    )
135
                    .replace(
136
                        "@canvase@",
137
                        html_parts["cx_canvas"],
138
                    )
139
                    .replace(
140
                        "@js_functions@",
141
                        _cx_js_intermixed_template.replace(
142
                            "@code@",
143
                            html_parts["cx_js"],
144
                        ),
145
                    )
146
                ),
147
            ),
148
        ]
149

150
        # Generate the chart DIV and provide it for rendering.
151
        ui_components = ui.TagList(components)
×
152
        html = ui_components.get_html_string().replace("\n", "")
×
153

154
        robjects.globalenv["html"] = html
×
155
        robjects.r(
×
156
            """
157
            tf <- tempfile(fileext = ".html")
158
            writeLines(html, tf)
159
            rstudioapi::viewer(tf)
160
            """
161
        )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc