• 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

40.74
/canvasxpress/plot.py
1
from typing import Any, Union
1✔
2

3
from canvasxpress.canvas import CanvasXpress
1✔
4
from canvasxpress.render.environment import (
1✔
5
    get_target_context,
6
    CONTEXT_RSTUDIO,
7
    CONTEXT_SHINY,
8
    CONTEXT_DASH,
9
    CONTEXT_JUPYTER,
10
    CONTEXT_STREAMLIT,
11
    CONTEXT_BROWSER,
12
)
13
from canvasxpress.render.image import CXImage
1✔
14
from canvasxpress.render.json import CXJSON
1✔
15
from canvasxpress.render.popup import CXBrowserPopup
1✔
16

17
# Track the runtime context
18
_g_contexts_imported: list = []
1✔
19
_g_context = get_target_context()
1✔
20

21
# Pre-load Jupyter JS and CSS.
22
if _g_context == CONTEXT_JUPYTER:
1✔
NEW
23
    if not _g_context in _g_contexts_imported:
×
NEW
24
        from canvasxpress.render.jupyter import CXNoteBook
×
25

NEW
26
        CXNoteBook.display_canvasxpress_header()
×
27

28

29
def convert_from_reproducible_json(json: str) -> Union[None, CanvasXpress]:
1✔
30
    """
31
    Accepts a str with a reproducible JSON and returns a CanvasXpress object.
32
    """
33
    return CanvasXpress.from_reproducible_json(json)
×
34

35

36
def convert_to_reproducible_json(canvas: CanvasXpress) -> str:
1✔
37
    """
38
    Converts the CanvasXpress object into a reproducible JSON string.
39
    """
40
    return CXJSON.render_to_json(canvas)
×
41

42

43
def convert_to_image(canvas: CanvasXpress, type: str = "png") -> Union[None, bytes]:
1✔
44
    """
45
    Converts the CanvasXpress object to an image of the specified type.
46
    """
47
    converter = CXImage(canvas)
×
48
    candidates = converter.render(format=type)
×
49
    for conversion in candidates:
×
50
        if conversion.get("image", {}).get("format") == type:
×
51
            return conversion.get("image", {}).get("binary")
×
52

53

54
def show_in_browser(canvas: CanvasXpress) -> None:
1✔
55
    """
56
    Opens a browser and displays the canvas.
57
    """
58
    plotter = CXBrowserPopup(canvas)
×
59
    plotter.render()
×
60

61

62
def graph(canvas: CanvasXpress) -> Any:
1✔
63
    """
64
    Displays the CanvasXpress object as a visualized chart in a manner appropriate to the running context.
65

66
    To override an assumed context the ENV variable `CANVASXPRESS_TARGET_CONTEXT` can be set to one of the following
67
    values, in which case `show` will attempt to illustrate the chart of the set target.
68

69
    - rstudio
70
    - shiny
71
    - jupyter
72
    - dash
73
    - streamlit
74
    - browser
75

76
    :returns: An `object` or `None` depending on the target context.  In the case of `browser` a popup browser will
77
        be launched.`
78
    """
79
    if _g_context == CONTEXT_RSTUDIO:
1✔
NEW
80
        if not _g_context in _g_contexts_imported:
×
UNCOV
81
            from canvasxpress.render.shiny import CXShinyWidget
×
82

83
        plotter = CXShinyWidget(canvas)
×
84
        plotter._repr_rstudio_viewer_()
×
85

86
    elif _g_context == CONTEXT_SHINY:
1✔
NEW
87
        if not _g_context in _g_contexts_imported:
×
UNCOV
88
            from canvasxpress.render.shiny import CXShinyWidget
×
89

90
        plotter = CXShinyWidget(canvas)
×
91
        return plotter
×
92

93
    elif _g_context == CONTEXT_DASH:
1✔
NEW
94
        if not _g_context in _g_contexts_imported:
×
UNCOV
95
            from canvasxpress.render.dash import CXElementFactory
×
96

97
        plotter = CXElementFactory()
×
98
        return plotter.render(canvas)
×
99

100
    elif _g_context == CONTEXT_JUPYTER:
1✔
NEW
101
        if not _g_context in _g_contexts_imported:
×
UNCOV
102
            from canvasxpress.render.jupyter import CXNoteBook
×
103

104
        plotter = CXNoteBook(canvas)
×
105
        return plotter.render()
×
106

107
    elif _g_context == CONTEXT_STREAMLIT:
1✔
108
        if not _g_context in _g_contexts_imported:
1✔
109
            from canvasxpress.render import streamlit
1✔
110

111
        streamlit.plot(canvas)
1✔
112

NEW
113
    elif _g_context == CONTEXT_BROWSER:
×
114
        plotter = CXBrowserPopup(canvas)
×
115
        plotter.render()
×
116

117
    else:
118
        return """
×
119
        show() cannot identify the target context.  Either explicitly use a module from canvasxpress.render to 
120
        illustrate the CanvasXpress object, which requires the proper canvasxpress package option to be installed 
121
        (for example, pip install canvasxpress[shiny]) or set the environment variable CANVASXPRESS_TARGET_CONTEXT 
122
        to be one of rstudio, shiny, jupyter, streamlit, dash, or browser.
123
        """
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