Skip to content

Re-enable write_image with plotly 0.13 (incl features) #55

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

EricFecteau
Copy link

@EricFecteau EricFecteau commented Jul 19, 2025

This PR re-enables write_image that was disabled due to #8. This comes from the update from kaleido to plotly_static in plotly 0.13. Had to also bring in plotly_static because of this bug.

To manage the backend, I crated feature flags, in the same way that plotly did, described here

I also enabled (and tested) these image formats: png, jpg, webp and svg.

Summary by CodeRabbit

  • New Features

    • Added the ability to export plots as images in multiple formats (PNG, JPG, JPEG, WEBP, SVG) when specific export features are enabled.
  • Chores

    • Updated dependencies and introduced new optional features for enhanced export capabilities.

Copy link

coderabbitai bot commented Jul 19, 2025

Walkthrough

The changes update the project's dependencies and feature flags in the manifest, introducing a new optional dependency and several feature flags for static image export. In the code, conditional compilation enables a fully implemented write_image method in the Plot trait, supporting multiple image formats and error handling when relevant features are enabled.

Changes

File(s) Change Summary
Cargo.toml Bumped version to 0.9.6, updated plotly dependency, added optional plotly_static, and introduced feature flags for static export.
src/common/plot.rs Implemented conditional write_image method in Plot trait with support for multiple image formats and error handling, gated by new feature flags.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Plot
    participant PlotHelper
    participant plotly_static

    User->>Plot: write_image(path, width, height, scale)
    activate Plot
    Plot->>PlotHelper: get_image_format(path)
    PlotHelper-->>Plot: ImageFormat or Error
    alt Supported format
        Plot->>plotly_static: write_image(path, width, height, scale, ImageFormat)
        plotly_static-->>Plot: Result<(), Error>
    else Unsupported or missing extension
        Plot-->>User: Error
    end
    Plot-->>User: Result<(), Error>
Loading

Poem

🐇
New features hop in with flags so neat,
Exporting images—a coder’s treat!
PNG, JPG, or SVG,
Now Plot can save them easily.
With static helpers, errors tamed,
Our plotting rabbit’s never blamed!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dedd69f and 9ed75fe.

📒 Files selected for processing (1)
  • src/common/plot.rs (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/common/plot.rs
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a2e42a5 and cf3b48e.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (2)
  • Cargo.toml (2 hunks)
  • src/common/plot.rs (3 hunks)
🔇 Additional comments (8)
Cargo.toml (5)

3-3: Version bump looks appropriate for new functionality.

The version increment from 0.9.5 to 0.9.6 is appropriate for adding new image export functionality.


21-21: Plotly dependency update aligns with PR objectives.

The update to plotly 0.13 correctly addresses the backend change from kaleido to plotly_static as mentioned in the PR objectives.


22-22: Optional plotly_static dependency correctly configured.

The plotly_static dependency is properly marked as optional, allowing users to opt into static export functionality only when needed.


27-31: Feature flag naming and organization follows good practices.

The feature flags follow a clear naming convention and logically map to the underlying plotly crate features. The separation of different export backends (chromedriver, geckodriver, default) provides good flexibility for users.


30-30: Verify consistency of the static_export_wd_download feature flag

The static_export_wd_download feature (Cargo.toml, line 30) only enables

static_export_wd_download = ["plotly/static_export_wd_download"]

whereas the other static-export features also include plotly_static. This discrepancy could lead to missing symbols or compilation errors if any code in plotly/static_export_wd_download expects plotly_static.

Please confirm whether this omission is intentional. If plotly_static is required, update the feature entry:

-static_export_wd_download = ["plotly/static_export_wd_download"]
+static_export_wd_download = ["plotly/static_export_wd_download", "plotly_static"]
src/common/plot.rs (3)

5-10: Conditional compilation correctly excludes static_export_wd_download.

The conditional compilation attributes correctly exclude the static_export_wd_download feature since it doesn't include the plotly_static dependency, which is consistent with the Cargo.toml configuration.


26-37: Method signature and conditional compilation look correct.

The write_image method is properly gated by the relevant feature flags and has a reasonable signature for image export functionality. The return type using boxed error trait object provides good flexibility for error handling.


108-125: Image export implementation follows good patterns.

The overall structure of the write_image method is well-designed:

  • Properly creates a plotly instance and configures it
  • Uses the helper trait for format detection
  • Delegates to the underlying plotly implementation
  • Returns appropriate error types

The implementation aligns well with the existing methods in the trait.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (1)
src/common/plot.rs (1)

50-62: Great! The error handling suggestion has been implemented.

The get_image_format method now properly returns a Result type with descriptive error messages for unsupported formats, addressing the previous review feedback about replacing panic with proper error handling.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cf3b48e and dedd69f.

📒 Files selected for processing (1)
  • src/common/plot.rs (3 hunks)
🔇 Additional comments (2)
src/common/plot.rs (2)

3-10: LGTM! Imports align with the new plotly_static backend.

The conditional import of ImageFormat from plotly_static correctly implements the feature flag approach described in the PR objectives.


26-37: LGTM! Well-structured method signature with proper feature gating.

The write_image method declaration correctly uses conditional compilation and provides a flexible interface for image export with appropriate error handling.

Comment on lines +122 to +127
if let Some((filename, extension)) = path.into().rsplit_once('.') {
let format = self.get_image_format(extension)?;
plot.write_image(filename, format, width, height, scale)?;
} else {
Err("No extension provided for image.")?;
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Improve error construction and consider edge cases in path handling.

While the error handling has been improved from the previous version, there are two issues to address:

  1. The error construction on line 126 is not idiomatic. Using ? on a string literal doesn't make sense.
  2. The current logic might incorrectly handle paths with multiple dots (e.g., file.backup.png would use file.backup as the filename without extension).

Apply this fix for proper error handling:

         if let Some((filename, extension)) = path.into().rsplit_once('.') {
             let format = self.get_image_format(extension)?;
             plot.write_image(filename, format, width, height, scale)?;
         } else {
-            Err("No extension provided for image.")?;
+            return Err("No extension provided for image.".into());
         }

Consider using the full path including the extension when calling plot.write_image, as most image writing libraries expect the complete filename:

-        if let Some((filename, extension)) = path.into().rsplit_once('.') {
+        let path_str = path.into();
+        if let Some((_, extension)) = path_str.rsplit_once('.') {
             let format = self.get_image_format(extension)?;
-            plot.write_image(filename, format, width, height, scale)?;
+            plot.write_image(&path_str, format, width, height, scale)?;
         } else {
             return Err("No extension provided for image.".into());
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if let Some((filename, extension)) = path.into().rsplit_once('.') {
let format = self.get_image_format(extension)?;
plot.write_image(filename, format, width, height, scale)?;
} else {
Err("No extension provided for image.")?;
}
let path_str = path.into();
if let Some((_, extension)) = path_str.rsplit_once('.') {
let format = self.get_image_format(extension)?;
plot.write_image(&path_str, format, width, height, scale)?;
} else {
return Err("No extension provided for image.".into());
}
🤖 Prompt for AI Agents
In src/common/plot.rs around lines 122 to 127, the error handling uses `?` on a
string literal which is not idiomatic; replace this with a proper error type or
construct a custom error to return. Also, to handle filenames with multiple dots
correctly, avoid splitting the path at the last dot to separate filename and
extension; instead, use the full path including the extension when calling
`plot.write_image`. Adjust the logic to extract only the extension for format
detection but pass the entire filename with extension to the image writing
function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant