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

Skip to content

Commit 564aa51

Browse files
committed
clean up deprecation messages, fix bug with passing mathjax
1 parent aca1620 commit 564aa51

File tree

1 file changed

+54
-28
lines changed

1 file changed

+54
-28
lines changed

plotly/io/_kaleido.py

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,24 @@
1414

1515
PLOTLY_GET_CHROME_ERROR_MSG = """
1616
17-
Kaleido requires Google Chrome to be installed. Install it by running:
17+
Kaleido requires Google Chrome to be installed.
18+
19+
Either download and install Chrome yourself following Google's instructions for your operating system,
20+
or install it from your terminal by running:
21+
1822
$ plotly_get_chrome
23+
1924
"""
2025

2126

2227
# TODO: Remove --pre flag once Kaleido v1 full release is available
2328
KALEIDO_DEPRECATION_MSG = f"""
2429
Support for Kaleido versions less than 1.0.0 is deprecated and will be removed after {ENGINE_SUPPORT_TIMELINE}.
25-
Please upgrade Kaleido to version 1.0.0 or greater (`pip install --upgrade --pre kaleido` or `pip install plotly[kaleido]`).
30+
Please upgrade Kaleido to version 1.0.0 or greater (`pip install 'kaleido>=1.0.0'` or `pip install 'plotly[kaleido]'`).
2631
"""
2732
ORCA_DEPRECATION_MSG = f"""
2833
Support for the Orca engine is deprecated and will be removed after {ENGINE_SUPPORT_TIMELINE}.
29-
Please install Kaleido (`pip install --upgrade --pre kaleido` or `pip install plotly[kaleido]`) to use the Kaleido engine.
34+
Please install Kaleido (`pip install 'kaleido>=1.0.0'` or `pip install 'plotly[kaleido]'`) to use the Kaleido engine.
3035
"""
3136
ENGINE_PARAM_DEPRECATION_MSG = f"""
3237
Support for the 'engine' argument is deprecated and will be removed after {ENGINE_SUPPORT_TIMELINE}.
@@ -110,6 +115,8 @@ def __getattr__(self, name):
110115
return super().__getattr__(name)
111116

112117
# Ensure the new method of setting defaults is backwards compatible with Kaleido v0
118+
# DefaultsBackwardsCompatible sets the attributes on `scope` object at the same time
119+
# as they are set on the `defaults` object
113120
class DefaultsBackwardsCompatible(defaults.__class__):
114121
def __init__(self, scope):
115122
self._scope = scope
@@ -267,7 +274,7 @@ def to_image(
267274
less than 1.0 will decrease the image resolution.
268275
269276
If not specified, will default to:
270-
- `plotly.io.defaults.default_scale` if engine is "kaliedo"
277+
- `plotly.io.defaults.default_scale` if engine is "kaleido"
271278
- `plotly.io.orca.config.default_scale` if engine is "orca" (deprecated)
272279
273280
validate: bool
@@ -329,9 +336,10 @@ def to_image(
329336
if not kaleido_available():
330337
raise ValueError(
331338
"""
332-
Image export using the "kaleido" engine requires the kaleido package,
339+
Image export using the "kaleido" engine requires the Kaleido package,
333340
which can be installed using pip:
334-
$ pip install -U kaleido
341+
342+
$ pip install --upgrade kaleido
335343
"""
336344
)
337345

@@ -348,9 +356,11 @@ def to_image(
348356
EPS export is not supported by Kaleido v1. Please use SVG or PDF instead.
349357
You can also downgrade to Kaleido v0, but support for Kaleido v0 will be removed after {ENGINE_SUPPORT_TIMELINE}.
350358
To downgrade to Kaleido v0, run:
351-
$ pip install kaleido<1.0.0
359+
$ pip install 'kaleido<1.0.0'
352360
"""
353361
)
362+
from kaleido.errors import ChromeNotFoundError
363+
354364
try:
355365
# TODO: Refactor to make it possible to use a shared Kaleido instance here
356366
img_bytes = kaleido.calc_fig_sync(
@@ -362,9 +372,13 @@ def to_image(
362372
scale=scale or defaults.default_scale,
363373
),
364374
topojson=defaults.topojson,
365-
# mathjax=Path(defaults.mathjax).as_uri() if defaults.mathjax else None,
375+
kopts=dict(
376+
mathjax=defaults.mathjax,
377+
)
378+
if defaults.mathjax
379+
else None,
366380
)
367-
except kaleido.errors.ChromeNotFoundError:
381+
except ChromeNotFoundError:
368382
raise RuntimeError(PLOTLY_GET_CHROME_ERROR_MSG)
369383

370384
else:
@@ -583,18 +597,20 @@ def write_images(
583597
if not kaleido_available():
584598
raise ValueError(
585599
"""
586-
The `write_images()` function requires the kaleido package,
600+
The `write_images()` function requires the Kaleido package,
587601
which can be installed using pip:
588-
$ pip install -U kaleido
602+
603+
$ pip install --upgrade kaleido
589604
"""
590605
)
591606
elif kaleido_major() < 1:
592607
raise ValueError(
593608
f"""
594609
You have Kaleido version {Version(importlib_metadata.version("kaleido"))} installed.
595-
The `write_images()` function requires the kaleido package version 1 or greater,
610+
The `write_images()` function requires the Kaleido package version 1.0.0 or greater,
596611
which can be installed using pip:
597-
$ pip install -U 'kaleido>=1.0.0'
612+
613+
$ pip install 'kaleido>=1.0.0'
598614
"""
599615
)
600616

@@ -621,24 +637,32 @@ def write_images(
621637
# has already been cast to a Path object.
622638
# Also insert defaults for any missing arguments as needed
623639
kaleido_specs = [
624-
{
625-
"fig": d["fig"],
626-
"path": d["file"],
627-
"opts": dict(
640+
dict(
641+
fig=d["fig"],
642+
path=d["file"],
643+
opts=dict(
628644
format=infer_format(d["file"], d["format"]) or defaults.default_format,
629645
width=d["width"] or defaults.default_width,
630646
height=d["height"] or defaults.default_height,
631647
scale=d["scale"] or defaults.default_scale,
632648
),
633-
"topojson": defaults.topojson,
634-
# "mathjax": Path(defaults.mathjax).as_uri() if defaults.mathjax else None,
635-
}
649+
topojson=defaults.topojson,
650+
)
636651
for d in arg_dicts
637652
]
638653

654+
from kaleido.errors import ChromeNotFoundError
655+
639656
try:
640-
kaleido.write_fig_from_object_sync(kaleido_specs)
641-
except kaleido.errors.ChromeNotFoundError:
657+
kaleido.write_fig_from_object_sync(
658+
kaleido_specs,
659+
kopts=dict(
660+
mathjax=defaults.mathjax,
661+
)
662+
if defaults.mathjax
663+
else None,
664+
)
665+
except ChromeNotFoundError:
642666
raise RuntimeError(PLOTLY_GET_CHROME_ERROR_MSG)
643667

644668

@@ -676,9 +700,10 @@ def full_figure_for_development(
676700
if not kaleido_available():
677701
raise ValueError(
678702
"""
679-
Full figure generation requires the kaleido package,
703+
Full figure generation requires the Kaleido package,
680704
which can be installed using pip:
681-
$ pip install -U kaleido
705+
706+
$ pip install --upgrade kaleido
682707
"""
683708
)
684709

@@ -700,7 +725,7 @@ def full_figure_for_development(
700725
# Kaleido v0
701726
warnings.warn(
702727
f"Support for Kaleido versions less than 1.0.0 is deprecated and will be removed after {ENGINE_SUPPORT_TIMELINE}. "
703-
+ "Please upgrade Kaleido to version 1.0.0 or greater (`pip install --upgrade kaleido`).",
728+
+ "Please upgrade Kaleido to version 1.0.0 or greater (`pip install 'kaleido>=1.0.0'`).",
704729
DeprecationWarning,
705730
)
706731
fig = json.loads(scope.transform(fig, format="json").decode("utf-8"))
@@ -720,9 +745,10 @@ def get_chrome() -> None:
720745
defined in pyproject.toml
721746
"""
722747
if not kaleido_available() or kaleido_major() < 1:
723-
raise ValueError(
724-
"This command requires Kaleido v1.0.0 or greater. Install it using `pip install kaleido`."
725-
)
748+
raise ValueError("""
749+
This command requires Kaleido v1.0.0 or greater.
750+
Install it using `pip install 'kaleido>=1.0.0'` or `pip install 'plotly[kaleido]'`."
751+
""")
726752
import sys
727753

728754
cli_yes = len(sys.argv) > 1 and sys.argv[1] == "-y"

0 commit comments

Comments
 (0)