Message Boards Message Boards

1
|
20443 Views
|
12 Replies
|
9 Total Likes
View groups...
Share
Share this post:

Saving a notebook as a WL script

Posted 10 years ago

I would like to develop scripts for execution by working first in a notebook. I tried this by save-as a WL package, but clearly, some special syntax is needed -- all my code was commented out (* *). Is there a way to do this? Is there any documentation on the use of save-as-a package in .wl in Mathematica 10?

Best, David

POSTED BY: David Keith
12 Replies

Hi David,

Execute the following in the notebook in quesiton:

SetOptions[EvaluationNotebook[],

  {AutoGeneratedPackage -> Automatic, 
  InitializationCellEvaluation -> False, 
  InitializationCellWarning -> False, 
  StyleDefinitions -> 
   Notebook[{Cell[StyleData[StyleDefinitions -> "Default.nb"]], 
     Cell[StyleData["Input"], InitializationCell -> True]}, 
    Visible -> False, 
    StyleDefinitions -> "PrivateStylesheetFormatting.nb"]}
 ]

Alternatively (I assuem that you are programming in Input cells rather than Code cells), do the following:

Edit the notebook's StyleSheet to modify the Input cell style to make it always an Initialization cell. Then via the Option Inspector for that notebook with its scope set to Selected Notebook set the following options:

AutoGeneratedPackage -> Automatic, 
InitializationCellEvaluation -> False, 
InitializationCellWarning -> False

These details are also contained in a small introductory tutorial that I wrote up a while ago and which I've posted before: I am attaching it here for reference...

--David

Attachments:
POSTED BY: David Reiss
Posted 10 years ago

Thanks, David. This worked, but perhaps there is a better way for me too accomplish this. I am experimenting with the idea of Wolfram Language as a more general tool. Clearly I am following WRI's lead in this -- they are certainly promoting WL as a language which transcends its usage in Mathematica. In this case, I have a Raspberry Pi running headless with remote shell functionality from Windows over my local network. Also I have the Pi configured so its file system is shared. So I can develop code in my workstation environment -- far more powerful than the Pi -- but store the results in or even work out of the memory in the Pi. This can certainly be done in Python or C, but I'm interested in doing it in WL also.

So what I would like to do is develop WL scripts in the notebook environment on the workstation, with consideration for the fact that they will be run without a front end, and save them to the Pi environment. There they can be called as executable scripts. For this it would be really convenient to be able to save the notebook as a text of WL language. (Maybe I should be doing this in Workbench?) Taking this even further, it would be good to have interfaces in/out of the WL scripts corresponding to the Standard Input and Standard Output in the Unix/Linux environment, so these scripts can be spliced together by the usual redirection and pipe operators.

Best, David

POSTED BY: David Keith

I typically stay away from Workbench since I like to use the full arsenal of possibilities that notebooks afford me. If you don't need a notebook directly then you can program a .m file directly in Mathematica. But I'd argue and ask why one would want to do that when one can work directly and document as one goes along in a notebook. By the way, any Input cells that you do not want to have included in the autosaved .m file in the approach above you can set them not to be Initialization Cells and then they will not end up in the resulting .m file.

Another possible approach to doing what you are describing might be to actually execute a notebook (and by pass the saving as a .m file as I described) on the raspberry pi even though it does not have a FrontEnd open associated with it. This is to use the function NotebookEvaluate on the notebook pointing its argument to the notebook;'s file path. One may have to wrap UsingFrontEnd around the NotebookEvaluate call. That's something that I am just suggesting but don't have the setup to test. Give that a try and let me know if it's something that works with your architecture,

POSTED BY: David Reiss
Posted 9 years ago

Wow, this is really good stuff about creating and Package scripts (aka .m files, I suppose). I was wondering, any significant difference between these (.m) files and "Wolfram Language" (.wl) files? Didn't know whether it might just be a change to the naming scheme, or some additional/different features to keep in mind for Version 10.

POSTED BY: Caitlin Ramsey

HI Caitlin,

My belief/understanding is that this is a positioning change to bring the language aspect of the Wolfram Language into focus and to orient it more broadly beyond its embedding in Mathematica itself.

--David

POSTED BY: David Reiss

Hi David,

I begin with Mathematica and try to convert a Mathematica Notebook to a Wolfram script .wls.

Unfortunately, if I apply a simple "Save as .wls script", the generated Wolfram script is commented everywhere (for all the lines)

So I am looking for uncomment only the instructions code lines and keep the real original comments.

How could I apply your suggestion of code snippet for my NoteBook ? Should I put it at the beginning of the Notebook and evaluate it, or at the end of the Notebook ? I don't know what to do.

I am on MacOS 12, I saw there was maybe a solution by select only the Input sections but I don't know there too how to proceed ? Maybe a shortcut exists on MacOS.

I would be grateful if you could help me

POSTED BY: christophe petit
Posted 2 years ago

How could I apply your suggestion of code snippet for my NoteBook ? Should I put it at the beginning of the Notebook and evaluate it, or at the end of the Notebook ? I don't know what to do.

By default, only Initialization cells are saved to .m or .wls uncommented. Place the snippet anywhere in the notebook and evaluate it. It will mark all Input cells as Initialization cells and automatically create a .m file whenever the notebook is saved. No need to manually Save As.

POSTED BY: Rohit Namjoshi

I note that you had a problem in a wl file where all of the code was commented out. I wonder whether there is a bug here. I had the same problem which was not obvious until I opened the file in an editor. Each line in the code had been commented and thus was unable to use it. I think it happened when I moved the code by cut and paste.

There needs to be
a) a means of seeing this problem and
b) a means of editing the problem out.

POSTED BY: Nigel King
Posted 2 years ago

Hi Nigel,

It is not a bug. Only Initialization cells are written uncommented. You can either manually set the cells to Initialization cells or evaluate the code provided by David Reiss earlier in this thread in the notebook. Also see my answer.

POSTED BY: Rohit Namjoshi

The bug was that the cells could not be changed to Code/Initialisation cells within the *.wl format in Mathematica. For some reason even Quit and restart did not enable these cells to change to Code cells but instead remained as commented lines. I could only restore normal operation by editing the lines outside Mathematica.

I have now lost the incorrect notebook and thus am unable to re-demonstrate the issue.

POSTED BY: Nigel King
Posted 2 years ago

Nigel,

I have attached a notebook to demonstrate how this works.

Attachments:
POSTED BY: Rohit Namjoshi
POSTED BY: Nigel King
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract