Skip to content

Commit

Permalink
Add other plot save formats (#2729)
Browse files Browse the repository at this point in the history
Support multiple formats for plot saving
Add SVG, PDF, JPEG support
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
  • Loading branch information
timtmok authored Apr 15, 2024
1 parent d6e75aa commit b41cf05
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class RenderParams(BaseModel):
description="The pixel ratio of the display device",
)

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


class RenderRequest(BaseModel):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
DEFAULT_HEIGHT_IN = 4.8
BASE_DPI = 100

MIME_TYPE = {
"png": "image/png",
"svg": "image/svg+xml",
"pdf": "application/pdf",
"jpeg": "image/jpeg",
}


class PositronDisplayPublisherHook:
def __init__(self, target_name: str, session_mode: SessionMode):
Expand Down Expand Up @@ -112,12 +119,16 @@ def handle_msg(self, msg: CommMessage[PlotBackendMessageContent], raw_msg: JsonR
width_px = request.params.width or 0
height_px = request.params.height or 0
pixel_ratio = request.params.pixel_ratio or 1.0
format = request.params.format or "png"

if width_px != 0 and height_px != 0:
format_dict = self._resize_pickled_figure(pickled, width_px, height_px, pixel_ratio)
data = format_dict["image/png"]
output = PlotResult(data=data, mime_type="image/png").dict()
figure_comm.send_result(data=output, metadata={"mime_type": "image/png"})
format_dict = self._resize_pickled_figure(
pickled, width_px, height_px, pixel_ratio, [format]
)
mime_type = MIME_TYPE[format]
data = format_dict[mime_type]
output = PlotResult(data=data, mime_type=mime_type).dict()
figure_comm.send_result(data=output, metadata={"mime_type": mime_type})

else:
logger.warning(f"Unhandled request: {request}")
Expand Down Expand Up @@ -170,7 +181,7 @@ def _resize_pickled_figure(
new_width_px: int = 614,
new_height_px: int = 460,
pixel_ratio: float = 1.0,
formats: list = ["image/png"],
formats: list = ["png"],
) -> dict:
# Delay importing matplotlib until the kernel and shell has been
# initialized otherwise the graphics backend will be reset to the gui
Expand Down Expand Up @@ -215,11 +226,12 @@ def _resize_pickled_figure(

# Render the figure to a buffer
# using format_display_data() crops the figure to smaller than requested size
figure.savefig(figure_buffer, format="png")
figure.savefig(figure_buffer, format=formats[0])
figure_buffer.seek(0)
image_data = base64.b64encode(figure_buffer.read()).decode()
key = MIME_TYPE[formats[0]]

format_dict = {"image/png": image_data}
format_dict = {key: image_data}

plt.close(figure)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def test_hook_call(hook: PositronDisplayPublisherHook, images_path: Path) -> Non
def render_request(comm_id: str, width_px: int = 500, height_px: int = 500, pixel_ratio: int = 1):
return json_rpc_request(
"render",
{"width": width_px, "height": height_px, "pixel_ratio": pixel_ratio},
{"width": width_px, "height": height_px, "pixel_ratio": pixel_ratio, "format": "png"},
comm_id=comm_id,
)

Expand Down
8 changes: 8 additions & 0 deletions positron/comms/plot-backend-openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
"schema": {
"type": "number"
}
},
{
"name": "format",
"description": "The requested plot format",
"schema": {
"type": "string"
},
"required": false
}
],
"result": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

.plot-preview-input {
height: 100%;
grid-template-rows: 1fr 2fr 4px 9fr;
grid-template-rows: 1fr 1fr 2fr 4px 9fr;
grid-template-areas:
"browse"
"file"
"plot-input"
"preview-progress"
"preview";
Expand All @@ -28,6 +29,17 @@
grid-area: input;
}

.plot-preview-input .file {
display: flex;
flex-direction: row;
column-gap: 10px;
align-items: flex-end;
}

.plot-preview-input .file button {
margin-top: 4px;
}

.plot-input div.error {
padding-top: 4px;
grid-column: 1 / span 3;
Expand All @@ -40,10 +52,14 @@
width: auto;
}

.plot-preview-input .labeled-text-input input {
.plot-preview-input .plot-input .labeled-text-input input {
width: 100px;
}

.plot-preview-input .file .labeled-text-input input {
width: 200px;
}

.plot-preview-input .preview-progress {
grid-area: preview-progress;
display: flex;
Expand Down
Loading

0 comments on commit b41cf05

Please sign in to comment.