Skip to content

Send pre-renderings with plot updates #894

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

Merged
merged 1 commit into from
Aug 21, 2025
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
9 changes: 8 additions & 1 deletion crates/amalthea/src/comm/plot_comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ pub struct RenderParams {
pub format: PlotRenderFormat,
}

/// Parameters for the Update method.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct UpdateParams {
/// Optional pre-rendering data for immediate display
pub pre_render: Option<PlotResult>,
}

/**
* Backend RPC request types for the plot comm
*/
Expand Down Expand Up @@ -172,7 +179,7 @@ pub enum PlotFrontendReply {
#[serde(tag = "method", content = "params")]
pub enum PlotFrontendEvent {
#[serde(rename = "update")]
Update,
Update(UpdateParams),

#[serde(rename = "show")]
Show,
Expand Down
27 changes: 25 additions & 2 deletions crates/ark/src/plots/graphics_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use amalthea::comm::plot_comm::PlotRenderFormat;
use amalthea::comm::plot_comm::PlotRenderSettings;
use amalthea::comm::plot_comm::PlotResult;
use amalthea::comm::plot_comm::PlotSize;
use amalthea::comm::plot_comm::UpdateParams;
use amalthea::socket::comm::CommInitiator;
use amalthea::socket::comm::CommSocket;
use amalthea::socket::iopub::IOPubMessage;
Expand Down Expand Up @@ -585,9 +586,31 @@ impl DeviceContext {
return;
});

let value = serde_json::to_value(PlotFrontendEvent::Update).unwrap();
// Create a pre-rendering of the updated plot
let settings = self.prerender_settings.get();
let update_params = match self.render_plot(id, &settings) {
Ok(pre_render) => {
let mime_type = Self::get_mime_type(&settings.format);

let pre_render = PlotResult {
data: pre_render.to_string(),
mime_type: mime_type.to_string(),
settings: Some(settings),
};

UpdateParams {
pre_render: Some(pre_render),
}
},
Err(err) => {
log::warn!("Can't pre-render plot update: {err:?}");
UpdateParams { pre_render: None }
},
};

let value = serde_json::to_value(PlotFrontendEvent::Update(update_params)).unwrap();

// Tell Positron we have an updated plot that it should request a rerender for
// Tell Positron we have an updated plot with optional pre-rendering
socket
.outgoing_tx
.send(CommMsg::Data(value))
Expand Down
Loading