Message Boards Message Boards

0
|
4105 Views
|
13 Replies
|
1 Total Likes
View groups...
Share
Share this post:

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

Posted 2 years ago

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

POSTED BY: Hongyi Zhao
13 Replies
Posted 2 years ago

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.

POSTED BY: Hongyi Zhao
Posted 2 years ago

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

POSTED BY: Hongyi Zhao
Posted 2 years ago

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.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

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

POSTED BY: Hongyi Zhao
Posted 2 years ago

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.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

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.
POSTED BY: Hongyi Zhao
Posted 2 years ago

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.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

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

POSTED BY: Hongyi Zhao
Posted 2 years ago

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.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

Thank you for sharing your practice.

POSTED BY: Hongyi Zhao
Posted 2 years ago

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

POSTED BY: Hans Milton
Posted 2 years ago

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

POSTED BY: Hongyi Zhao
Posted 2 years ago

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

POSTED BY: Hans Milton
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