Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 732be17

Browse files
full_figure_for_development
1 parent 9fab582 commit 732be17

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

packages/python/plotly/plotly/basedatatypes.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ def _add_annotation_like(
11041104
if refs[0].subplot_type != "xy":
11051105
raise ValueError(
11061106
"""
1107-
Cannot add {prop_singular} to subplot at position ({r}, {c}) because subplot
1107+
Cannot add {prop_singular} to subplot at position ({r}, {c}) because subplot
11081108
is of type {subplot_type}.""".format(
11091109
prop_singular=prop_singular,
11101110
r=row,
@@ -2896,6 +2896,35 @@ def to_json(self, *args, **kwargs):
28962896

28972897
return pio.to_json(self, *args, **kwargs)
28982898

2899+
def full_figure_for_development(self, warn=True, as_dict=False):
2900+
"""
2901+
Compute default values for all attributes not specified in the input figure and
2902+
returns the output as a "full" figure. This function calls Plotly.js via Kaleido
2903+
to populate unspecified attributes. This function is intended for interactive use
2904+
during development to learn more about how Plotly.js computes default values and is
2905+
not generally necessary or recommended for production use.
2906+
2907+
Parameters
2908+
----------
2909+
fig:
2910+
Figure object or dict representing a figure
2911+
2912+
warn: bool
2913+
If False, suppress warnings about not using this in production.
2914+
2915+
as_dict: bool
2916+
If True, output is a dict with some keys that go.Figure can't parse.
2917+
If False, output is a go.Figure with unparseable keys skipped.
2918+
2919+
Returns
2920+
-------
2921+
plotly.graph_objects.Figure or dict
2922+
The full figure
2923+
"""
2924+
import plotly.io as pio
2925+
2926+
return pio.full_figure_for_development(self, warn, as_dict)
2927+
28992928
def write_json(self, *args, **kwargs):
29002929
"""
29012930
Convert a figure to JSON and write it to a file or writeable

packages/python/plotly/plotly/io/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33

44
if sys.version_info < (3, 7):
5-
from ._kaleido import to_image, write_image
5+
from ._kaleido import to_image, write_image, full_figure_for_development
66
from . import orca, kaleido
77
from ._json import to_json, from_json, read_json, write_json
88
from ._templates import templates, to_templated
@@ -25,6 +25,7 @@
2525
"renderers",
2626
"show",
2727
"base_renderers",
28+
"full_figure_for_development",
2829
]
2930
else:
3031
__all__, __getattr__, __dir__ = relative_import(
@@ -33,6 +34,7 @@
3334
[
3435
"._kaleido.to_image",
3536
"._kaleido.write_image",
37+
"._kaleido.full_figure_for_development",
3638
"._json.to_json",
3739
"._json.from_json",
3840
"._json.read_json",

packages/python/plotly/plotly/io/_kaleido.py

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import
22
from six import string_types
33
import os
4+
import json
45
import plotly
56
from plotly.io._utils import validate_coerce_fig_to_dict
67

@@ -120,7 +121,7 @@ def to_image(
120121
"""
121122
Image export using the "kaleido" engine requires the kaleido package,
122123
which can be installed using pip:
123-
$ pip install -U kaleido
124+
$ pip install -U kaleido
124125
"""
125126
)
126127

@@ -260,4 +261,58 @@ def write_image(
260261
file.write(img_data)
261262

262263

263-
__all__ = ["to_image", "write_image", "scope"]
264+
def full_figure_for_development(fig, warn=True, as_dict=False):
265+
"""
266+
Compute default values for all attributes not specified in the input figure and
267+
returns the output as a "full" figure. This function calls Plotly.js via Kaleido
268+
to populate unspecified attributes. This function is intended for interactive use
269+
during development to learn more about how Plotly.js computes default values and is
270+
not generally necessary or recommended for production use.
271+
272+
Parameters
273+
----------
274+
fig:
275+
Figure object or dict representing a figure
276+
277+
warn: bool
278+
If False, suppress warnings about not using this in production.
279+
280+
as_dict: bool
281+
If True, output is a dict with some keys that go.Figure can't parse.
282+
If False, output is a go.Figure with unparseable keys skipped.
283+
284+
Returns
285+
-------
286+
plotly.graph_objects.Figure or dict
287+
The full figure
288+
"""
289+
290+
# Raise informative error message if Kaleido is not installed
291+
if scope is None:
292+
raise ValueError(
293+
"""
294+
Image export using the "kaleido" engine requires the kaleido package,
295+
which can be installed using pip:
296+
$ pip install -U kaleido
297+
"""
298+
)
299+
300+
if warn:
301+
import warnings
302+
303+
warnings.warn(
304+
"full_figure_for_development is not recommended or necessary for "
305+
"production use in most circumstances. \n"
306+
"To suppress this warning, set warn=False"
307+
)
308+
309+
fig = json.loads(scope.transform(fig, format="json"))
310+
if as_dict:
311+
return fig
312+
else:
313+
import plotly.graph_objects as go
314+
315+
return go.Figure(fig, skip_invalid=True)
316+
317+
318+
__all__ = ["to_image", "write_image", "scope", "full_figure_for_development"]

0 commit comments

Comments
 (0)