Re: How do I script the evaluation of a Notebook
- To: mathgroup at smc.vnet.net
- Subject: [mg30564] Re: How do I script the evaluation of a Notebook
- From: Tom Burton <tburton at cts.com>
- Date: Wed, 29 Aug 2001 01:40:08 -0400 (EDT)
- References: <9m6ted$l8f$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hello, On Sat, 25 Aug 2001 01:02:05 +0000 (UTC), in comp.soft-sys.math.mathematica you wrote: >I'd like to programmatically evaluate a Notebook multiple times in a loop. >Although I don't know how to write it, here is the Mathematic pseudo-code >that I'd like to learn how to write: > >nb = NotebookOpen["foo.nb"] >Do[NotebookEvaluate[nb], {myVar,4}] ... I do a more complex variant of this, where the notebook to be executed is read from a template, altered, and then executed repeatedly in a loop. (Each iteration of the loop writes a chapter in an automatically generated report.) To the advice you have already received, I want to point out a wrinkle that you may face. If your one and only kernel is busy executing the outer loop, then, if the NotebookEvaluate operation also requires a kernel (the FrontEnd process cannot do everything a kernel can do), then the evaluation won't happen, because a single kernel cannot process two sets of instructions at once. My license allows (at least) two kernels, so here is how I solved this problem: The notebook containing the outer loop uses the default kernel, which I call local1. The notebooks to be evaluated are set to use a second kernel, which I call local2. Two complications then arise. First, the local1 needs to wait for local 2 finish with one copy of the notebook before preparing the next (at least, I need this). I syncronize the two kernels with the following simple functions. syncFileName = ToFileName[{dirTemplate}, "syncFile.txt"] (* dirTemplate is my template directory *) syncBusy[tf : (True | False)] := Put[tf, syncFileName] syncWait[t_] := While[ReadList[syncFileName, Expression] === {True}, Pause[t]] Second, besides the information passed from one kernel to the other via the notebooks "foo.nb", the kernels need some common initialization. (For instance, they both must initialize the syncronization functions above.) So you need a common (fixed) notebook that both kernels execute as part of their initialization. But all this is easy to set up, provided you can run two or more kernels. If you can and need to run two kernels and would like me to elaborate, please let me know. Tom Burton