Message Boards Message Boards

0
|
249 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Package function doesn't plot math function when given as input

Posted 13 days ago

Hi everybody! I'm new to Mathematica and I'm trying to learn to write some basic code to create a DialogInput in which the user writes the function to plot.

I'd like to understand how packages work, so I'm trying to move the function GuessTheFunctionGUI in the package, leaving just the call of the package function in the notebook.

When I leave the GuessTheFunctionGUI in the notebook everything works fine, but at the moment when I run the code and write the function in the InputField then the Plot is not dynamically updated (to tell the truth, nothing is plotted).

Any tips on what I'm messing? My idea is that the Private scope of the function doesn't allow updating dynamically the value of the math function in the plot.

Notebook.nb

Get["Package.wl"];

ButtonBox["Start", Appearance -> Automatic, 
  ButtonFunction :> 
   FrontEndExecute[FrontEndToken["EvaluateNotebook"]], 
  Evaluator -> Automatic, Method -> "Preemptive"] // DisplayForm
GuessTheFunctionGUI[];

Package.wl

BeginPackage["Package`"];

GuessTheFunctionGUI::usage = "GuessTheFunctionGUI[]";

Begin["`Private`"];
SetDirectory[NotebookDirectory[]];

GuessTheFunctionGUI[] := DynamicModule[{func = ""},
  DialogInput[
  Column[{
    InputField[Dynamic[func], String, FieldHint -> "Insert the function here"],
    Dynamic@Plot[ToExpression[func], {x, -10, 10}, PlotStyle -> Blue, 
      AxesLabel -> {"x", "y"}, PlotLabel -> "Function plot", ImageSize -> Medium]
    }],
    WindowTitle -> "Interactive Plot", WindowSize -> {500, 500}, 
    WindowMargins -> {{Automatic, 0}, {0, Automatic}}, 
    WindowElements -> {"VerticalScrollBar", "HorizontalScrollBar", "StatusArea"}
  ]]

End[];
EndPackage[];

I don't know if this is relevant too, but the version of Mathematica that I'm using is the following:

$Version
"14.0.0 for Microsoft Windows (64-bit) (December 13, 2023)"
POSTED BY: L P
3 Replies
Posted 13 days ago

Hello! I hate to say this, but for some reason your code works for me, so I'm not sure whether it's because you're using a different version of Mathematica? One thing I did note was that the 'NoteBookDirectory[]' function didn't work for me, so I changed it so that it was set to the actual path of your main file. You could otherwise (I believe) just save to the same directory. If you're not getting an actual error though, that's pretty odd. It might just be worth restarting your PC/Mathematica.

To summate quicker, maybe put in the direct file path to your main file into the directory function, and try that. It seems to suggest something wrong parsing the actual package. Hopefully this helps though :)

POSTED BY: T Vine
Posted 12 days ago

Thanks for your answer! I could put in the direct file path to my main file into the directory function, but I'm trying to develop something that can be used as a standalone software. The idea is that every user that clones my repository can just click the Start button and play with it without having to worry about changing anything.

For the

You could otherwise (I believe) just save to the same directory.

part, I'm already working on a repo that contains both .nb and .wl in the same folder, so I guess that is not the case unfortunately.

I believe that the code that I wrote so far isn't exactly fit for this purpose. Any idea on how I could change it?

P.S. are you on Linux? Because a colleague of mine stated that somehow this code works on his computer.

POSTED BY: L P
Posted 12 days ago

Hey, no problem sorry that won't work! I am on Linux (at least the Raspberry Pi distro), which is odd. If you're planning it as something anyone can just fork, perhaps it would be a good idea to just leave out the NoteBookDirectory function, and instead create a subfolder in your repository where packages are? For example: SRC- - Main.wl -Notebook.nb -PACKAGES - Package.wl And then use <<"PACKAGES/Package.wl" (or Needs[]) in your notebook file? That should hopefully enable it to work from scratch, as you said I hope. Although I'm not sure whether that should fix the original problem, I'll try and take a deeper look into it.

POSTED BY: T Vine
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