Construct a figure from the JSON contents of a local file or readable Python object. Note: A figure converted to JSON with one version of Plotly.py may not be compatible with another version. Parameters ---------- file: str or readable A string containing the path t
(file, output_type="Figure", skip_invalid=False, engine=None)
| 414 | |
| 415 | |
| 416 | def read_json(file, output_type="Figure", skip_invalid=False, engine=None): |
| 417 | """ |
| 418 | Construct a figure from the JSON contents of a local file or readable |
| 419 | Python object. |
| 420 | |
| 421 | Note: A figure converted to JSON with one version of Plotly.py may not be compatible with another version. |
| 422 | |
| 423 | Parameters |
| 424 | ---------- |
| 425 | file: str or readable |
| 426 | A string containing the path to a local file or a read-able Python |
| 427 | object (e.g. a pathlib.Path object or an open file descriptor) |
| 428 | |
| 429 | output_type: type or str (default 'Figure') |
| 430 | The output figure type or type name. |
| 431 | One of: graph_objs.Figure, 'Figure', graph_objs.FigureWidget, 'FigureWidget' |
| 432 | |
| 433 | skip_invalid: bool (default False) |
| 434 | False if invalid figure properties should result in an exception. |
| 435 | True if invalid figure properties should be silently ignored. |
| 436 | |
| 437 | engine: str (default None) |
| 438 | The JSON decoding engine to use. One of: |
| 439 | - if "json", parse JSON using built in json module |
| 440 | - if "orjson", parse using the faster orjson module, requires the orjson |
| 441 | package |
| 442 | - if "auto" use orjson module if available, otherwise use the json module |
| 443 | |
| 444 | If not specified, the default engine is set to the current value of |
| 445 | plotly.io.json.config.default_engine. |
| 446 | |
| 447 | Returns |
| 448 | ------- |
| 449 | Figure or FigureWidget |
| 450 | """ |
| 451 | |
| 452 | # Try to cast `file` as a pathlib object `path`. |
| 453 | if isinstance(file, str): |
| 454 | # Use the standard Path constructor to make a pathlib object. |
| 455 | path = Path(file) |
| 456 | elif isinstance(file, Path): |
| 457 | # `file` is already a Path object. |
| 458 | path = file |
| 459 | else: |
| 460 | # We could not make a Path object out of file. Either `file` is an open file |
| 461 | # descriptor with a `write()` method or it's an invalid object. |
| 462 | path = None |
| 463 | |
| 464 | # Read file contents into JSON string |
| 465 | # ----------------------------------- |
| 466 | if path is not None: |
| 467 | json_str = path.read_text() |
| 468 | else: |
| 469 | json_str = file.read() |
| 470 | |
| 471 | # Construct and return figure |
| 472 | # --------------------------- |
| 473 | return from_json( |