Headless server-side geogebra (nodejs)

Josep Mulet shared this question 2 years ago
Answered

I am exploring the possibility of using geogebra in nodejs, that is to say without the need of x11 nor a browser window. The main purpose is to generate images from constructions completely build dynamically with javascript. If you have ever used gnuplot, the system would operate similarly to a set terminal svg. Thus, all geogebra commands passed would be used to generate an image.

If I am not wrong, both geogebra desktop and web require a graphical device to operate. So the beforementioned goal seems impossible.

That is way I am developing a nodejs API that runs a chrome-headless browser in which I run the geogebra web app (actually a local copy that I download). This a basic sample of the idea:


const puppeteer = require('puppeteer');
const path = require("path");
const fs = require("fs");
const dir = path.resolve("./geogebra-math-apps-bundle/GeoGebra/HTML5/5.0/simple.html");
// Sample script
const ggbScript = ["f(x)=cos(x)*sin(x)*exp(-x)",  "A=(0,0)", "Tangent(A,f)"]; 

(async () => {
  const browser = await puppeteer.launch(); 
  const page = await browser.newPage();
  await page.goto('file://'+dir);
  await page.waitForFunction('window.ggbApplet != null');
  let svg = await page.evaluate(async (script) => {
            window.ggbApplet.evalCommand(script);
            const svg = window.ggbApplet.exportSVG();            
            return svg;
  }, ggbScript.join("\n"));
  fs.writeFileSync("ggb.svg", svg);
  console.log("Resulting image:", svg)
  await page.close();
  await browser.close();
})();

This code generates a svg image from the commands written in ggbScript. The generated image is ggb.svg

The strange proportions of the image are caused by the template simple.html. This page creates a ggbApplet using deployggb.js. I have tried all parameter combinations but I have been unable to hide the left algebra panel. Even with "showAlgebraInput=false" it shows a blank left panel with a divider. Is there any trick to get rid of this panel?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <meta name=viewport content="width=device-width,initial-scale=1">
    <script src="../../deployggb.js"></script>
    <title>Simple - GeoGebra</title>	
</head>
<body>
    <div id="ggb-element"></div>  
    
    <script type="text/javascript">
        var opts = {"app": false,
                    "appName": "classic", //"classic graphing, geometry, 3d"
                    "width": 800,
                    "height": 800, 
                    "showToolBar":false,
                    "showMenuBar":false,
                    "showAlgebraInput":false,
                    "showResetIcon":false,
                    "enableLabelDrags":false,
                    "enableShiftDragZoom":true,
                    "enableRightClick":false,
                    "showToolBarHelp":false,
                    "errorDialogsActive":true,
                    "useBrowserForJS":true,
                    "showAppsPicker": false
                 };
        /* is3D=is 3D applet using 3D view, AV=Algebra View, SV=Spreadsheet View, CV=CAS View, EV2=Graphics View 2, CP=Construction Protocol, PC=Probability Calculator, DA=Data Analysis, FI=Function Inspector, PV=Python, macro=Macro View */
                 
        var views = {"is3D":false,"AV":false,"SV":true,"CV":false,"EV2":true,"CP":false,"PC":false,"DA":false,"FI":false,"PV":false,"macro":false};                 
        var ggbApp = new GGBApplet(opts, false);      
        ggbApp.setHTML5Codebase("./web")
        window.addEventListener("load", function() { 
            ggbApp.inject('ggb-element');            
        });
    </script>
</body>
</html>

Comments (2)

photo
1

Yes, specify a .ggb file with the filename or ggbBase64 parameter with the Algebra View closed. You can also try the SetPerspective() command

photo
1

Hi Michael,

After multiple attempts I have discovered that one possibility is to set the option app=true and SetPerspective="G"

or

app=false and you need to set a ggbBase64 string

Unfortunately, it does not support the combination app=false and SetPerspective="G" without specifying a GGB construction.

Meanwhile, I have created a github repository https://github.com/jmulet/n... on how to use Geogebra classic app from nodejs.

Cheers,

Josep

© 2020 International GeoGebra Institute