• 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

96.0
/canvasxpress/render/streamlit.py
1
import streamlit.components.v1 as components
1✔
2

3
from canvasxpress.canvas import CanvasXpress
1✔
4

5
_cx_iframe_padding = 50
1✔
6

7
_cx_fx_template = """
1✔
8
<script type="text/javascript">
9
    @code@
10
</script>
11
"""
12

13
_cx_html_template = """
1✔
14
<html>
15
    <head>
16
        <meta charset="UTF-8">
17
        <title>CanvasXpress</title>
18

19
        <!-- 1. Include the CanvasXpress library -->
20
        @canvasxpress_license@
21
        <link 
22
                href='@css_url@' 
23
                rel='stylesheet' 
24
                type='text/css'
25
                referrerpolicy='origin-when-cross-origin'
26
        />
27
        <script 
28
                src='@js_url@' 
29
                type='text/javascript'>
30
                referrerpolicy='origin-when-cross-origin'
31
        </script>
32
    </head>
33
    <body>
34
        <!-- 3. DOM element where the visualization will be displayed -->
35
        @canvases@
36
        <!-- 2. Include script to initialize object -->
37
        @js_functions@
38
     </body>
39
</html>
40
"""
41

42

43
def _get_chart_display_code(cx: CanvasXpress) -> tuple:
1✔
44
    """
45
    Get the html display code and iframe dimensions for rendering a CanvasXpress
46
    object in a Streamlit application.
47
    :param cx: `CanvasXpress`
48
        The `CanvasXpress` object to be rendered.
49
    :returns: `tuple` of html, iframe_width, iframe_height
50
    """
51
    html_parts = cx.render_to_html_parts()
1✔
52
    canvases = html_parts["cx_canvas"]
1✔
53
    js_functions = _cx_fx_template.replace("@code@", html_parts["cx_js"])
1✔
54

55
    cx_license = ""
1✔
56
    if html_parts.get("cx_license"):
1✔
NEW
57
        cx_license = html_parts["cx_license"]
×
58

59
    css_url = CanvasXpress.css_library_url()
1✔
60
    js_url = CanvasXpress.js_library_url()
1✔
61

62
    html = (
1✔
63
        _cx_html_template.replace("@canvases@", canvases)
64
        .replace("@canvasxpress_license@", cx_license)
65
        .replace("@js_functions@", js_functions)
66
        .replace("@css_url@", css_url)
67
        .replace("@js_url@", js_url)
68
    )
69

70
    iframe_width = cx.width + _cx_iframe_padding
1✔
71
    iframe_height = cx.height + _cx_iframe_padding
1✔
72

73
    return html, iframe_width, iframe_height
1✔
74

75

76
def plot(cx: CanvasXpress) -> None:
1✔
77
    """
78
    Renders the provided CanvasXpress object for display in a Streamlit application.
79
    :param cx: `CanvasXpress`
80
        The `CanvasXpress` object to be rendered.
81
    :returns: `None` or raises a `TypeError` exception if `cx` is not a CanvasXpress
82
        object.
83
    """
84
    if cx is None:
1✔
85
        return None
1✔
86

87
    elif not isinstance(cx, CanvasXpress):
1✔
88
        raise TypeError(f"Argument 'cx' is not a CanvasXpress object")
1✔
89

90
    html, iframe_width, iframe_height = _get_chart_display_code(cx)
1✔
91

92
    components.html(html, width=iframe_width, height=iframe_height)
1✔
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