|
| 1 | +''' |
| 2 | +Collation of patches made to other python libraries, such as Dash itself. |
| 3 | +
|
| 4 | +If/when the patches are not needed they will be removed from this file. |
| 5 | +
|
| 6 | +
|
| 7 | +Copyright (c) 2022 Gibbs Consulting and others - see CONTRIBUTIONS.md |
| 8 | +
|
| 9 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +of this software and associated documentation files (the "Software"), to deal |
| 11 | +in the Software without restriction, including without limitation the rights |
| 12 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +copies of the Software, and to permit persons to whom the Software is |
| 14 | +furnished to do so, subject to the following conditions: |
| 15 | +
|
| 16 | +The above copyright notice and this permission notice shall be included in all |
| 17 | +copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | +SOFTWARE. |
| 26 | +
|
| 27 | +''' |
| 28 | + |
| 29 | +import json |
| 30 | + |
| 31 | + |
| 32 | +from plotly.io._json import config |
| 33 | +from plotly.utils import PlotlyJSONEncoder |
| 34 | + |
| 35 | +from _plotly_utils.optional_imports import get_module |
| 36 | +from django.utils.encoding import force_text |
| 37 | +from django.utils.functional import Promise |
| 38 | + |
| 39 | + |
| 40 | +class DjangoPlotlyJSONEncoder(PlotlyJSONEncoder): |
| 41 | + """Augment the PlotlyJSONEncoder class with Django delayed processing""" |
| 42 | + def default(self, obj): |
| 43 | + if isinstance(obj, Promise): |
| 44 | + return force_text(obj) |
| 45 | + return super().default(obj) |
| 46 | + |
| 47 | + |
| 48 | +def to_json_django_plotly(plotly_object, pretty=False, engine=None): |
| 49 | + """ |
| 50 | + Convert a plotly/Dash object to a JSON string representation |
| 51 | +
|
| 52 | + Parameters |
| 53 | + ---------- |
| 54 | + plotly_object: |
| 55 | + A plotly/Dash object represented as a dict, graph_object, or Dash component |
| 56 | +
|
| 57 | + pretty: bool (default False) |
| 58 | + True if JSON representation should be pretty-printed, False if |
| 59 | + representation should be as compact as possible. |
| 60 | +
|
| 61 | + engine: str (default None) |
| 62 | + The JSON encoding engine to use. One of: |
| 63 | + - "json" for an engine based on the built-in Python json module |
| 64 | + - "orjson" for a faster engine that requires the orjson package |
| 65 | + - "auto" for the "orjson" engine if available, otherwise "json" |
| 66 | + If not specified, the default engine is set to the current value of |
| 67 | + plotly.io.json.config.default_engine. |
| 68 | +
|
| 69 | + Returns |
| 70 | + ------- |
| 71 | + str |
| 72 | + Representation of input object as a JSON string |
| 73 | +
|
| 74 | + See Also |
| 75 | + -------- |
| 76 | + to_json : Convert a plotly Figure to JSON with validation |
| 77 | + """ |
| 78 | + orjson = get_module("orjson", should_load=True) |
| 79 | + |
| 80 | + # Determine json engine |
| 81 | + if engine is None: |
| 82 | + engine = config.default_engine |
| 83 | + |
| 84 | + if engine == "auto": |
| 85 | + if orjson is not None: |
| 86 | + engine = "orjson" |
| 87 | + else: |
| 88 | + engine = "json" |
| 89 | + elif engine not in ["orjson", "json"]: |
| 90 | + raise ValueError("Invalid json engine: %s" % engine) |
| 91 | + |
| 92 | + modules = { |
| 93 | + "sage_all": get_module("sage.all", should_load=False), |
| 94 | + "np": get_module("numpy", should_load=False), |
| 95 | + "pd": get_module("pandas", should_load=False), |
| 96 | + "image": get_module("PIL.Image", should_load=False), |
| 97 | + } |
| 98 | + |
| 99 | + # Dump to a JSON string and return |
| 100 | + # -------------------------------- |
| 101 | + if engine == "json": |
| 102 | + opts = {} |
| 103 | + if pretty: |
| 104 | + opts["indent"] = 2 |
| 105 | + else: |
| 106 | + # Remove all whitespace |
| 107 | + opts["separators"] = (",", ":") |
| 108 | + |
| 109 | + return json.dumps(plotly_object, cls=DjangoPlotlyJSONEncoder, **opts) |
| 110 | + elif engine == "orjson": |
| 111 | + JsonConfig.validate_orjson() |
| 112 | + opts = orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY |
| 113 | + |
| 114 | + if pretty: |
| 115 | + opts |= orjson.OPT_INDENT_2 |
| 116 | + |
| 117 | + # Plotly |
| 118 | + try: |
| 119 | + plotly_object = plotly_object.to_plotly_json() |
| 120 | + except AttributeError: |
| 121 | + pass |
| 122 | + |
| 123 | + # Try without cleaning |
| 124 | + try: |
| 125 | + return orjson.dumps(plotly_object, option=opts).decode("utf8") |
| 126 | + except TypeError: |
| 127 | + pass |
| 128 | + |
| 129 | + cleaned = clean_to_json_compatible( |
| 130 | + plotly_object, |
| 131 | + numpy_allowed=True, |
| 132 | + datetime_allowed=True, |
| 133 | + modules=modules, |
| 134 | + ) |
| 135 | + return orjson.dumps(cleaned, option=opts).decode("utf8") |
| 136 | + |
| 137 | + |
| 138 | +import plotly.io.json |
| 139 | +plotly.io.json.to_json_plotly = to_json_django_plotly |
0 commit comments