6

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.

Sâu
  • 535
  • 2
  • 10
  • What platform are you on and what is the name of your plot viewer? – Syed Nov 14 '21 at 21:09
  • I'm on macOS and the plot viewer is the one that cames with matplotlib in python – Sâu Nov 14 '21 at 21:12
  • 1
    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. – Syed Nov 14 '21 at 21:30
  • You may consider using Jupyter notebooks with wolfram engine. The plots get rasterised in Jypyter notebooks though.

    https://github.com/WolframResearch/WolframLanguageForJupyter

    – Pavel Perikov Nov 15 '21 at 09:00
  • 5
    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. – rhermans Nov 15 '21 at 10:32
  • I would appreciate your feedback. Thanks. – Syed Nov 16 '21 at 04:55

2 Answers2

9

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.

rnotlnglgq
  • 3,740
  • 7
  • 28
3

There is a complete solution, that uses Wolfram Engine and javascript as a frontend

enter image description here

Kirill Vasin
  • 1,235
  • 12
  • 14