3
$\begingroup$

I would like to set up a plot viewer that can display plots from a Wolfram Engine session. (Something similar to python where when we plot something, the Graphic appears in a new window). The plot doesn't have to be interactive as the main idea is to quickly see what the plot looks like for the given code.

Of course, I can link the kernel with Jupyter notebooks and get an experience that closely resembles that of Mathematica notebooks, but I am curious to know if a plot viewer can be set up that can be directly invoked from the terminal.

I would really appreciate any tips and suggestions. Thank you.

$\endgroup$
6
  • $\begingroup$ What platform are you on and what is the name of your plot viewer? $\endgroup$
    – Syed
    Nov 14, 2021 at 21:09
  • $\begingroup$ I'm on macOS and the plot viewer is the one that cames with matplotlib in python $\endgroup$
    – Sâu
    Nov 14, 2021 at 21:12
  • 1
    $\begingroup$ SystemOpen[File["C:\\test.pdf"]] runs the acrobat reader and opens this file on my machine. I am on Windows though. Try to see if you can Export a graphic to a certain location on disk. Then use the full path to the file and it should open with the registered application on your OS. This is as much as I can help you since I am not on a Mac. I am using Mathematica not the Wolfram Engine. $\endgroup$
    – Syed
    Nov 14, 2021 at 21:30
  • $\begingroup$ You may consider using Jupyter notebooks with wolfram engine. The plots get rasterised in Jypyter notebooks though. github.com/WolframResearch/WolframLanguageForJupyter $\endgroup$ Nov 15, 2021 at 9:00
  • 5
    $\begingroup$ If viewing plots in PDF format, I would recommend SumatraPDF instead of Acrobat Reader. The reason is that Sumatra is lighter, free and doesn't lock the file, so overwriting the file with a new version is easier. I say this as a user, I'm not linked to Sumatra development at all. $\endgroup$
    – rhermans
    Nov 15, 2021 at 10:32

1 Answer 1

8
$\begingroup$

The basic idea is to set $DisplayFunction or $Post.

I have the following code in my tool bag:

BeginPackage["CommandLineUtilities`Display`"];

WolframPlayer;

Begin["`Private`"];

$TemporaryOutput = FileNameJoin@{$TemporaryDirectory, "WolframKernelOutput"};
If[!DirectoryQ@#, CreateDirectory@#] &@$TemporaryOutput;
WriteToShell[str_] := (
  If[Head@$ShellProcess =!= ProcessObject, $ShellProcess = StartProcess@$SystemShell];
  WriteLine[$ShellProcess, str];
  str
)

MakeNotebook[box_] := Notebook[{Cell@BoxData@box}, WindowSize -> All]

WolframPlayer[expr_, box_] := (
  $WolframPlayerProcess = If[# === {}, StartProcess@"WolframPlayer", None] &@SystemProcesses@"WolframPlayer";
  StringJoin[
    "WolframPlayer ",
    Export[FileNameJoin@{$TemporaryOutput, CreateUUID["CDFOutput-"]<>".cdf"}, MakeNotebook@box, "CDF"]
  ] //WriteToShell;
  ExportString[OutputForm@expr, "Text"]
)

$Epilog := (
  KillProcess/@{$ShellProcess, $WolframPlayerProcess};
  Quiet@DeleteFile@FileNames[__, $TemporaryOutput];
  If[FindFile["end`"] =!= $Failed, << "end`"] (* Original definition. You can read it with OwnValues. *)
)

End[];

EndPackage[];

(*$DisplayFunction = WolframPlayer[#, ToBoxes@#]&;*)
$Post = With[{box = ToBoxes@#},
  If[FreeQ[DynamicBox|DynamicModuleBox|GraphicsBox|Graphics3DBox]@box,
    #,
    WolframPlayer[#, box]
  ]
]&;

This calls WolframPlayer. For non-interactive contents, you can simply export as PNG and use another picture viewer.

Another way to do this is JavaGraphics`. It costs a lot of time on its first loading, so that it may be inconvenient when you frequently restart wolfram kernel sessions.

$\endgroup$

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.