Re: How to evaluate a notebook programmatically?
- To: mathgroup at smc.vnet.net
- Subject: [mg98627] Re: How to evaluate a notebook programmatically?
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Tue, 14 Apr 2009 06:19:53 -0400 (EDT)
- References: <firstname.lastname@example.org>
Paul Ellsmore wrote: > Hi, > > > > In one notebook, "gui.nb", I have the code for a user interface. In another, > "functional.nb" I have around 3000 lines of code, in several hundred cells. > 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 tutorials > 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 undesirable, > 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 workbench > might mitigate some of these issues, but that means my colleagues and I all > 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 the > 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 > If you move your code from a notebook to a .m file (often known as a package file, but the code does not need to be a package), you will be able to execute the code by calling Get. Since version 6, .m files edit inside Mathematica beautifully - you can even have headings in the code (which are stored inside the .m file as special comments) - it is easy to forget if you are editing a notebook or a package file. David Bailey http://www.dbaileyconsultancy.co.uk