Skip to content
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

Support Kaleido v1 in Plotly.py #5062

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft

Support Kaleido v1 in Plotly.py #5062

wants to merge 11 commits into from

Conversation

emilykl
Copy link
Contributor

@emilykl emilykl commented Mar 4, 2025

This initial draft is an attempt at supporting both the Kaleido v0 and v1 APIs in Plotly.py.

The changes so far are contained to plotly/io/_kaleido.py, plotly/io/kaleido.py, and tests/test_optional/test_kaleido/test_kaleido.py.

fig.write_image(), fig.to_image(), pio.write_image(), and pio.to_image() are basically working with both Kaleido versions as far as I can tell (with the exception of writing EPS files, which is not yet supported by Kaleido v1).

These changes also remove support for orca.

There are some remaining test failures to investigate.


TODO

  • Update CI to run all tests with Kaleido v1
  • Add CI job to run Kaleido tests with Kaleido v0
  • Finalize UX for what happens if Chrome is not found
  • Finalize UX / API for EPS
  • Figure out what to do about 'renderers' Kaleido test
  • Add deprecation warning for engine argument to write_image / to_image
  • Fully remove all orca-related code from repo

Copy link
Contributor

@gvwilson gvwilson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments and a question, happy to read through again as soon as you want.

- 'webp'
- 'svg'
- 'pdf'
- 'eps' (Requires the poppler library to be installed and on the PATH)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we still handle EPS?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do the latter - thanks

- "kaleido": Use Kaleido for image export
- "orca": Use Orca for image export
- "auto" (default): Use Kaleido if installed, otherwise use orca
engine (deprecated): str
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we print a deprecation warning if this argument is used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet, assuming we are in agreement about removing this argument, I'll add one

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm in agreement that we should remove this argument

def check_image(path_or_buffer, size=(700, 500), format="PNG"):
if format == "PDF":
img = PdfReader(path_or_buffer)
# TODO: There is a conversion factor needed here
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of the TODO is for me to educate myself on how Plotly currently determines PDF size when writing to PDF. :)

But the size argument is measured in pixels (at least for raster file types) and PDFs have no concept of pixels so I wouldn't expect the PDF to report the same "size" as passed in the argument necessarily.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will also take a looksy on that issue, lets say for this and the above tomorrow. I am flying today.

@emilykl
Copy link
Contributor Author

emilykl commented Mar 4, 2025

cc @ayjayt

@marthacryan
Copy link
Collaborator

Do we need to maintain backwards compatibility with older Kaleido versions here?

Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

If not specified, will default to:
- `plotly.io.kaleido.scope.default_format` if engine is "kaleido"
- `plotly.io.orca.config.default_format` if engine is "orca"
If not specified, will default to `plotly.io.kaleido.scope.default_format`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these references to plotly.io.kaleido.scope.. need to be something else if that's no longer imported here
https://github.com/plotly/plotly.py/pull/5062/files#diff-40813ac13aafeaa7135da57a86bec52cae638135b0acf9324677c9c4eee2ba2bR1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, those docstrings should be changed -- I need to research where those defaults are defined in Kaleido v1. (@ayjayt do you know off the top of your head?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/plotly/Kaleido/blob/d493b9dc0c04940721dc2d67e8b342d31fc6bb18/src/py/kaleido/_fig_tools.py#L9-L13

The order is png default overridden by user-specified output path w/ extension overriden by user-specified format in opts

uv pip install -r ./test_requirements/requirements_optional.txt
# Install Kaleido v1 instead of the default version
uv pip uninstall -y kaleido
uv pip install 'git+https://github.com/plotly/[email protected]#subdirectory=src/py'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uv pip install kaleido>=1.0.0 || uv pip install 'git+https://github.com/plotly/Kaleido.git@latest-tag#subdirectory=src/py' is my recommendation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature something new P1 needed for current cycle
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants