diff --git a/lib/main.gd b/lib/main.gd index c6c8da2..5fe3b2e 100644 --- a/lib/main.gd +++ b/lib/main.gd @@ -284,20 +284,14 @@ DeclareGlobalVariable( "ConvertGraphForTool" ); #!

#! If the JupyterViz Package is loaded without the #! JupyterKernel Package already loaded, then it will -#! omit support for Jupyter, and will initialize -#! to +#! initialize to #! , which is what the user probably -#! wants if using ⪆ from a terminal, for example. In that case, this -#! package does not support later reassignment of -#! to any other value, because Jupyter -#! support was not set up at package loading time. -#!

-#! Thus if the user desires support for both Jupyter-based and -#! HTML-based plotting, he or she should load the -#! JupyterKernel Package before the -#! JupyterViz Package. Then one can freely change -#! the value of to choose among any of the -#! behaviors documented below. +#! wants if using ⪆ from a terminal, for example. You may later +#! assign to another value, but doing so +#! has little purpose from the REPL. You would need to first load the +#! JupyterKernel Package, and even then, all that +#! would be produced by this package would be data structures that would, +#! if evaluated in a Jupyter notebook, produce visualizations. DeclareGlobalVariable( "PlotDisplayMethod" ); MakeReadWriteGlobal( "PlotDisplayMethod" ); diff --git a/lib/main.gi b/lib/main.gi index f09d8a2..ebdd305 100644 --- a/lib/main.gi +++ b/lib/main.gi @@ -25,6 +25,29 @@ else fi; +JUPVIZSetUpJupyterRenderable := function () + if IsBoundGlobal( "JupyterRenderable" ) + and not IsBoundGlobal( "JUPVIZFileContentsType" ) then + BindGlobal( "JUPVIZFileContentsType", + NewType( NewFamily( "JUPVIZFileContentsFamily" ), + JUPVIZIsFileContentsRep ) ); + InstallMethod( JUPVIZFileContents, "for a string", [ IsString ], + function( content ) + return Objectify( ValueGlobal( "JUPVIZFileContentsType" ), + rec( content := content ) ); + end ); + InstallMethod( ValueGlobal( "JupyterRender" ), + [ JUPVIZIsFileContents ], + function ( fileContents ) + return Objectify( ValueGlobal( "JupyterRenderableType" ), + rec( data := rec( text\/plain := fileContents!.content ), + metadata := rec( text\/plain := "" ) ) ); + end ); + fi; +end; +JUPVIZSetUpJupyterRenderable(); + + InstallGlobalFunction( RunJavaScript, function ( script, returnHTML... ) local html, filename, file; if PlotDisplayMethod = PlotDisplayMethod_HTML then @@ -58,6 +81,11 @@ InstallGlobalFunction( RunJavaScript, function ( script, returnHTML... ) fi; return Concatenation( "Displaying result stored in ", filename, "." ); else + # Ensure that we have the global variables we need. + JUPVIZSetUpJupyterRenderable(); + if ( not IsBoundGlobal( "JupyterRenderable" ) ) then + Error( "The JupyterKernel package is required for this feature." ); + fi; # The output element in the notebook will be passed called "element" in # the script's environment, which we capture with the closure wrapper # below, so that any callbacks or asynchronous code can rely on its having @@ -88,24 +116,6 @@ function ( relativeFilename ) end ); -if IsBoundGlobal( "JupyterRenderable" ) then - BindGlobal( "JUPVIZFileContentsType", - NewType( NewFamily( "JUPVIZFileContentsFamily" ), - JUPVIZIsFileContentsRep ) ); - InstallMethod( JUPVIZFileContents, "for a string", [ IsString ], - function( content ) - return Objectify( JUPVIZFileContentsType, - rec( content := content ) ); - end ); - InstallMethod( JupyterRender, [ JUPVIZIsFileContents ], - function ( fileContents ) - return Objectify( JupyterRenderableType, - rec( data := rec( text\/plain := fileContents!.content ), - metadata := rec( text\/plain := "" ) ) ); - end ); -fi; - - InstallValue( JUPVIZLoadedJavaScriptCache, rec( ) ); InstallGlobalFunction( LoadJavaScriptFile, function ( filename ) local absolute, result; diff --git a/tst/low-level-api.tst b/tst/low-level-api.tst index 5a73247..93e83bc 100644 --- a/tst/low-level-api.tst +++ b/tst/low-level-api.tst @@ -7,6 +7,11 @@ gap> START_TEST("JupyterViz package: low-level-api.tst"); # Ensure some basic requirements of the output of functions defined in this # package +# Tell the package we're inside a Jupyter notebook. +gap> LoadPackage( "JupyterKernel", false ); +true +gap> PlotDisplayMethod := PlotDisplayMethod_Jupyter;; + # RunJavaScript function gap> tmp := RunJavaScript( "var x = 5;" ); diff --git a/tst/support-for-repl.tst b/tst/support-for-repl.tst index b34845c..c568022 100644 --- a/tst/support-for-repl.tst +++ b/tst/support-for-repl.tst @@ -7,7 +7,12 @@ gap> START_TEST("JupyterViz package: support-for-repl.tst"); # Ensure some basic requirements of the output of functions defined in this # package -# First verify that the RunJavaScript function, by default, functions the +# Tell the package we're inside a Jupyter notebook. +gap> LoadPackage( "JupyterKernel", false ); +true +gap> PlotDisplayMethod := PlotDisplayMethod_Jupyter;; + +# Then verify that the RunJavaScript function works the # same way that it does in the low-level-api.tst file. gap> tmp := RunJavaScript( "var x = 5;" ); diff --git a/tst/testall.g b/tst/testall.g index cd80c5c..283b6ab 100644 --- a/tst/testall.g +++ b/tst/testall.g @@ -2,12 +2,6 @@ # This file follows the pattern set down in the Example package. # It runs all .tst files in this same directory. -# We must load the Jupyter Kernel first, so that we can test all the -# possible behaviors of the JupyterViz package. If we don't load this -# one, then some tools dependent on it won't be defined when JupyterViz -# loads. -LoadPackage( "jupyterkernel" ); -# OK, now load the package we're actually testing. LoadPackage( "jupyterviz" ); TestDirectory( DirectoriesPackageLibrary( "jupyterviz", "tst" ),