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

Add other plot save formats #2729

Merged
merged 9 commits into from
Apr 15, 2024
Merged

Add other plot save formats #2729

merged 9 commits into from
Apr 15, 2024

Conversation

timtmok
Copy link
Contributor

@timtmok timtmok commented Apr 10, 2024

Intent

Address #2657

Approach

Adds a format option when selecting where to save the plot. This adds a new parameter to the render request and updates the Python extension to render the other formats. Ark will have its own PR to add in that support. Until Ark supports the other formats, plot saving will always save as a PNG regardless of the chosen format.

image

QA Notes

Sample plot for Python

import numpy as np
import matplotlib.pyplot as plt

with plt.xkcd():
    # plt in widget mode
    # %matplotlib ipympl
    # v = np.array([1, 3, 5, 365])
    v = np.arange(365, 0, -1)
    # w = np.array([4, 6, 2, 10, 8, 1])

    fig, ax = plt.subplots()
    fig.set_facecolor('whitesmoke')
    fig.set_edgecolor('black')
    ax.set_xlabel('Day of the year')
    ax.set_ylabel('Days until New Year\'s Eve')

    plt.xlim(0, 365)
    plt.ylim(1, 365)

    plt.plot(v)
    # plt.plot(w)

Sample plot for R

library(ggplot2)
data("midwest")
ggplot(midwest, aes(x=area, y=poptotal)) + geom_point()

@timtmok timtmok requested review from jmcphers and seeM April 10, 2024 19:53
@timtmok
Copy link
Contributor Author

timtmok commented Apr 10, 2024

I'm working on fixing the test failure

Copy link
Contributor

@seeM seeM left a comment

Choose a reason for hiding this comment

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

Python changes LGTM!

const extension = path.value.fsPath.split('.').pop()?.toLowerCase();
let format = extension as PlotFormat;

// default to PNG if the format is unknown
Copy link
Contributor

Choose a reason for hiding this comment

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

If the user gets to choose the file format in the modal, should we always use that file format regardless of the file name's extension? Or maybe we could show a validation error if those don't match? Silently defaulting to png feels like it would confuse users

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This part of saving does feel weird. RStudio sets the options to browse for a directory for the file, the file format, and file name. That seems like a better way to reduce user error and simplify validation.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've changed it to use a directory selector and separate file name field. The user selects the format. No need to check the extension anymore as it will be added for the user.

The overwrite check also occurs at save and the user can cancel if they do not wish to overwrite. Any errors during save due to write access or anything else is shown after saving and closing the dialog

image

Copy link
Contributor

Choose a reason for hiding this comment

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

That's much better!

Out of interest, I compared this with VSCode's save modal (you can get there by creating a new untitled file and then saving it). When you select a file format it updates the extension in the file name. If you use a mismatched format and extension, it shows the following error:

image

I wonder if we want to hook into that or if we prefer to roll our own? I don't have any personal preference. This feels like a broader Positron UX question.

* @returns A promise that resolves to a rendered image, or rejects with an error.
*/
public render(height: number, width: number, pixel_ratio: number): Promise<IRenderedPlot> {
public render(height: number, width: number, pixel_ratio: number, format = 'png'): Promise<IRenderedPlot> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Will we want to remove these defaults once ARK supports the format arg?

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, that would be good to avoid the confusion of falling back to a png when the user selected something else.

timtmok added 8 commits April 12, 2024 13:29
Add format selector in dialog
Browse now sets directory save location
Add field for file name
Validate path at save time
Show error if saving failed
@timtmok
Copy link
Contributor Author

timtmok commented Apr 12, 2024

posit-dev/ark#311

I've opened the PR for Ark

Copy link
Contributor

@seeM seeM left a comment

Choose a reason for hiding this comment

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

LGTM

@timtmok
Copy link
Contributor Author

timtmok commented Apr 15, 2024

@jonvanausdeln I'm updating the spec to reflect the changes in choosing the file path and format.

@timtmok timtmok merged commit b41cf05 into main Apr 15, 2024
24 checks passed
@timtmok timtmok deleted the plot-save-formats branch April 15, 2024 15:49
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.

2 participants