Import a .wl file into .nb format to help understand and study it

In Mathematica, a package is often stored in .wl file format, which has a very different writing style and execute behavior from those of the notebook (.nb) file format.

IMO, the .nb file is more straightforward to learn, study, and develop. So, I want to know if I can import a .wl file into .nb format to help understand and study it.

Regards,
HZ

No problem. Just open the package .wl file and save it as a .nb

1. SimplePackage.wl (285 Bytes)
2. SimplePackage.nb (2.83 KB)

Thank you for your minimal example. But for a truly complex project, should I develop in nb format first, then convert to wl?

Regards,
HZ

I do the development, function by function, in notebooks. Once a function is debugged I copy it to the package wl file.

Thank you for sharing your practice.

Another problem is: Take the following code snippet as an example, as shown in notebook:

{{1, 2}, {3, 4}} // MatrixForm

If I exported it into wl format, I want the beautification related command, e.g., MatrixForm used here, is automatically removed, but I still observed the following exported result via Save As:

$ cat Untitled-1.wl 
(* ::Package:: *)

(* ::Input:: *)
(*{{1,2},{3,4}}//MatrixForm*)

I’m not sure if there are any tricks to solve this problem.

Regards,
HZ

Hongyi,

Only Initialization cells are saved to the wl file as uncommented code. Add the Initialization Cell property to the cells you want to export as code. From the menu Cell / Cell Properties / Initialization Cell. On a Mac the shortcut is Control 8.

You could also add the following to the notebook

SetOptions[EvaluationNotebook[], AutoGeneratedPackage -> Automatic]

After evaluating it, whenever the notebook is saved a corresponding .m file is created for all Initialization cells. I often put expressions that are used in many different projects into such a notebook and use Get to use the expressions in the .m file in another notebook.

Rohit Namjoshi,

Thank you for your comments. But according to the explanation here :

The .m suffix is the old way of saving package files, and is used only for backwards compatibility. Use .wl for new stuff.

Hongyi,

For backwards compatibility AutoGeneratedPackage -> Automatic still uses the old .m extension even though .wl is recommended. Matlab also uses the .m extension which is probably why its use for Wolfram Language files is discouraged.

I checked with the following notebook, and the corresponding .m file will be updated each time I save the notebook.

But I hope that the following line appears in the .m file:

PauliMatrix/@{1,2,3}

Instead of the following one:

PauliMatrix/@{1,2,3}//MatrixForm

Regards,
HZ

SimplePackage.m (1.39 KB)
SimplePackage.nb (7.91 KB)

I don’t understand the question. If you want this in the output .m file

PauliMatrix/@{1,2,3}

then you have to have the same expression in the input .nb file. You have this in the input .nb file

PauliMatrix /@ {1, 2, 3} // MatrixForm

so that is what appears in the output .m file.

I mean: Stripping the // MatrixForm when saved as .m file automatically.

How do you expect the system to know what expressions you want to be modified and exactly how they should be modified before they are saved?

What if you have

mf[m_] := Row[{"The matrix is:", MatrixForm[m]}]
PauliMatrix /@ {1, 2, 3} // mf

Do you expect // mf to be removed?

Your question appears to be an XY Problem.

It’s not exactly an XY Problem. My core idea is how to automatically remove code fragments that beautify and increase readability when saving as a .wl file, thus increasing the compactness and simplicity of the source code.