Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Lets say I have a file named test.m containing some functions.

test[arg1_, arg2_] := (
  Print[arg1];
  Print[arg2];
)
test3[arg1_, arg2_] := (
  Print[arg1];
  Print[arg2];
)

What would be a good way to generate a list of butons that would allow you to invoke the functions easily? For example.

enter image description here

The tricky part here (and is mainly why I am asking this question) is calling the functions, which will have several different argument types? I had thought about using an InputField with a MessageDialog, but I would bet there is better way to incorporate Mathematica's features.

ClearAll[loadFile];
loadFile[path_String?FileExistsQ] := DeleteCases[ToExpression[
    FromCharacterCode[BinaryReadList[path]]
    , InputForm, HoldComplete], Null];
x = ReleaseHold[
   loadFile["test.m"] /.
    HoldPattern[sym_[args__] := eval_] :>
     Button[sym, eval]
   ];
x[[0]] = List;
x
share|improve this question
add comment

1 Answer

The following code allows you to click the button which will allow you call the function. (functionality still buggy, 2 options 1) add functions to Global namespace 2) figure out some way to allow you to call the functions ).

ClearAll[loadFile];
loadFile[path_String?FileExistsQ] := 
  DeleteCases[
   ToExpression[FromCharacterCode[BinaryReadList[path]], InputForm, 
    HoldComplete], Null];

Cases[
 loadFile["test.m"] /. 
    HoldPattern[sym_[args___] := eval_] :> 
     Button[Defer[sym], CellPrint[Cell[
         BoxData@ToBoxes[Defer[sym[args]]]
         , "Input", Evaluatable -> True, 
         CellEvaluationFunction -> Function[ToExpression[#];], 
         ShowStringCharacters -> True
         ]];
      ] /.
   HoldPattern[Pattern][a__, _[x___]] :> Placeholder[a _ x] /. 
  List :> CompoundExpression,
 Button[___]
 ]

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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