Convert a figure to a JSON string representation Parameters ---------- fig: Figure object or dict representing a figure validate: bool (default True) True if the figure should be validated before being converted to JSON, False otherwise. pretty: bo
(fig, validate=True, pretty=False, remove_uids=True, engine=None)
| 173 | |
| 174 | |
| 175 | def to_json(fig, validate=True, pretty=False, remove_uids=True, engine=None): |
| 176 | """ |
| 177 | Convert a figure to a JSON string representation |
| 178 | |
| 179 | Parameters |
| 180 | ---------- |
| 181 | fig: |
| 182 | Figure object or dict representing a figure |
| 183 | |
| 184 | validate: bool (default True) |
| 185 | True if the figure should be validated before being converted to |
| 186 | JSON, False otherwise. |
| 187 | |
| 188 | pretty: bool (default False) |
| 189 | True if JSON representation should be pretty-printed, False if |
| 190 | representation should be as compact as possible. |
| 191 | |
| 192 | remove_uids: bool (default True) |
| 193 | True if trace UIDs should be omitted from the JSON representation |
| 194 | |
| 195 | engine: str (default None) |
| 196 | The JSON encoding engine to use. One of: |
| 197 | - "json" for an engine based on the built-in Python json module |
| 198 | - "orjson" for a faster engine that requires the orjson package |
| 199 | - "auto" for the "orjson" engine if available, otherwise "json" |
| 200 | If not specified, the default engine is set to the current value of |
| 201 | plotly.io.json.config.default_engine. |
| 202 | |
| 203 | Returns |
| 204 | ------- |
| 205 | str |
| 206 | Representation of figure as a JSON string |
| 207 | |
| 208 | See Also |
| 209 | -------- |
| 210 | to_json_plotly : Convert an arbitrary plotly graph_object or Dash component to JSON |
| 211 | """ |
| 212 | # Validate figure |
| 213 | # --------------- |
| 214 | fig_dict = validate_coerce_fig_to_dict(fig, validate) |
| 215 | |
| 216 | # Remove trace uid |
| 217 | # ---------------- |
| 218 | if remove_uids: |
| 219 | for trace in fig_dict.get("data", []): |
| 220 | trace.pop("uid", None) |
| 221 | |
| 222 | return to_json_plotly(fig_dict, pretty=pretty, engine=engine) |
| 223 | |
| 224 | |
| 225 | def write_json(fig, file, validate=True, pretty=False, remove_uids=True, engine=None): |
no test coverage detected