0
$\begingroup$

This question is a further question to my previous one. The solution for that question works well as long as the original input Manipulate code is still provided in the notebook. But in general, there are cases that only the output cell is included and the input code omitted. In such case, I think we could possily make the Manipulate remembers its own input code, and by request (e.g. a button) we can show the code.

A first attempt starts with either DynamicModule or Pattern. For instance, I tried

DynamicModule[{mc = HoldComplete@Manipulate[
 Column[{
   Plot[Sin[x - n], {x, 0, 12.4}],
   Button["show code",
    NotebookPut[Notebook[{Cell[BoxData[ToBoxes[mc]], "Intput"]}], 
     EvaluationNotebook[]]
    ]}], {n, .3, 11.8}]},
 ReleaseHold[mc]]

where mc is the short for 'manipulatecode'. But the code will not be shown if we click the button, only the output instead.

I also tried

mem : Manipulate[
Column[{
Plot[Sin[x - n], {x, 0, 12.4}],
Button["show code",
 NotebookPut[Notebook[{Cell[BoxData[ToBoxes[mc]], "Intput"]}], 
  EvaluationNotebook[]]
 ]}],
{n, .3, 11.8},
Initialization :> (mc := HoldComplete[mem];)]

But this will not work also. Is there a way to let the button creates a new cell and show the Manipulate code in this cell (better in expression format)?

$\endgroup$
  • $\begingroup$ Do you want to store the code to re-evaluate Manipualteor to show it? Printable CellData is not readable $\endgroup$ – Kuba Oct 7 '13 at 6:55
2
$\begingroup$
manipulatecode = NotebookRead@EvaluationCell[];

Manipulate[
 Column[{
         Plot[Sin[x - n], {x, 0, 1}],
         Button["PrintCode", CellPrint[manipulatecode];]
       }]
 , {n, 0, 1}
 , SaveDefinitions -> True
 ]

And then, even if you delete the input code you can print it. Does it fit your needs?

$\endgroup$
  • $\begingroup$ Thank you Kuba, I can try to add some additional functions based on your solution now. Previously I was thinking on the direction of meta programming, but I was too busy to finish it. $\endgroup$ – saturasl Oct 14 '13 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.