-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Support Kaleido v1 in Plotly.py #5062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
f0a78a6
a681c84
a7a6f24
2e9c8af
60bb748
b87f752
e203623
8054331
1a195a7
577d3ca
89209ad
96bf9a0
350dd48
08c1d4e
8b47a0a
249cc9f
ad9dbd9
e75a5df
a2b4f3c
2549299
ab3b700
de473e1
71696fe
100b955
5541a79
95a05db
0a73d0e
4d3dd56
d871e74
b3e8d36
ef5f520
611e2e4
c92a1ee
b56d5ec
c01cb8a
bcd40f3
54985b8
b4af0d5
0f61cc3
fe12aec
692842f
aac9c20
ad57031
b9e5f7a
720ada5
53d486a
c990736
96bb17a
f736f4c
aca1620
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Default settings for image generation | ||
|
||
|
||
class _Defaults(object): | ||
""" | ||
Class to store default settings for image generation. | ||
""" | ||
def __init__(self): | ||
self.default_format = "png" | ||
self.default_width = 700 | ||
self.default_height = 500 | ||
self.default_scale = 1 | ||
|
||
defaults = _Defaults() | ||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,32 @@ | |
|
||
import plotly | ||
from plotly.io._utils import validate_coerce_fig_to_dict, as_individual_args | ||
from plotly.io import defaults | ||
|
||
ENGINE_SUPPORT_TIMELINE = "September 2025" | ||
|
||
kaleido_scope_default_getwarning = ( | ||
lambda x: f""" | ||
Accessing plotly.io.kaleido.scope.{x} is deprecated and will be removed after {ENGINE_SUPPORT_TIMELINE}. | ||
Please use plotly.io.defaults.{x} instead. | ||
""" | ||
) | ||
|
||
kaleido_scope_default_setwarning = ( | ||
lambda x: f""" | ||
Setting plotly.io.kaleido.scope.{x} is deprecated and will be removed after {ENGINE_SUPPORT_TIMELINE}. " | ||
emilykl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Please set plotly.io.defaults.{x} instead. | ||
""" | ||
) | ||
|
||
bad_attribute_error = ( | ||
lambda x: f""" | ||
Attribute plotly.io.defaults.{x} is not valid. | ||
Also, plotly.io.kaleido.scope.* is deprecated and will be removed after {ENGINE_SUPPORT_TIMELINE}. Please use plotly.io.defaults.* instead. | ||
""" | ||
) | ||
|
||
|
||
try: | ||
import kaleido | ||
|
||
|
@@ -20,7 +43,28 @@ | |
# Kaleido v0 | ||
from kaleido.scopes.plotly import PlotlyScope | ||
|
||
scope = PlotlyScope() | ||
# Show a deprecation warning if the old method of setting defaults is used | ||
class PlotlyScopeWithDeprecationWarnings(PlotlyScope): | ||
def __setattr__(self, name, value): | ||
if name in defaults.__dict__: | ||
warnings.warn( | ||
kaleido_scope_default_setwarning(name), | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) | ||
setattr(defaults, name, value) | ||
super(PlotlyScopeWithDeprecationWarnings, self).__setattr__(name, value) | ||
|
||
def __getattr__(self, name): | ||
if name in defaults.__dict__: | ||
warnings.warn( | ||
kaleido_scope_default_getwarning(name), | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) | ||
return super(PlotlyScopeWithDeprecationWarnings, self).__getattr__(name) | ||
|
||
scope = PlotlyScopeWithDeprecationWarnings() | ||
# Compute absolute path to the 'plotly/package_data/' directory | ||
root_dir = os.path.dirname(os.path.abspath(plotly.__file__)) | ||
package_dir = os.path.join(root_dir, "package_data") | ||
|
@@ -29,6 +73,34 @@ | |
scope.mathjax = ( | ||
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" | ||
) | ||
else: | ||
# Kaleido v1 | ||
|
||
# Show a deprecation warning if the old method of setting defaults is used | ||
class DefaultsDeprecationWarning: | ||
def __getattr__(self, name): | ||
if name in defaults.__dict__: | ||
warnings.warn( | ||
kaleido_scope_default_getwarning(name), | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) | ||
return getattr(defaults, name) | ||
else: | ||
raise AttributeError(bad_attribute_error(name)) | ||
|
||
def __setattr__(self, name, value): | ||
if name in defaults.__dict__: | ||
warnings.warn( | ||
kaleido_scope_default_setwarning(name), | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) | ||
setattr(defaults, name, value) | ||
else: | ||
raise AttributeError(bad_attribute_error(name)) | ||
|
||
scope = DefaultsDeprecationWarning() | ||
|
||
except ImportError as e: | ||
kaleido_available = False | ||
|
@@ -64,29 +136,37 @@ def to_image( | |
- 'pdf' | ||
- 'eps' (Requires the poppler library to be installed and on the PATH) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we still handle EPS? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gvwilson Following up on this — Kaleido v1 does not support EPS yet. So either we drop support for EPS entirely, or document that EPS is only available with Kaleido v0 and add an informative error message. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please do the latter - thanks |
||
|
||
If not specified, will default to `plotly.io.kaleido.scope.default_format` | ||
If not specified, will default to: | ||
- `plotly.io.defaults.default_format` if engine is "kaleido" | ||
- `plotly.io.orca.config.default_format` if engine is "orca" (deprecated) | ||
|
||
width: int or None | ||
The width of the exported image in layout pixels. If the `scale` | ||
property is 1.0, this will also be the width of the exported image | ||
in physical pixels. | ||
|
||
If not specified, will default to `plotly.io.kaleido.scope.default_width` | ||
If not specified, will default to: | ||
- `plotly.io.defaults.default_width` if engine is "kaleido" | ||
- `plotly.io.orca.config.default_width` if engine is "orca" (deprecated) | ||
|
||
height: int or None | ||
The height of the exported image in layout pixels. If the `scale` | ||
property is 1.0, this will also be the height of the exported image | ||
in physical pixels. | ||
|
||
If not specified, will default to `plotly.io.kaleido.scope.default_height` | ||
If not specified, will default to: | ||
- `plotly.io.defaults.default_height` if engine is "kaleido" | ||
- `plotly.io.orca.config.default_height` if engine is "orca" (deprecated) | ||
|
||
scale: int or float or None | ||
The scale factor to use when exporting the figure. A scale factor | ||
larger than 1.0 will increase the image resolution with respect | ||
to the figure's layout pixel dimensions. Whereas as scale factor of | ||
less than 1.0 will decrease the image resolution. | ||
|
||
If not specified, will default to `plotly.io.kaleido.scope.default_scale` | ||
If not specified, will default to: | ||
- `plotly.io.defaults.default_scale` if engine is "kaliedo" | ||
- `plotly.io.orca.config.default_scale` if engine is "orca" (deprecated) | ||
|
||
validate: bool | ||
True if the figure should be validated before being converted to | ||
|
@@ -174,8 +254,8 @@ def to_image( | |
if format == "eps": | ||
raise ValueError( | ||
f""" | ||
EPS export is not supported with Kaleido v1. Please use SVG or PDF instead. | ||
You can also downgrade to Kaleido v0, but support for v0 will be removed after {ENGINE_SUPPORT_TIMELINE}. | ||
EPS export is not supported by Kaleido v1. Please use SVG or PDF instead. | ||
You can also downgrade to Kaleido v0, but support for Kaleido v0 will be removed after {ENGINE_SUPPORT_TIMELINE}. | ||
To downgrade to Kaleido v0, run: | ||
$ pip install kaleido<1.0.0 | ||
""" | ||
|
@@ -187,10 +267,10 @@ def to_image( | |
img_bytes = kaleido.calc_fig_sync( | ||
fig_dict, | ||
opts=dict( | ||
format=format, | ||
width=width, | ||
height=height, | ||
scale=scale, | ||
format=format or defaults.default_format, | ||
width=width or defaults.default_width, | ||
height=height or defaults.default_height, | ||
scale=scale or defaults.default_scale, | ||
), | ||
) | ||
except choreographer.errors.ChromeNotFoundError: | ||
|
@@ -252,30 +332,36 @@ def write_image( | |
If not specified and `file` is a string then this will default to the | ||
file extension. If not specified and `file` is not a string then this | ||
will default to: | ||
- `plotly.io.kaleido.scope.default_format` if engine is "kaleido" | ||
- `plotly.io.orca.config.default_format` if engine is "orca" | ||
- `plotly.io.defaults.default_format` if engine is "kaleido" | ||
- `plotly.io.orca.config.default_format` if engine is "orca" (deprecated) | ||
|
||
width: int or None | ||
The width of the exported image in layout pixels. If the `scale` | ||
property is 1.0, this will also be the width of the exported image | ||
in physical pixels. | ||
|
||
If not specified, will default to`plotly.io.kaleido.scope.default_width` | ||
If not specified, will default to: | ||
- `plotly.io.defaults.default_width` if engine is "kaleido" | ||
- `plotly.io.orca.config.default_width` if engine is "orca" (deprecated) | ||
|
||
height: int or None | ||
The height of the exported image in layout pixels. If the `scale` | ||
property is 1.0, this will also be the height of the exported image | ||
in physical pixels. | ||
|
||
If not specified, will default to `plotly.io.kaleido.scope.default_height` | ||
If not specified, will default to: | ||
- `plotly.io.defaults.default_height` if engine is "kaleido" | ||
- `plotly.io.orca.config.default_height` if engine is "orca" (deprecated) | ||
|
||
scale: int or float or None | ||
The scale factor to use when exporting the figure. A scale factor | ||
larger than 1.0 will increase the image resolution with respect | ||
to the figure's layout pixel dimensions. Whereas as scale factor of | ||
less than 1.0 will decrease the image resolution. | ||
|
||
If not specified, will default to `plotly.io.kaleido.scope.default_scale` | ||
If not specified, will default to: | ||
- `plotly.io.defaults.default_scale` if engine is "kaleido" | ||
- `plotly.io.orca.config.default_scale` if engine is "orca" (deprecated) | ||
|
||
validate: bool | ||
True if the figure should be validated before being converted to | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from ._kaleido import write_image, to_image | ||
from ._kaleido import to_image, write_image, scope |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there percy tests that use kaleido to make sure that the actual images generated are correct? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will there be a way to set
mathjax
,topojson
, andmapbox_access_token
, the other export settings available in v0?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mapbox_access_token
isn't relevant anymore since Mapbox traces are no longer supported; the other two I guess we should expose. I'll add a TODO.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
topojson
has been added but I have not tested it yet;mathjax
is added indefaults
but not functional quite yet