0
$\begingroup$

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
$\endgroup$

1 Answer 1

2
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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