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

docinfosci / canvasxpress-python / 9767c6d3-1849-41a8-b40a-ef78011bd9ff

27 Jun 2024 09:55PM UTC coverage: 80.564% (-0.2%) from 80.791%
9767c6d3-1849-41a8-b40a-ef78011bd9ff

push

circleci

web-flow
Support convenience debugging f(x) (#140)

Support convenience reproducible JSON export and import functions.

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

2027 of 2516 relevant lines covered (80.56%)

0.81 hits per line

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

0.0
/canvasxpress/plot.py
NEW
1
from typing import Any, Union
×
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

NEW
17
_contexts_imported: list = []
×
18

19

NEW
20
def convert_from_reproducible_json(json: str) -> Union[None, CanvasXpress]:
×
21
    """
22
    Accepts a str with a reproducible JSON and returns a CanvasXpress object.
23
    """
NEW
24
    return CanvasXpress.from_reproducible_json(json)
×
25

26

NEW
27
def convert_to_reproducible_json(canvas: CanvasXpress) -> str:
×
28
    """
29
    Converts the CanvasXpress object into a reproducible JSON string.
30
    """
NEW
31
    return CXJSON.render_to_json(canvas)
×
32

33

NEW
34
def convert_to_image(canvas: CanvasXpress, type: str = "png") -> Union[None, bytes]:
×
35
    """
36
    Converts the CanvasXpress object to an image of the specified type.
37
    """
NEW
38
    converter = CXImage(canvas)
×
NEW
39
    candidates = converter.render(format=type)
×
NEW
40
    for conversion in candidates:
×
NEW
41
        if conversion.get("image", {}).get("format") == type:
×
NEW
42
            return conversion.get("image", {}).get("binary")
×
43

44

45
def show_in_browser(canvas: CanvasXpress) -> None:
×
46
    """
47
    Opens a browser and displays the canvas.
48
    """
49
    plotter = CXBrowserPopup(canvas)
×
50
    plotter.render()
×
51

52

53
def graph(canvas: CanvasXpress) -> Any:
×
54
    """
55
    Displays the CanvasXpress object as a visualized chart in a manner appropriate to the running context.
56

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

60
    - rstudio
61
    - shiny
62
    - jupyter
63
    - dash
64
    - streamlit
65
    - browser
66

67
    :returns: An `object` or `None` depending on the target context.  In the case of `browser` a popup browser will
68
        be launched.`
69
    """
70
    context = get_target_context()
×
71

72
    if context == CONTEXT_RSTUDIO:
×
73
        if not context in _contexts_imported:
×
74
            from canvasxpress.render.shiny import CXShinyWidget
×
75

76
        plotter = CXShinyWidget(canvas)
×
77
        plotter._repr_rstudio_viewer_()
×
78

79
    elif context == CONTEXT_SHINY:
×
80
        if not context in _contexts_imported:
×
81
            from canvasxpress.render.shiny import CXShinyWidget
×
82

83
        plotter = CXShinyWidget(canvas)
×
84
        return plotter
×
85

86
    elif context == CONTEXT_DASH:
×
87
        if not context in _contexts_imported:
×
88
            from canvasxpress.render.dash import CXElementFactory
×
89

90
        plotter = CXElementFactory()
×
91
        return plotter.render(canvas)
×
92

93
    elif context == CONTEXT_JUPYTER:
×
94
        if not context in _contexts_imported:
×
95
            from canvasxpress.render.jupyter import CXNoteBook
×
96

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

100
    elif context == CONTEXT_STREAMLIT:
×
101
        if not context in _contexts_imported:
×
102
            from canvasxpress.render import streamlit
×
103

104
        return streamlit.plot(canvas)
×
105

106
    elif context == CONTEXT_BROWSER:
×
107
        plotter = CXBrowserPopup(canvas)
×
108
        plotter.render()
×
109

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