If rasterization of a result fails, try again with Shallow applied (to the result)

This commit is contained in:
cc-wr 2019-07-09 18:57:03 -04:00
parent 5faf681109
commit 70d471dc81
2 changed files with 88 additions and 14 deletions

View File

@ -213,6 +213,9 @@ If[
If[$outputSetToTeXForm, TeXForm, #&]
];
(* hard-coded base64 rasterization of $Failed *)
failedInBase64 = "iVBORw0KGgoAAAANSUhEUgAAADcAAAARCAIAAAD2TKM6AAAAhXpUWHRSYXcgcHJvZmlsZSB0eXBlIGV4aWYAAHjaVYvBDcMwDAP/mqIjyLJM2uMYiQNkg45fuu0n9yApgbT1vi97felp2dgxABc5csRU6P6juNciDXn+X9MfZGWgoRhTluEkhnJXkqKFN+LCAahYcIbnIV8gNQN3o86928QyPusLVffpbh/5eCey76LuBgAAAAlwSFlzAAALEwAACxMBAJqcGAAAADx0RVh0U29mdHdhcmUAQ3JlYXRlZCB3aXRoIHRoZSBXb2xmcmFtIExhbmd1YWdlIDogd3d3LndvbGZyYW0uY29tXKKmhQAAACF0RVh0Q3JlYXRpb24gVGltZQAyMDE5OjA3OjAyIDAzOjExOjExSFD8JQAAA8xJREFUSInVlk9IKl8Ux68ajjThLynMwCKLICSDjBbRxpVSFhFkCtFfBMMKAhcu3GXhQopaJBEFISjhsn9YUGE6zSJqkRURFVpKLQqNcYgZHectpszfez9exoP33u+zmnPm3HO/c865w2XRNA3+eth/WkBOfKjEMAzH8T8o5Sd8qJybm1tcXPz1jCaTyeVyMc8URXV3d2s0Go1GEwgEvppqZGQERdEPldfX1yiKIghydnaWCerp6WFn0dbW9mlekiTdbrfX62VMNpvd398/MDAQCATu7u6+qnJ9fT0SiQAA8gAAJycnzc3NnZ2dfD5foVA4nc7W1lYAQDqd1uv1drudWZOXl/dpXi6XG4lEOBwOY7JYLLVaDQDIz8//qsRs2AAAt9vd2NjodrsXFhYuLy9VKlXmNQRB/7wDwzAAIBaLmc1miUTC5/MHBwfPz8+ZSJvNJpVKpVKpTCazWq2fbkySpMViEYvFAoFAp9M9Pz8z/v39/bq6Oj6fr9FoMueEDQBQKpUoihqNxtvb2+Li4kwlAADxePz6nXQ6DQC4uroKBoPz8/MoimIYZjabmciuri6Hw+FwOMRi8ePj46cqLRaLx+NZXV1dW1u7v78fGxsDAESj0fb29qamJp/PV19f//Ly8hZN0zRN016vVy6Xs9nsoaGh19dXxqnT6QAAzFByOJxYLEa/Q5Lk6enp9PQ0BEE4jtNZ9Pb2Go1G+t9UVla6XK6MmUwmIQja3t5mzI2NjYKCApqm7Xa7RCJJpVKMv6yszOPx0DT9dnpUKtXx8fHW1tbm5ubk5GTmi0dHRymKoigqlUoVFhYCAAiCMBqNQqFweHj48PCQIIh4PP5p5b4jHA4TBNHS0sLlcrlcbkdHB0EQOI7f3NzU1tZmN/Oj4wcHBxiGMVpVKlVm1P6TqakpBEEuLi4QBJmdnc1RFovFoigqY4pEIhaL5ff7SZIkSTKVSpEkCcOwQCAIBoM/LmcDACYmJqqrq/v6+rRarcfjYRr9kzJUVVWJRKJEImGz2XJU2dDQsLu7m06nHx4eAAAwDKvVapPJxExwNBpFEAQAoFAoQqHQ0tISSZIrKytM8JvKnZ0dxoVhmM/n02q1mQL8uJ/BYPD7/UKhsKKioqioCIKgXFTq9fq9vT0YhuVyOVPU5eXlkpKS8vLy0tJSqVTK/POVSuX4+LjBYODxeE6ns6am5m19ZqKtVuvMzAydAwRBhEIhiqJyCc5AUVQoFEomk9lOHMfD4fB3qRKJxNPTU7aHRb/fiY6Ojng8nkwmy7GJv5MPlX8z/4+b2zdkknhkRbjZsAAAAABJRU5ErkJggg==";
(* obtain details on how to connect to Jupyter, from Jupyter's invocation of "KernelForWolframLanguageForJupyter.wl" *)
connectionAssoc = ToString /@ Association[Import[$CommandLine[[4]], "JSON"]];

View File

@ -10,6 +10,7 @@ Description:
Symbols defined:
textQ,
toOutText,
toImageData,
toOutImage
*************************************************)
@ -30,7 +31,7 @@ If[
Get[FileNameJoin[{DirectoryName[$InputFileName], "Initialization.wl"}]]; (* $canUseFrontEnd, $outputSetToTeXForm,
$outputSetToTraditionalForm,
$trueFormatType *)
$trueFormatType, failedInBase64 *)
(************************************
private symbols
@ -229,21 +230,91 @@ If[
];
];
(* generate a byte array of image data for the rasterized form of a result *)
toImageData[result_] :=
Module[
{
(* the preprocessed form of a result *)
preprocessedForm
},
(* preprocess the result *)
If[
Head[result] === Manipulate,
preprocessedForm = result;
,
preprocessedForm = Rasterize[result];
];
(* if the preprocessing failed, return $Failed *)
If[
FailureQ[preprocessedForm],
Return[$Failed];
];
(* now return preprocessedForm as a byte array corresponding to the PNG format *)
Return[
ExportByteArray[
preprocessedForm,
"PNG"
]
];
];
(* generate HTML for the rasterized form of a result *)
toOutImage[result_] :=
StringJoin[
(* display a inlined PNG image encoded in base64 *)
"<img alt=\"Output\" src=\"data:image/png;base64,",
(* the rasterized form of the result, converted to base64 *)
BaseEncode[
UsingFrontEnd[ExportByteArray[
(If[Head[#1] === Manipulate, #1, Rasterize[#1]] &) @
$trueFormatType[result],
"PNG"
]]
],
(* end the element *)
"\">"
Module[
{
(* the rasterization of result *)
imageData,
(* the rasterization of result in base 64 *)
imageDataInBase64
},
(* rasterize the result *)
imageData =
toImageData[
$trueFormatType[result]
];
If[
!FailureQ[imageData],
(* if the rasterization did not fail, convert it to base 64 *)
imageInBase64 = BaseEncode[imageData];
,
(* if the rasterization did fail, try to rasterize result with Shallow *)
imageData =
toImageData[
$trueFormatType[Shallow[result]]
];
If[
!FailureQ[imageData],
(* if the rasterization did not fail, convert it to base 64 *)
imageInBase64 = BaseEncode[imageData];
,
(* if the rasterization did fail, try to rasterize $Failed *)
imageData =
toImageData[
$trueFormatType[$Failed]
];
If[
!FailureQ[imageData],
(* if the rasterization did not fail, convert it to base 64 *)
imageInBase64 = BaseEncode[imageData];
,
(* if the rasterization did fail, use a hard-coded base64 rasterization of $Failed *)
imageInBase64 = failedinBase64;
];
];
];
(* return HTML for the rasterized form of result *)
Return[
StringJoin[
(* display a inlined PNG image encoded in base64 *)
"<img alt=\"Output\" src=\"data:image/png;base64,",
(* the rasterized form of the result, converted to base64 *)
imageInBase64,
(* end the element *)
"\">"
]
]
];
(* end the private context for WolframLanguageForJupyter *)