Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

workaround for svg jupyter issue #135

Merged
merged 5 commits into from
Aug 29, 2024
Merged
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
53 changes: 30 additions & 23 deletions gap/JupyterUtil.gi
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ end);
# To show TikZ in a GAP jupyter notebook
BindGlobal("JupyterSplashTikZ",
function(tikz)
local tmpdir, fn, header, ltx, svgfile, stream, svgdata, tojupyter,hasbp;
local tmpdir, fn, header, ltx, svgfile, stream, svgdata, tojupyter, hasbp, img, b64file;

hasbp:=PositionSublist(tikz,"begin{tikzpicture}")<>fail;

Expand All @@ -64,6 +64,7 @@ function(tikz)
else
header:=Concatenation(header,"\\end{tikzpicture}\n\\end{document}");
fi;

tmpdir := DirectoryTemporary();
fn := Filename( tmpdir, "svg_get" );

Expand All @@ -76,32 +77,38 @@ function(tikz)

if not( IsExistingFile( Concatenation(fn, ".pdf") ) ) then
tojupyter := rec( json := true, name := "stdout",
data := "No pdf was created; pdflatex is installed in your system?" );
else
svgfile := Concatenation( fn, ".svg" );
data := "No pdf was created; pdflatex is installed in your system?",metadata:=rec() );
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, " >> ", Concatenation( fn, ".log2" ) );
Exec( ltx );
svgfile, "; base64 -i ", svgfile," >> ", b64file );

if not( IsExistingFile( svgfile ) ) then
tojupyter := rec( json := true, name := "stdout",
data := "No svg was created; pdf2svg is installed in your system?" );
else
stream := InputTextFile( svgfile );
if stream <> fail then
svgdata := ReadAll( stream );
tojupyter := rec( json := true, source := "gap",
data := rec( ( "image/svg+xml" ) := svgdata ),
metadata := rec( ( "image/svg+xml" ) := rec( width := "100%", height := "100%" ) ) );
CloseStream( stream );
else
tojupyter := rec( json := true, name := "stdout",
data := Concatenation( "Unable to render ", tikz ), metadata := rec() );
fi;
fi;
else
ltx := Concatenation( "pdf2svg ", Concatenation( fn, ".pdf" ), " ",
svgfile, "; base64 ", svgfile," >> ", b64file );
fi;
Exec( ltx );
if not( IsExistingFile( svgfile ) ) then
tojupyter := rec( json := true, name := "stdout",
data := "No svg was created; pdf2svg is installed in your system?", metadata := rec());
return JupyterRenderable(tojupyter.data, tojupyter.metadata);
fi;
stream := InputTextFile( b64file );
if stream <> fail then
svgdata := ReadAll( stream );
CloseStream( stream );
else
tojupyter := rec( json := true, name := "stdout",
data := Concatenation( "Unable to render ", tikz ), metadata := rec() );
return JupyterRenderable(tojupyter.data, tojupyter.metadata);
fi;

return JupyterRenderable(tojupyter.data, tojupyter.metadata);
img:=Concatenation("<img src='data:image/svg+xml;base64,", svgdata,"'>");
return Objectify( JupyterRenderableType, rec( data := rec( ("text/html") := img), metadata:=rec() ));
end);

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