Convert a plotly/Dash object to a JSON string representation Parameters ---------- plotly_object: A plotly/Dash object represented as a dict, graph_object, or Dash component pretty: bool (default False) True if JSON representation should be pretty-printed, Fals
(plotly_object, pretty=False, engine=None)
| 77 | |
| 78 | |
| 79 | def to_json_plotly(plotly_object, pretty=False, engine=None): |
| 80 | """ |
| 81 | Convert a plotly/Dash object to a JSON string representation |
| 82 | |
| 83 | Parameters |
| 84 | ---------- |
| 85 | plotly_object: |
| 86 | A plotly/Dash object represented as a dict, graph_object, or Dash component |
| 87 | |
| 88 | pretty: bool (default False) |
| 89 | True if JSON representation should be pretty-printed, False if |
| 90 | representation should be as compact as possible. |
| 91 | |
| 92 | engine: str (default None) |
| 93 | The JSON encoding engine to use. One of: |
| 94 | - "json" for an engine based on the built-in Python json module |
| 95 | - "orjson" for a faster engine that requires the orjson package |
| 96 | - "auto" for the "orjson" engine if available, otherwise "json" |
| 97 | If not specified, the default engine is set to the current value of |
| 98 | plotly.io.json.config.default_engine. |
| 99 | |
| 100 | Returns |
| 101 | ------- |
| 102 | str |
| 103 | Representation of input object as a JSON string |
| 104 | |
| 105 | See Also |
| 106 | -------- |
| 107 | to_json : Convert a plotly Figure to JSON with validation |
| 108 | """ |
| 109 | orjson = get_module("orjson", should_load=True) |
| 110 | |
| 111 | # Determine json engine |
| 112 | if engine is None: |
| 113 | engine = config.default_engine |
| 114 | |
| 115 | if engine == "auto": |
| 116 | if orjson is not None: |
| 117 | engine = "orjson" |
| 118 | else: |
| 119 | engine = "json" |
| 120 | elif engine not in ["orjson", "json"]: |
| 121 | raise ValueError("Invalid json engine: %s" % engine) |
| 122 | |
| 123 | modules = { |
| 124 | "sage_all": get_module("sage.all", should_load=False), |
| 125 | "np": get_module("numpy", should_load=False), |
| 126 | "pd": get_module("pandas", should_load=False), |
| 127 | "image": get_module("PIL.Image", should_load=False), |
| 128 | } |
| 129 | |
| 130 | # Dump to a JSON string and return |
| 131 | # -------------------------------- |
| 132 | if engine == "json": |
| 133 | opts = {} |
| 134 | if pretty: |
| 135 | opts["indent"] = 2 |
| 136 | else: |
no test coverage detected