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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading