Skip to content

Commit 8a8bb08

Browse files
authored
Respect plot sizing metadata when present (#1119)
This change adds the ability to change the way plots are drawn using four new values in the `positron` field of `execute_request` - `fig-width` / `fig-height` from Quarto (takes precedence), from the cell options - `output_width_px` and `output_pixel_ratio` from the notebook and/or source editor. These are device metrics that can change at any time. Used together, these values cause most plots in notebooks and Quarto documents to be drawn at the user's preferred size, and at the scaling appropriate for their display. Quarto: <img width="764" height="466" alt="image" src="https://github.com/user-attachments/assets/45053988-afbb-46ad-8b60-b3977efd72a6" /> Jupyter: <img width="774" height="833" alt="image" src="https://github.com/user-attachments/assets/c797ebd8-ad6d-465e-b632-9576cf4f3cd1" />
1 parent 7f8487f commit 8a8bb08

7 files changed

Lines changed: 414 additions & 63 deletions

File tree

crates/amalthea/src/wire/execute_request.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,23 @@ pub struct ExecuteRequest {
4141
}
4242

4343
#[serde_with::skip_serializing_none]
44-
#[derive(Debug, Serialize, Deserialize, Clone)]
44+
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
4545
pub struct ExecuteRequestPositron {
4646
pub code_location: Option<JupyterPositronLocation>,
47+
48+
/// Figure width in inches, set by Quarto
49+
#[serde(rename = "fig-width")]
50+
pub fig_width: Option<f64>,
51+
52+
/// Figure height in inches, set by Quarto
53+
#[serde(rename = "fig-height")]
54+
pub fig_height: Option<f64>,
55+
56+
/// Output area width in pixels
57+
pub output_width_px: Option<f64>,
58+
59+
/// Device pixel ratio of the output area
60+
pub output_pixel_ratio: Option<f64>,
4761
}
4862

4963
#[derive(Debug, Serialize, Deserialize, Clone)]

crates/ark/src/console/console_repl.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1288,12 +1288,20 @@ impl Console {
12881288
reply_tx,
12891289
});
12901290

1291-
// Push execution context to graphics device for plot attribution.
1291+
// Push execution context to graphics device for plot attribution
1292+
// and optional sizing overrides from Quarto.
12921293
let code_location = exec_req.code_location().log_err().flatten();
1294+
let (render_settings, intrinsic_size) = exec_req
1295+
.positron
1296+
.as_ref()
1297+
.map(graphics_device::compute_plot_overrides)
1298+
.unwrap_or((None, None));
12931299
graphics_device::on_execute_request(
12941300
originator.header.msg_id.clone(),
12951301
exec_req.code.clone(),
12961302
code_location,
1303+
render_settings,
1304+
intrinsic_size,
12971305
);
12981306

12991307
input

crates/ark/src/modules/positron/graphics.R

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,7 @@ finalize_device_arguments <- function(format, width, height, pixel_ratio) {
421421
#' "on screen" be as close to the size in which they are actually printed at,
422422
#' which has always been tricky.
423423
default_resolution_in_pixels_per_inch <- function() {
424-
if (Sys.info()[["sysname"]] == "Darwin") {
425-
96L
426-
} else {
427-
72L
428-
}
424+
.ps.Call("ps_graphics_default_dpi")
429425
}
430426

431427
#' Determines the default device `type` for png, jpeg, and tiff

0 commit comments

Comments
 (0)