Re: How to evaluate a notebook programmatically?
- To: mathgroup at smc.vnet.net
- Subject: [mg98607] Re: How to evaluate a notebook programmatically?
- From: mike.honeychurch at gmail.com
- Date: Tue, 14 Apr 2009 06:16:11 -0400 (EDT)
- References: <grupsj$rf$1@smc.vnet.net>
On Apr 13, 2:33 am, "Paul Ellsmore" <paul.ellsm... at nanion.co.uk> wrote: > Hi, > > In one notebook, "gui.nb", I have the code for a user interface. In anoth= er, > "functional.nb" I have around 3000 lines of code, in several hundred cell= s. > I want to click a button (or something similar) in gui.nb, and tell > Mathematica to execute all the cells in functional.nb. Functional.nb will > use values for global variables that have been assigned in gui.nb, and > gui.nb will display the results of the execution of functional.nb. Seems > straightforward. > > Now in functional.nb, we group all the cells together, so that we can > manually execute them all with a single shift-click. What is the > programmatical equivalent of this? I have looked through all the tutorial= s > etc on controlling notebooks from the front end, frontendtokens etc., but > nothing seems to do quite what I want. The options seem to be: > > Option 1. Merge all the cells in functional.nb, and assign that cell to a > Function, which can then be called from gui.nb. This is highly undesirabl= e, > because if an error occurs, we find out about it at the end of 3000 lines= of > code, with no idea where the error occurred. The cell structure in > Mathematica is just too valuable to throw away. I guess that using workbe= nch > might mitigate some of these issues, but that means my colleagues and I a= ll > having to learn a new (and complicated) tool. > > Option 2. Assign each of the (hundreds of!) cells to a separate function, > and call each of them in turn from gui.nb. A lot of hard work here with t= he > potential for errors in naming and ordering the functions in the two > notebooks, and probably an increase in execution time. > > Option 3. Make all the cells in functional.nb initialization cells, then = get > gui.nb to open functional.nb each time we want to evaluate it, and then > close it again. Very clunky, and again probably slow. > > I am sure there are better options than these? Any ideas? > > Thanks, > > Paul. > > Dr. Paul A. Ellsmore > > Nanion Limited > > Oxford Centre for Innovation > > Mill Street > > Oxford > > United Kingdom > > OX2 0JX > > Tel: +44 (0) 1865 811175 > > Fax: +44 (0) 1865 248594 > without seeing the notebooks my guess would be that you might want to do something like to tag the cells that you want to group and evaluate together, then use NotebookFind to locate those tagged cells and then SelectionEvaluate. Those steps could be combined into a function evaluteCells[yourTags_String]:=(NotebookFind[nb, "yourTags", All, CellTags];SelectionEvaluate[nb];) where nb=EvalutionNotebook[] Mike