Message Boards Message Boards


Beautifier for .wl and .m code ?

Posted 6 years ago
6 Replies
2 Total Likes

Is there a way to "Save As..." from a Notebook to a .m or .wl file without uglifying the code?

Is there a beautifier for Mathematica .m or Wolfram .wl code?

I like to develop code in Mathematica notebooks because the integrated tools (like plotting) make development very fast and easy. When I'm done, I like to save code as either Wolfram .wl packages or Mathematica .m packages for deployment, distribution, and collaboration. However, it is a HUGE pain and shame because I don't know a way to preserve all the indentation that the notebook puts in. Nobody bothers to look at the uglified code and I don't know any tools for beautifying it. I tried to bring up some old emacs packages, but they're ancient and very crusty.

POSTED BY: Brian Beckman
6 Replies

Brian, Your posting brings up a number of good questions. There are real problems and there may be some things you are not aware of.

  1. Has Wolfram anywhere explained the difference between .wl and .m packages? Is the .m suffix deprecated? What is the difference in their behavior?

  2. The .m and .wl files can have all the Title and Sectional structure that a regular notebook has. So it's possible to make a package file have a nice organization even including Text cells and not be just a lot of code more or less run together.

  3. Like you, I like to develop routines in regular notebooks, usually where the need for the routine arose, and then after using it for a while I transfer it to a package file. I do this by having an established package and moving the routine into the package and making it a Code cell. I don't try to convert the entire notebook to a package because the main purpose of the notebook was not to be a package.

  4. Hopefully, by this time, the routine will be relatively stable so there might not be much need to edit it. Nevertheless, sometimes there is and that's where some of the problems come in. If you are just making small fix-ups the best thing to just edit the Code cell. If you convert to an Input cell then indentation problems occur and it's not possible to nicely edit the cell. That's a problem that Wolfram should fix but I doubt if they take these things seriously. It's one extra little barrier in building applications, and applications could be the best thing about Mathematica.

  5. You can reformat the Input cell by using Ctrl+Shift+N but that puts it all in a standard form and you have to add back all the line returns and spacing and reformat postfix statements and such things. It also gets rid of all comments. There is no really good reason to delete comments in this operation.

  6. If you have some extensive routines that are really important, you could create separate development notebooks for each one and keep the current input form in its development notebook. Unprotect, develop and test it there and then copy it to the package.

Posted 6 years ago

David, thank you for your thoughtful answer. I don't know a difference between .wl files and .m files. I also don't know a difference between .nb and .cdf files. I have tried a few times to use the Workbench tool ( I think it's based on Eclipse ), which does a better job of beautifying code, but it feels very heavy and slow compared to Notebooks and isn't an inviting place to start, especially given that I prefer a literate style where my code is "inside" a document about the code, rather than a "Doxygen" style, where my documentation is inside the code.

I'll have to keep looking, I suppose. Perhaps I can find the time to write some scripts or meta-Notebooks that take me from Notebooks to packages and back.

POSTED BY: Brian Beckman

It would be helpful, Brian, to know what your larger objective is in using Mathematica. It's not clear enough from your posting. If your objective is to study or develop capability in some subject area and Mathematica is just a tool and medium for doing that, then I have an extended essay A Mathematica Style at my web site that gives a lot of information on doing that: application structure, packages, style sheets, palettes and documentation with Workbench.

I think what you are aiming for is probably doable within Mathematica and WL. It is basically easy once you've had a little practice. Everything can look nice and be as neat and organized as you want. The only extra software you need is Workbench 3 if you want to do documentation. There is a certain lure of "computer science" for many users but unless that is your specific subject area you can leave all the computer science to Wolfram Research. That's what we pay them for. Resist turning interesting math or science into a vast computer science boondoggle.

Posted 6 years ago

Hi, David

I use Mathematica for physics, mathematics, engineering, and ... most importantly ... designing and prototyping software. I write software in C, C++, Python, Clojure, Common Lisp, and many other languages, but I usually start in Mathematica, work out the design (interfaces, functions, protocols, data types), then transcribe into other languages as needed. For this kind of use, Notebooks are (usually) ideal, because I can write about my design and prove correctness results with prototypes written in Mathematica.

Occasionally, I need to collaborate on Mathematica code with other people, and that's happening now. For this kind of application, I need the Mathematica code in pure-text form, so that we can generate textual diffs in git repositories. That's where I have a need to take my Notebooks and save them out as .wl or .m files for collaboration.

POSTED BY: Brian Beckman

Hi Brian,

This is not something I have direct experience in. Maybe you could start a new posting with a title something like "Prototyping C++ Code with Mathematica?" and see if there are other people with experience in this. You might also post on Mathematica Stack Exchange. Also you might contact Wolfram Research directly. It sounds like this already works productively for you but the problem is transferring your output to other people who don't have Mathematica. What resources and environment do they have? Is your Wolfram Language code close enough to the target language to be really useful? I would think there are many detailed techniques to carry this off successfully and other people might be interested in your own experience.

I made a package using the menu item New/Package. After some fooling around, I got the code I had already made pasted into the Package framework. When I saved the file, it has a .wl extension.

I used the menu item Install... to install the package. This worked just fine. However, when I looked at the file, it has a .m extension.

My guess is that the .wl file is for this new package format, which has some modest debugging and formatting. If you did not need to make documentation, it would serve as a Workbench replacement.

The .m files essentially fill the same role that they did from version 1.

I'd be glad to get some clarification about this from someone from Wolfram Research.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract