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

docinfosci / canvasxpress-python / ce08ebab-6478-4081-bd9a-6762629aabf9

01 Apr 2025 05:21PM UTC coverage: 81.122% (-0.09%) from 81.208%
ce08ebab-6478-4081-bd9a-6762629aabf9

push

circleci

docinfosci
Pre-load JS and CSS headers for Jupyter NoteBooks

12 of 20 new or added lines in 2 files covered. (60.0%)

4 existing lines in 1 file now uncovered.

1938 of 2389 relevant lines covered (81.12%)

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
×
NEW
25
    CXNoteBook.display_canvasxpress_header()
×
26

27

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

34

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

41

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

52

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

60

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

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

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

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

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

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

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

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

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

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

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

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

110
        streamlit.plot(canvas)
1✔
111

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

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

© 2026 Coveralls, Inc