Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
828 changes: 828 additions & 0 deletions demos/dot-and-numericalsgps.ipynb

Large diffs are not rendered by default.

191 changes: 191 additions & 0 deletions demos/tikz-and-intpic.ipynb

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions doc/_Chapter_Jupyter_Kernel.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- This is an automatically generated file. -->
<Chapter Label="Chapter_Jupyter_Kernel">
<Heading>Jupyter Kernel</Heading>

<P/>
A <C>Jupyter Kernel</C> is an object that can handles the Jupyter Protocol.
<P/>
<Section Label="Chapter_Jupyter_Kernel_Section_Functions">
<Heading>Functions</Heading>

<P/>
<ManSection>
<Func Arg="filename" Name="JUPYTER_LogProtocol" />
<Description>
Opens a file that is used to log all jupyter protocol
messages.
</Description>
</ManSection>


<ManSection>
<Func Arg="arg" Name="JUPYTER_UnlogProtocol" />
<Description>
Closes the protocol log.
</Description>
</ManSection>


</Section>


</Chapter>

Empty file added doc/_Chunks.xml
Empty file.
8 changes: 8 additions & 0 deletions doc/_entities.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!ENTITY crypting '<Package>crypting</Package>'>
<!ENTITY json '<Package>json</Package>'>
<!ENTITY uuid '<Package>uuid</Package>'>
<!ENTITY Jupyter '<URL Text="Jupyter">https://jupyter.org</URL>'>
<!ENTITY ZeroMQ '<URL Text="ZeroMQ">https://zeromq.org</URL>'>
<!ENTITY IO '<Package>IO</Package>'>
<!ENTITY ZeroMQInterface '<Package>ZeroMQInterface</Package>'>
<!ENTITY JupyterKernel '<Package>JupyterKernel</Package>'>
1 change: 1 addition & 0 deletions gap/JupyterRenderable.gd
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ DeclareOperation("JupyterRenderable", [IsObject, IsObject] );
#! Method that provides rich viewing experience if
#! code used inside Jupyter
DeclareOperation("JupyterRender", [IsObject]);
DeclareOperation("JupyterRender", [IsRecord]);

#! @Description
#! Accessor for data in a JupyterRenderable
Expand Down
5 changes: 5 additions & 0 deletions gap/JupyterRenderable.gi
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ function(data, metadata)
return Objectify( JupyterRenderableType
, rec( data := data, metadata := metadata ) );
end);

InstallMethod( JupyterRender, "for a record", [ IsRecord ],
r -> Objectify( JupyterRenderableType
, rec( data := rec( text\/plain := String(r) )
, metadata := rec() ) ) );
27 changes: 26 additions & 1 deletion gap/JupyterUtil.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#! @Chapter Jupyter Utility Functions
#! @Section Functions
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more descritptive title for the section

#!
#! Several utility functions for GAP Jupyter Kernel
#!
#! @Section Print and complete functions
#!
#! @Description
#! Jupyter printing
DeclareGlobalFunction("JUPYTER_print");
Expand Down Expand Up @@ -28,3 +32,24 @@ DeclareGlobalFunction("JUPYTER_Inspect");
#! Don't trust this function.
DeclareGlobalFunction("ISO8601Stamp");

#! @Description
#! @Arguments dot
#! The input is a dot (grpahviz) string.
#!
#! The output is the graph corresponding to the dot string.
#!
#! Examples can be found in the numericalsgps notebook in the demo folder
#!
#! Prerrequisites: dot must be installed in the system.
DeclareGlobalFunction("JupyterSplashDot");

#! @Description
#! @Arguments tikz
#! The input is a string containing a tikzfigure.
#!
#! The output is the figure corresponding to that tikzfigure.
#!
#! Examples can be found in the inpic notebook in the demo folder
#!
#! Prerrequisites: pdflatex, pdfinfo, pdftoppm, and base64 must be installed in the system.
DeclareGlobalFunction("JupyterSplashTikZ");
77 changes: 45 additions & 32 deletions gap/JupyterUtil.gi
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
InstallMethod( JupyterRender, [ IsRecord ],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was moved to JupyterRenderable.gi
It makes more sense to be there

r -> Objectify( JupyterRenderableType
, rec( data := rec( text\/plain := String(r) )
, metadata := rec() ) ) );

# This is still an ugly hack, but its already much better than before!
BindGlobal("JupyterSplashDot",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used InstallGlobalFunction + declare to add documentation

InstallGlobalFunction("JupyterSplashDot",
function(dot)
local fn, fd, r;

Expand Down Expand Up @@ -43,13 +38,13 @@ function(group)
end);

# To show TikZ in a GAP jupyter notebook
BindGlobal("JupyterSplashTikZ",
InstallGlobalFunction("JupyterSplashTikZ",
function(tikz)
local tmpdir, fn, header, ltx, svgfile, stream, svgdata, tojupyter, hasbp, img, b64file;
local tmpdir, fn, header, ltx, pngfile, stream, pngdata, tojupyter, hasbp, img, b64file, b64cmd, dims, dimsfile, pdffile, dimx;

hasbp:=PositionSublist(tikz,"begin{tikzpicture}")<>fail;
hasbp:=PositionSublist(tikz,"begin[border=2pt]{tikzpicture}")<>fail;

header:=Concatenation( "\\documentclass[crop,tikz]{standalone}\n",
header:=Concatenation( "\\documentclass[crop,tikz,border=2pt]{standalone}\n",
"\\usepackage{pgfplots}",
"\\makeatletter\n",
"\\batchmode\n",
Expand All @@ -75,40 +70,58 @@ function(tikz)
Concatenation( fn, ".tex" ), " > ", Concatenation( fn, ".log2" ) );
Exec( ltx );

if not( IsExistingFile( Concatenation(fn, ".pdf") ) ) then
pdffile:=Concatenation(fn, ".pdf");
if not( IsExistingFile( pdffile ) ) then
tojupyter := rec( json := true, name := "stdout",
data := "No pdf was created; pdflatex is installed in your system?",metadata:=rec() );
Info(InfoWarning,1,"No pdf was created; pdflatex is installed in your system?");
return JupyterRenderable(tojupyter.data, tojupyter.metadata);
fi;

svgfile := Concatenation( fn, ".svg" );
b64file := Concatenation( fn, ".b64" );
if ARCH_IS_MAC_OS_X() then
ltx := Concatenation( "pdf2svg ", Concatenation( fn, ".pdf" ), " ",
svgfile, "; base64 -i ", svgfile," >> ", b64file );

else
ltx := Concatenation( "pdf2svg ", Concatenation( fn, ".pdf" ), " ",
svgfile, "; base64 ", svgfile," >> ", b64file );
fi;
Exec( ltx );
if not( IsExistingFile( svgfile ) ) then
dimsfile:=Concatenation(fn, "-dims.txt");
ltx:=Concatenation("pdfinfo ",pdffile," | grep \"Page size\" > ",dimsfile);
Exec(ltx);
if not( IsExistingFile( dimsfile ) ) then
tojupyter := rec( json := true, name := "stdout",
data := "No svg was created; pdf2svg is installed in your system?", metadata := rec());
data := "pdfinfo missing in your system",metadata:=rec() );
Info(InfoWarning,1,"No pdf was created; pdflatex is installed in your system?");
return JupyterRenderable(tojupyter.data, tojupyter.metadata);
fi;
stream := InputTextFile( b64file );
if stream <> fail then
svgdata := ReadAll( stream );
CloseStream( stream );
else

stream := InputTextFile( dimsfile );
dims:= ReadAll( stream );
NormalizeWhitespace(dims);
CloseStream( stream );
dimx:=Float(NormalizedWhitespace(dims{[PositionSublist(dims,": ")+2..PositionSublist(dims," x")]}));

pngfile := Concatenation( fn, ".png" );
ltx := Concatenation( "pdftoppm -r 300 -png ", pdffile, " > ", pngfile);
Exec( ltx );

if not( IsExistingFile( pngfile ) ) then
tojupyter := rec( json := true, name := "stdout",
data := Concatenation( "Unable to render ", tikz ), metadata := rec() );
data := "No png was created; pdftoppm is installed in your system?",metadata:=rec() );
Info(InfoWarning,1,"No png was created; are convert and pdftoppm installed in your system?");
return JupyterRenderable(tojupyter.data, tojupyter.metadata);
fi;

img:=Concatenation("<img src='data:image/svg+xml;base64,", svgdata,"'>");
return Objectify( JupyterRenderableType, rec( data := rec( ("text/html") := img), metadata:=rec() ));
b64file := Concatenation( fn, ".b64" );
if ARCH_IS_MAC_OS_X() then
b64cmd:="base64 -i ";
else
b64cmd:="base64 ";
fi;

ltx := Concatenation( b64cmd, pngfile," > ", b64file );
Exec( ltx );
stream := InputTextFile( b64file );
pngdata:= ReadAll( stream );
CloseStream( stream );


img:=Concatenation("\n <img src='data:image/png;base64,",pngdata,"' style=\"width:",String(dimx),"px;\" >");

return Objectify( JupyterRenderableType, rec( data := rec( ("text/html") := img), metadata:=rec( ) ));
end);

# This is really not what I should be doing here...
Expand Down