Skip to content

Commit

Permalink
Define plot format enum in comms (#2814)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtmok authored Apr 22, 2024
1 parent 4967231 commit b99a5e3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@
from ._vendor.pydantic import BaseModel, Field


@enum.unique
class RenderFormat(str, enum.Enum):
"""
Possible values for Format in Render
"""

Png = "png"

Jpeg = "jpeg"

Svg = "svg"

Pdf = "pdf"


class PlotResult(BaseModel):
"""
A rendered plot
Expand Down Expand Up @@ -59,7 +74,7 @@ class RenderParams(BaseModel):
description="The pixel ratio of the display device",
)

format: str = Field(
format: RenderFormat = Field(
description="The requested plot format",
)

Expand Down
8 changes: 7 additions & 1 deletion positron/comms/plot-backend-openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@
"name": "format",
"description": "The requested plot format",
"schema": {
"type": "string"
"type": "string",
"enum": [
"png",
"jpeg",
"svg",
"pdf"
]
},
"required": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,13 @@ import { FileFilter } from 'electron';
import { DropDownListBox } from 'vs/workbench/browser/positronComponents/dropDownListBox/dropDownListBox';
import { DropDownListBoxItem } from 'vs/workbench/browser/positronComponents/dropDownListBox/dropDownListBoxItem';
import { IFileService } from 'vs/platform/files/common/files';
import { RenderFormat } from 'vs/workbench/services/languageRuntime/common/positronPlotComm';

export interface SavePlotOptions {
uri: string;
path: URI;
}

export enum PlotFormat {
PNG = 'png',
SVG = 'svg',
PDF = 'pdf',
JPEG = 'jpeg',
}

const SAVE_PLOT_MODAL_DIALOG_WIDTH = 500;
const SAVE_PLOT_MODAL_DIALOG_HEIGHT = 600;
const BASE_DPI = 100; // matplotlib default DPI
Expand Down Expand Up @@ -110,7 +104,7 @@ interface DirectoryState {
const SavePlotModalDialog = (props: SavePlotModalDialogProps) => {
const [directory, setDirectory] = React.useState<DirectoryState>({ value: props.suggestedPath ?? URI.file(''), valid: true });
const [name, setName] = React.useState({ value: 'plot', valid: true });
const [format, setFormat] = React.useState(PlotFormat.PNG);
const [format, setFormat] = React.useState(RenderFormat.Png);
const [width, setWidth] = React.useState({ value: props.plotWidth, valid: true });
const [height, setHeight] = React.useState({ value: props.plotHeight, valid: true });
const [dpi, setDpi] = React.useState({ value: 100, valid: true });
Expand All @@ -119,7 +113,7 @@ const SavePlotModalDialog = (props: SavePlotModalDialogProps) => {
const inputRef = React.useRef<HTMLInputElement>(null);

const filterEntries: FileFilter[] = [];
for (const filter in PlotFormat) {
for (const filter in RenderFormat) {
filterEntries.push({ extensions: [filter.toLowerCase()], name: filter.toUpperCase() });
}

Expand Down Expand Up @@ -216,14 +210,14 @@ const SavePlotModalDialog = (props: SavePlotModalDialogProps) => {
}
setRendering(true);
try {
const plotResult = await generatePreview(PlotFormat.PNG);
const plotResult = await generatePreview(RenderFormat.Png);
setUri(plotResult.uri);
} finally {
setRendering(false);
}
};

const generatePreview = async (format: PlotFormat): Promise<IRenderedPlot> => {
const generatePreview = async (format: RenderFormat): Promise<IRenderedPlot> => {
return props.plotClient.preview(height.value, width.value, dpi.value / BASE_DPI, format);
};

Expand Down Expand Up @@ -281,10 +275,10 @@ const SavePlotModalDialog = (props: SavePlotModalDialogProps) => {
keybindingService={props.keybindingService}
layoutService={props.layoutService}
entries={[
new DropDownListBoxItem<PlotFormat, PlotFormat>({ identifier: PlotFormat.PNG, title: PlotFormat.PNG.toUpperCase(), value: PlotFormat.PNG }),
new DropDownListBoxItem<PlotFormat, PlotFormat>({ identifier: PlotFormat.JPEG, title: PlotFormat.JPEG.toUpperCase(), value: PlotFormat.JPEG }),
new DropDownListBoxItem<PlotFormat, PlotFormat>({ identifier: PlotFormat.SVG, title: PlotFormat.SVG.toUpperCase(), value: PlotFormat.SVG }),
new DropDownListBoxItem<PlotFormat, PlotFormat>({ identifier: PlotFormat.PDF, title: PlotFormat.PDF.toUpperCase(), value: PlotFormat.PDF }),
new DropDownListBoxItem<RenderFormat, RenderFormat>({ identifier: RenderFormat.Png, title: RenderFormat.Png.toUpperCase(), value: RenderFormat.Png }),
new DropDownListBoxItem<RenderFormat, RenderFormat>({ identifier: RenderFormat.Jpeg, title: RenderFormat.Jpeg.toUpperCase(), value: RenderFormat.Jpeg }),
new DropDownListBoxItem<RenderFormat, RenderFormat>({ identifier: RenderFormat.Svg, title: RenderFormat.Svg.toUpperCase(), value: RenderFormat.Svg }),
new DropDownListBoxItem<RenderFormat, RenderFormat>({ identifier: RenderFormat.Pdf, title: RenderFormat.Pdf.toUpperCase(), value: RenderFormat.Pdf }),
]} />
</label>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IRuntimeClientInstance, RuntimeClientState } from 'vs/workbench/service
import { Event, Emitter } from 'vs/base/common/event';
import { DeferredPromise } from 'vs/base/common/async';
import { IPositronPlotClient } from 'vs/workbench/services/positronPlots/common/positronPlots';
import { PositronPlotComm } from 'vs/workbench/services/languageRuntime/common/positronPlotComm';
import { PositronPlotComm, RenderFormat } from 'vs/workbench/services/languageRuntime/common/positronPlotComm';

/**
* The possible states for the plot client instance
Expand Down Expand Up @@ -78,7 +78,7 @@ interface RenderRequest {
pixel_ratio: number;

/** The format of the plot */
format: string;
format: RenderFormat;
}

/**
Expand Down Expand Up @@ -263,7 +263,7 @@ export class PlotClientInstance extends Disposable implements IPositronPlotClien
* @param format The format of the plot ('png', 'svg')
* @returns A promise that resolves to a rendered image, or rejects with an error.
*/
public render(height: number, width: number, pixel_ratio: number, format = 'png'): Promise<IRenderedPlot> {
public render(height: number, width: number, pixel_ratio: number, format = RenderFormat.Png): Promise<IRenderedPlot> {
// Deal with whole pixels only
height = Math.floor(height);
width = Math.floor(width);
Expand Down Expand Up @@ -325,7 +325,7 @@ export class PlotClientInstance extends Disposable implements IPositronPlotClien
* @param pixel_ratio The device pixel ratio (e.g. 1 for standard displays, 2 for retina displays)
* @returns A promise that resolves when the render request is scheduled, or rejects with an error.
*/
public preview(height: number, width: number, pixel_ratio: number, format: string): Promise<IRenderedPlot> {
public preview(height: number, width: number, pixel_ratio: number, format: RenderFormat): Promise<IRenderedPlot> {
// Deal with whole pixels only
height = Math.floor(height);
width = Math.floor(width);
Expand Down Expand Up @@ -509,7 +509,7 @@ export class PlotClientInstance extends Disposable implements IPositronPlotClien
height: Math.floor(height!),
width: Math.floor(width!),
pixel_ratio: pixel_ratio!,
format: this._currentRender?.renderRequest.format ?? 'png'
format: this._currentRender?.renderRequest.format ?? RenderFormat.Png
});

this.scheduleRender(req, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ export interface PlotResult {

}

/**
* Possible values for Format in Render
*/
export enum RenderFormat {
Png = 'png',
Jpeg = 'jpeg',
Svg = 'svg',
Pdf = 'pdf'
}

/**
* Event: Notification that a plot has been updated on the backend.
*/
Expand Down Expand Up @@ -63,7 +73,7 @@ export class PositronPlotComm extends PositronBaseComm {
*
* @returns A rendered plot
*/
render(height: number, width: number, pixelRatio: number, format: string): Promise<PlotResult> {
render(height: number, width: number, pixelRatio: number, format: RenderFormat): Promise<PlotResult> {
return super.performRpc('render', ['height', 'width', 'pixel_ratio', 'format'], [height, width, pixelRatio, format]);
}

Expand Down

0 comments on commit b99a5e3

Please sign in to comment.