6
$\begingroup$

Suppose I have a notebook template.nb like this:

enter image description here

and I want to evaluate this notebook from the command line. So I wrote this Mathematica script:

UsingFrontEnd[NotebookEvaluate["C:\\Users\\delfinog\\Desktop\\template.nb", 
                               InsertResults -> True]]

And I run the script like this (Windows 7):

enter image description here

C:> "C:\Program Files\Wolfram Research\Mathematica\10.2\wolfram.exe" -script C:\Users\delfinog\Desktop\mma-script.wl

My expectation is that I should be getting this:

enter image description here

but instead, I get this:

enter image description here

This is the complete output cell

Graphics[{{{}, {}, {Directive[Opacity[1.], RGBColor[0.368417, 0.506779, 0.709798], AbsoluteThickness[1.6]], Line[{{2.040816326530612*^-8, 2.040816326530612*^-8}, {0.000306717908041361, 0.000306717908041361}, {0.0006134154079194566, 0.0006134154079194566}, {0.001226810407675648, 0.001226810407675648}, {0.002453600407188031, 0.002453600407188031}, {0.004907180406212797, 0.004907180406212797}, {0.009814340404262328, 0.009814340404262328}, {0.01962866040036139, 0.01962866040036139}, {0.040908355415730624, 0.040908355415730624}, {0.060777881089422996, 0.060777881089422996}, {0.08025764554305448, 0.08025764554305448}, {0.10138846501985709, 0.10138846501985709}, {0.12110911515498282, 0.12110911515498282}, {0.1424808203132797, 0.1424808203132797}, {0.16346276425151565, 0.16346276425151565}, {0.18303453884807477, 0.18303453884807477}, {0.204257368467805, 0.204257368467805}, {0.22407002874585835, 0.22407002874585835}, {0.24349292780385082, 0.24349292780385082}, {0.2645668818850144, 0.2645668818850144}, {0.28423066662450114, 0.28423066662450114}, {0.305545506387159, 0.305545506387159}, {0.32647058492975595, 0.32647058492975595}, {0.34598549413067603, 0.34598549413067603}, {0.36715145835476726, 0.36715145835476726}, {0.3869072532371816, 0.3869072532371816}, {0.40831410314276706, 0.40831410314276706}, {0.42933119182829166, 0.42933119182829166}, {0.4489381111721394, 0.4489381111721394}, {0.4701960855391582, 0.4701960855391582}, {0.49004389056450015, 0.49004389056450015}, {0.5095019343697813, 0.5095019343697813}, {0.5306110331982336, 0.5306110331982336}, {0.550309962685009, 0.550309962685009}, {0.5716599471949555, 0.5716599471949555}, {0.5926201704848412, 0.5926201704848412}, {0.6121702244330499, 0.6121702244330499}, {0.6333713334044297, 0.6333713334044297}, {0.6531622730341328, 0.6531622730341328}, {0.6725634514437749, 0.6725634514437749}, {0.6936156848765882, 0.6936156848765882}, {0.7132577489677245, 0.7132577489677245}, {0.7345508680820321, 0.7345508680820321}, {0.7544338178546627, 0.7544338178546627}, {0.7739270064072324, 0.7739270064072324}, {0.7950712499829733, 0.7950712499829733}, {0.8148053242170373, 0.8148053242170373}, {0.8361904534742725, 0.8361904534742725}, {0.8571858215114467, 0.8571858215114467}, {0.8767710202069441, 0.8767710202069441}, {0.8980072739256126, 0.8980072739256126}, {0.9178333583026043, 0.9178333583026043}, {0.937269681459535, 0.937269681459535}, {0.9583570596396369, 0.9583570596396369}, {0.978034268478062, 0.978034268478062}, {0.9783774827142147, 0.9783774827142147}, {0.9787206969503675, 0.9787206969503675}, {0.9794071254226728, 0.9794071254226728}, {0.9807799823672838, 0.9807799823672838}, {0.9835256962565057, 0.9835256962565057}, {0.9890171240349493, 0.9890171240349493}, {0.989360338271102, 0.989360338271102}, {0.9897035525072548, 0.9897035525072548}, {0.9903899809795602, 0.9903899809795602}, {0.9917628379241712, 0.9917628379241712}, {0.994508551813393, 0.994508551813393}, {0.9948517660495457, 0.9948517660495457}, {0.9951949802856985, 0.9951949802856985}, {0.9958814087580039, 0.9958814087580039}, {0.9972542657026149, 0.9972542657026149}, {0.9975974799387677, 0.9975974799387677}, {0.9979406941749204, 0.9979406941749204}, {0.9986271226472259, 0.9986271226472259}, {0.9989703368833786, 0.9989703368833786}, {0.9993135511195312, 0.9993135511195312}, {0.999656765355684, 0.999656765355684}, {0.9999999795918367, 0.9999999795918367}}]}}}, {DisplayFunction -> Identity, AspectRatio -> GoldenRatio^(-1), Axes -> {True, True}, AxesLabel -> {None, None}, AxesOrigin -> {0, 0}, DisplayFunction :> Identity, Frame -> {{False, False}, {False, False}}, FrameLabel -> {{None, None}, {None, None}}, FrameTicks -> {{Automatic, Automatic}, {Automatic, Automatic}}, GridLines -> {None, None}, GridLinesStyle -> Directive[GrayLevel[0.5, 0.4]], Method -> {DefaultBoundaryStyle -> Automatic, DefaultMeshStyle -> AbsolutePointSize[6], ScalingFunctions -> None}, PlotRange -> {{0, 1}, {0., 0.9999999795918367}}, PlotRangeClipping -> True, PlotRangePadding -> {{Scaled[0.02], Scaled[0.02]}, {Scaled[0.05], Scaled[0.05]}}, Ticks -> {Automatic, Automatic}}]

So the figure is there, but it is not being displayed. Is there a way to make this work? I was thinking that the UsingFrontEnd command should avoid this situation but there seems to be something else missing.

Motivation: I am developing a report generation system that should be running automatically late at night. As the built-in template system does not work well from MathematicaScript (CASE:3432062) and using ScheduledTasks on my interactive Mathematica session has been unreliable, I am exploring using NotebookEvaluate with the InsertResults -> True option as an alternative template system.

$\endgroup$
  • 1
    $\begingroup$ Your question is actually one part of the answer I gave to a similar problem few days after yours ! I also noted that graphics were not rendered but did not find the solution you gave below ! Also, you'll probably be interested in the other solution proposed which does not use the -script option but the -ne option directly on the notebook. However with this second approach it is impossible to import/export data ! $\endgroup$ – SquareOne Nov 12 '15 at 22:04
6
$\begingroup$

This is because the default format type is OutputForm when running on the command line. Add this to the beginning of your script to change it to StandardForm:

SetOptions[First[$Output], FormatType -> StandardForm]

References:

$\endgroup$
4
$\begingroup$

I have found a solution. Just adding a CellPrint makes the plot visible. In fact this also makes the version 10 templates work from command line scripts.

enter image description here

Anyway, it would still be good to know why the CellPrint is needed. I discovered this by pure luck.

$\endgroup$
  • $\begingroup$ Your discovery has been very useful to me. I'm trying to find a way to have Mathematica evaluate a notebook programmatically for a batch system in Linux, and had exactly your problem. See this thread mathematica.stackexchange.com/questions/98549/… $\endgroup$ – Pincopallino Nov 13 '15 at 9:43
1
$\begingroup$

The template notebook can be run as-is in Windows using a batch file.

template.nb

Plot[x, {x, 0, 1}]

runTemplate.m

NotebookPauseForEvaluation[nb_] := Module[{},
  While[NotebookEvaluatingQ[nb], Pause[0.25]]] 

NotebookEvaluatingQ[nb_] := Module[{},
  SelectionMove[nb, All, Notebook]; 
  Or @@ Map["Evaluating" /. # &, Developer`CellInformation[nb]]] 

UsingFrontEnd[nb = NotebookOpen[FileNameJoin[
     {$InitialDirectory, "template.nb"}], Visible -> True];
  SelectionMove[nb, All, Notebook]; SelectionEvaluate[nb]; 
  NotebookPauseForEvaluation[nb]; NotebookSave[nb];
  NotebookClose[nb];
  DeleteFile["C:\\Temp\\wait"]]

runTemplate.bat

@echo off
setlocal
PATH = C:\Program Files\Wolfram Research\Mathematica\11.0\;%PATH%
echo running template.nb %TIME%
break > "C:\Temp\wait"
start /min WolframKernel -noprompt -initfile runTemplate.m
:Label
ping localhost -n 2 > nul
if exist "C:\Temp\wait" goto Label
ping localhost -n 1 > nul
echo Terminating WolframKernel %TIME%
taskkill /f /fi "IMAGENAME eq WolframKernel.exe" > nul
ping localhost -n 3 > nul
endlocal

Note. Terminating the WolframKernel in this way on a network licence leaves the licence held until it expires five minutes later. So if this used for batching and the jobs run in under five minutes the licence pool will be depleted.

On a standalone licence there is no problem though. Once it is terminated it can be relaunched immediately.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.