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

docinfosci / canvasxpress-python / 3905f6b2-c80b-4463-8dcf-f5bc35f911c7

27 Jun 2024 04:35PM UTC coverage: 80.469% (-0.3%) from 80.791%
3905f6b2-c80b-4463-8dcf-f5bc35f911c7

push

circleci

docinfosci
Support convenience conversion f(x)

0 of 10 new or added lines in 1 file covered. (0.0%)

2023 of 2514 relevant lines covered (80.47%)

0.8 hits per line

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

0.0
/canvasxpress/plot.py
1
from typing import Any
×
2

3
from canvasxpress.canvas import CanvasXpress
×
4
from canvasxpress.render.environment import (
×
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.popup import CXBrowserPopup
×
NEW
14
from canvasxpress.render.image import CXImage
×
NEW
15
from canvasxpress.render.json import CXJSON
×
16

17

NEW
18
def convert_to_reproducible_json(canvas: CanvasXpress) -> str:
×
19
    """
20
    Converts the CanvasXpress object into a reproducible JSON string.
21
    """
NEW
22
    return CXJSON.render_to_json(canvas)
×
23

24

NEW
25
def convert_to_image(canvas: CanvasXpress, type: str = "png") -> Union[None, bytes]:
×
26
    """
27
    Converts the CanvasXpress object to an image of the specified type.
28
    """
NEW
29
    converter = CXImage(canvas)
×
NEW
30
    candidates = converter.render(format=type)
×
NEW
31
    for conversion in candidates:
×
NEW
32
        if conversion.get("image", {}).get("format") == type:
×
NEW
33
            return conversion.get("image", {}).get("binary")
×
34

35

36
def show_in_browser(canvas: CanvasXpress) -> None:
×
37
    """
38
    Opens a browser and displays the canvas.
39
    """
40
    plotter = CXBrowserPopup(canvas)
×
41
    plotter.render()
×
42

43

44
_contexts_imported: list = []
×
45

46

47
def graph(canvas: CanvasXpress) -> Any:
×
48
    """
49
    Displays the CanvasXpress object as a visualized chart in a manner appropriate to the running context.
50

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

54
    - rstudio
55
    - shiny
56
    - jupyter
57
    - dash
58
    - streamlit
59
    - browser
60

61
    :returns: An `object` or `None` depending on the target context.  In the case of `browser` a popup browser will
62
        be launched.`
63
    """
64
    context = get_target_context()
×
65

66
    if context == CONTEXT_RSTUDIO:
×
67
        if not context in _contexts_imported:
×
68
            from canvasxpress.render.shiny import CXShinyWidget
×
69

70
        plotter = CXShinyWidget(canvas)
×
71
        plotter._repr_rstudio_viewer_()
×
72

73
    elif context == CONTEXT_SHINY:
×
74
        if not context in _contexts_imported:
×
75
            from canvasxpress.render.shiny import CXShinyWidget
×
76

77
        plotter = CXShinyWidget(canvas)
×
78
        return plotter
×
79

80
    elif context == CONTEXT_DASH:
×
81
        if not context in _contexts_imported:
×
82
            from canvasxpress.render.dash import CXElementFactory
×
83

84
        plotter = CXElementFactory()
×
85
        return plotter.render(canvas)
×
86

87
    elif context == CONTEXT_JUPYTER:
×
88
        if not context in _contexts_imported:
×
89
            from canvasxpress.render.jupyter import CXNoteBook
×
90

91
        plotter = CXNoteBook(canvas)
×
92
        return plotter.render()
×
93

94
    elif context == CONTEXT_STREAMLIT:
×
95
        if not context in _contexts_imported:
×
96
            from canvasxpress.render import streamlit
×
97

98
        return streamlit.plot(canvas)
×
99

100
    elif context == CONTEXT_BROWSER:
×
101
        plotter = CXBrowserPopup(canvas)
×
102
        plotter.render()
×
103

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