Skip to main content

programming - Programmatically generate packages from notebook files?


Currently, there are two ways I know of to generate a package (.m) file from a notebook file; one is to set the AutoGeneratedPackage option in the Option Inspector, and the other is to use the menu optoin to "Save as" a package. The first doesn't really suit my needs because it creates the auto-generated packages in the same directory as the notebook file, which clutters things up (there are dozens of notebooks in this directory) and adds a needless complication to keeping the set-up version controlled. The second option is less than ideal because I don't know how to invoke the option programmatically.


In theory, I know it might be possible with FrontEnd commands, but I'd really prefer to be able to do this without a front end if at all possible. This is something I want to do from GNU make, and while I know you can now set up front ends from the command line, ideally I could avoid that.



Answer



I tried the ExportPacket approach described in Chris Degnen's answer, and while it's very simple, the need for FrontEndExecute and the difficulties dealing with cells of text and section headings and the like make it less appealing. Here's a sketch of the approach I'll ultimately be using, though I haven't filled all the details in yet:


notebookToPackage[notebook_String, package_String] :=
With[{nbObj = Get@notebook},
If[
nbObj === $Failed,
$Failed,

Export[package, notebookToStrings@nbObj, {"Text", "Lines"}]]]

This just loads the notebook file and does some basic error handling and exports the result after notebookToStrings converts the mess into strings. Using Get to get the notebook is the easiest way to get it as an object with head Notebook, and from there you can get at the Cell structures.


notebookToStrings[nbObj_] :=
With[{cells =
Cases[nbObj,
Cell[body_, style : "Code" | "Text", ___] :> {body, style},
Infinity]},
Riffle[
Replace[cells,

$cellRules, {1}],
""]]

Since notebook objects are Mathematica structures like everything else, you can use the same old structural manipulations and rule replacements that you use everywhere else. Cases proves once again that, with the Infinity level specification, it's the most useful function in the Mathematica library, and the pattern matching means you can whitelist some cells while not having to decide what you want to do about input or output cells or subsections or whatever. Everything else is done using rules. Riffle is just there to add white space to aesthetics.


Lastly, we have the rules, and some helper functions:


$cellRules = {
{body_, "Code"} :> boxesToStrings[body],
{body_String, "Text"} :> commentate[body]};

The contents of the "Code" cells are going to be boxes, and I turn the text in text cells into comments:



$scratchContext = "Scratch`Private`";

Attributes[stringulate] = {HoldAllComplete};
stringulate[expr_] :=
ToString[Unevaluated[expr], InputForm, CharacterEncoding -> "ASCII",
PageWidth -> 72]

boxesToStrings[boxes_] :=
Block[{$Context = $scratchContext},
stringulate @@

ToExpression[StripBoxes[boxes], StandardForm, HoldComplete]]

There's a little extra ceremony to keep this function from interning symbols into random contexts, and I had to introduce an extra function, stringulate, to keep everything held until I make it into a string. The handling for text is simpler:


commentate[s_String] :=
ToString["(*\n" <> s <> "\n*)", OutputForm,
CharacterEncoding -> "ASCII", PageWidth -> Infinity]

Ultimately, I'll probably add some rules for handling "Title" cells, "Section"/"Subsection"/... cells, and the like. In particular, if you have a pair of comments like this:


(* ::Section:: *)    
(* The section title *)


the code editor in Wolfram Workbench will fold the thing up like a notebook section when you double click on it. In the end, I'll probably also want to hold onto the whole Cell expressions so that I can pick out input cells that are also initialization cells, because those should be dumped to the package as well.


Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...