Skip to content

Commit

Permalink
Upgrading to permit changing plot method at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
nathancarter committed Feb 26, 2019
1 parent 688da4b commit dd55d50
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 38 deletions.
20 changes: 7 additions & 13 deletions lib/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -284,20 +284,14 @@ DeclareGlobalVariable( "ConvertGraphForTool" );
#! <P/>
#! If the <Package>JupyterViz</Package> Package is loaded without the
#! <Package>JupyterKernel</Package> Package already loaded, then it will
#! omit support for Jupyter, and will initialize
#! <Ref Var="PlotDisplayMethod"/> to
#! initialize <Ref Var="PlotDisplayMethod"/> to
#! <Ref Var="PlotDisplayMethod_HTML"/>, which is what the user probably
#! wants if using &GAP; from a terminal, for example. In that case, this
#! package does not support later reassignment of
#! <Ref Var="PlotDisplayMethod"/> to any other value, because Jupyter
#! support was not set up at package loading time.
#! <P/>
#! Thus if the user desires support for both Jupyter-based and
#! HTML-based plotting, he or she should load the
#! <Package>JupyterKernel</Package> Package before the
#! <Package>JupyterViz</Package> Package. Then one can freely change
#! the value of <Ref Var="PlotDisplayMethod"/> to choose among any of the
#! behaviors documented below.
#! wants if using &GAP; from a terminal, for example. You may later
#! assign <Ref Var="PlotDisplayMethod"/> to another value, but doing so
#! has little purpose from the REPL. You would need to first load the
#! <Package>JupyterKernel</Package> 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" );

Expand Down
46 changes: 28 additions & 18 deletions lib/main.gi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions tst/low-level-api.tst
Original file line number Diff line number Diff line change
Expand Up @@ -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;" );
<jupyter renderable>
Expand Down
7 changes: 6 additions & 1 deletion tst/support-for-repl.tst
Original file line number Diff line number Diff line change
Expand Up @@ -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;" );
<jupyter renderable>
Expand Down
6 changes: 0 additions & 6 deletions tst/testall.g
Original file line number Diff line number Diff line change
Expand Up @@ -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" ),
Expand Down

0 comments on commit dd55d50

Please sign in to comment.