Skip to content

Commit

Permalink
Fix issue with image output from Jupyter kernels that didn't use base…
Browse files Browse the repository at this point in the history
…64 padding (#20561)

This upgrades `nbformat` and `runtimelib` to handle jupyter types with
even more validation and flexiblity. This also processes any multiline
string data coming from the kernel, including with image data (like
`image/png`). While I was at it I also fixed a longstanding issue around
images by eliminating all whitespace (something `atob` does) and using
the no pad decoder.

Fixes: #17956

Before:

<img width="741" alt="image"
src="https://github.com/user-attachments/assets/37ec2cae-ce78-4475-aaa3-4d785e4015d0">

After:

<img width="727" alt="image"
src="https://github.com/user-attachments/assets/e2431ba2-048b-4205-9898-54f357795a9c">


Release Notes:

- Fixed issue with image output from REPL kernels that didn't use base64
padding
  • Loading branch information
rgbkrk authored Nov 13, 2024
1 parent 3ebb64e commit 466d331
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
13 changes: 6 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ linkify = "0.10.0"
log = { version = "0.4.16", features = ["kv_unstable_serde", "serde"] }
markup5ever_rcdom = "0.3.0"
nanoid = "0.4"
nbformat = "0.3.2"
nbformat = "0.5.0"
nix = "0.29"
num-format = "0.4.4"
once_cell = "1.19.0"
Expand Down Expand Up @@ -408,7 +408,7 @@ reqwest = { git = "https://github.com/zed-industries/reqwest.git", rev = "fd110f
"stream",
] }
rsa = "0.9.6"
runtimelib = { version = "0.16.1", default-features = false, features = [
runtimelib = { version = "0.19.0", default-features = false, features = [
"async-dispatcher-runtime",
] }
rustc-demangle = "0.1.23"
Expand Down
5 changes: 4 additions & 1 deletion crates/repl/src/outputs/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ pub struct ImageView {

impl ImageView {
pub fn from(base64_encoded_data: &str) -> Result<Self> {
let bytes = BASE64_STANDARD.decode(base64_encoded_data.trim())?;
let filtered =
base64_encoded_data.replace(&[' ', '\n', '\t', '\r', '\x0b', '\x0c'][..], "");
let bytes = BASE64_STANDARD_NO_PAD.decode(filtered)?;

let format = image::guess_format(&bytes)?;

let mut data = image::load_from_memory_with_format(&bytes, format)?.into_rgba8();

// Convert from RGBA to BGRA.
Expand Down

0 comments on commit 466d331

Please sign in to comment.