Loading external javascript libraries in jupyter notebooks





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am currently trying to load an external JavaScript library (https://github.com/enkimute/ganja.js) from a jupyter notebook and add an element to the notebook I am working in



Here is gist of a minimal example of my code:
https://gist.github.com/hugohadfield/7c42d6944b154ba8d73f07059964c730



%%javascript
require.config({paths: {ganja: 'https://unpkg.com/ganja.js@1.0.99/ganja'}});

console.log('Test1');

require(['ganja'],
function(){
Algebra(2,0,1,()=>{

// We work in dual space so we define our points to be bivectors. Ganja.js overloads scientific notation to specify basis blades.
// For readability we create a function that converts 2D euclidean coordinates to their 3D bivector representation.
var point = (x,y)=>1e12-x*1e02+y*1e01;

// Similarly, we can define lines directly. The euclidean line ax + by + c can be specified so :
var line = (a,b,c)=>a*1e1+b*1e2+c*1e0;

// Define 3 points.
var A=point(-1,1), B=point(-1,-1), C=point(1,-1);

// Define the line y=x-0.5
var L=line(-1,1,0.5)

// Or by joining two points. We define M as a function so it will update when C or A are dragged.
var M=()=>C&A;

// Points can also be found by intersecting two lines. We similarly define D as a function for interactive updates.
var D=()=>L^M;


console.log('Test2');


// We now use the graph function to create an SVG object that visualises our algebraic elements. The graph function accepts
// an array of items that it will render in order. It can render points, lines, labels, colors, line segments and polygons.
element.append(this.graph([
A, "A", // Render point A and label it.
B, "B", // Render point B and label it.
C, "C", // Render point C and label them.
L, "L", M, "M", // Render and label lines.
D, "D", // Intersection point of L and M
0xff0000, // Set the color to red.
[B,C], // Render line segment from B to C.
0xffcccc, // Set the color to light red.
[A,B,C] // render polygon ABC.
],{grid:true}));

});

});


Nothing displays in the notebook and I get an error code of:
ReferenceError: "Algebra is not defined"



I thought that the require would handle the loading of the library and as such I should be able to use Algebra, which is defined in that library. Why can I not do this, what is the correct form for loading external libraries into jupyter notebooks?










share|improve this question

























  • Please, do not edit the answer into a question, rather post it as an answer.

    – Suma
    Dec 12 '18 at 2:28


















0















I am currently trying to load an external JavaScript library (https://github.com/enkimute/ganja.js) from a jupyter notebook and add an element to the notebook I am working in



Here is gist of a minimal example of my code:
https://gist.github.com/hugohadfield/7c42d6944b154ba8d73f07059964c730



%%javascript
require.config({paths: {ganja: 'https://unpkg.com/ganja.js@1.0.99/ganja'}});

console.log('Test1');

require(['ganja'],
function(){
Algebra(2,0,1,()=>{

// We work in dual space so we define our points to be bivectors. Ganja.js overloads scientific notation to specify basis blades.
// For readability we create a function that converts 2D euclidean coordinates to their 3D bivector representation.
var point = (x,y)=>1e12-x*1e02+y*1e01;

// Similarly, we can define lines directly. The euclidean line ax + by + c can be specified so :
var line = (a,b,c)=>a*1e1+b*1e2+c*1e0;

// Define 3 points.
var A=point(-1,1), B=point(-1,-1), C=point(1,-1);

// Define the line y=x-0.5
var L=line(-1,1,0.5)

// Or by joining two points. We define M as a function so it will update when C or A are dragged.
var M=()=>C&A;

// Points can also be found by intersecting two lines. We similarly define D as a function for interactive updates.
var D=()=>L^M;


console.log('Test2');


// We now use the graph function to create an SVG object that visualises our algebraic elements. The graph function accepts
// an array of items that it will render in order. It can render points, lines, labels, colors, line segments and polygons.
element.append(this.graph([
A, "A", // Render point A and label it.
B, "B", // Render point B and label it.
C, "C", // Render point C and label them.
L, "L", M, "M", // Render and label lines.
D, "D", // Intersection point of L and M
0xff0000, // Set the color to red.
[B,C], // Render line segment from B to C.
0xffcccc, // Set the color to light red.
[A,B,C] // render polygon ABC.
],{grid:true}));

});

});


Nothing displays in the notebook and I get an error code of:
ReferenceError: "Algebra is not defined"



I thought that the require would handle the loading of the library and as such I should be able to use Algebra, which is defined in that library. Why can I not do this, what is the correct form for loading external libraries into jupyter notebooks?










share|improve this question

























  • Please, do not edit the answer into a question, rather post it as an answer.

    – Suma
    Dec 12 '18 at 2:28














0












0








0








I am currently trying to load an external JavaScript library (https://github.com/enkimute/ganja.js) from a jupyter notebook and add an element to the notebook I am working in



Here is gist of a minimal example of my code:
https://gist.github.com/hugohadfield/7c42d6944b154ba8d73f07059964c730



%%javascript
require.config({paths: {ganja: 'https://unpkg.com/ganja.js@1.0.99/ganja'}});

console.log('Test1');

require(['ganja'],
function(){
Algebra(2,0,1,()=>{

// We work in dual space so we define our points to be bivectors. Ganja.js overloads scientific notation to specify basis blades.
// For readability we create a function that converts 2D euclidean coordinates to their 3D bivector representation.
var point = (x,y)=>1e12-x*1e02+y*1e01;

// Similarly, we can define lines directly. The euclidean line ax + by + c can be specified so :
var line = (a,b,c)=>a*1e1+b*1e2+c*1e0;

// Define 3 points.
var A=point(-1,1), B=point(-1,-1), C=point(1,-1);

// Define the line y=x-0.5
var L=line(-1,1,0.5)

// Or by joining two points. We define M as a function so it will update when C or A are dragged.
var M=()=>C&A;

// Points can also be found by intersecting two lines. We similarly define D as a function for interactive updates.
var D=()=>L^M;


console.log('Test2');


// We now use the graph function to create an SVG object that visualises our algebraic elements. The graph function accepts
// an array of items that it will render in order. It can render points, lines, labels, colors, line segments and polygons.
element.append(this.graph([
A, "A", // Render point A and label it.
B, "B", // Render point B and label it.
C, "C", // Render point C and label them.
L, "L", M, "M", // Render and label lines.
D, "D", // Intersection point of L and M
0xff0000, // Set the color to red.
[B,C], // Render line segment from B to C.
0xffcccc, // Set the color to light red.
[A,B,C] // render polygon ABC.
],{grid:true}));

});

});


Nothing displays in the notebook and I get an error code of:
ReferenceError: "Algebra is not defined"



I thought that the require would handle the loading of the library and as such I should be able to use Algebra, which is defined in that library. Why can I not do this, what is the correct form for loading external libraries into jupyter notebooks?










share|improve this question
















I am currently trying to load an external JavaScript library (https://github.com/enkimute/ganja.js) from a jupyter notebook and add an element to the notebook I am working in



Here is gist of a minimal example of my code:
https://gist.github.com/hugohadfield/7c42d6944b154ba8d73f07059964c730



%%javascript
require.config({paths: {ganja: 'https://unpkg.com/ganja.js@1.0.99/ganja'}});

console.log('Test1');

require(['ganja'],
function(){
Algebra(2,0,1,()=>{

// We work in dual space so we define our points to be bivectors. Ganja.js overloads scientific notation to specify basis blades.
// For readability we create a function that converts 2D euclidean coordinates to their 3D bivector representation.
var point = (x,y)=>1e12-x*1e02+y*1e01;

// Similarly, we can define lines directly. The euclidean line ax + by + c can be specified so :
var line = (a,b,c)=>a*1e1+b*1e2+c*1e0;

// Define 3 points.
var A=point(-1,1), B=point(-1,-1), C=point(1,-1);

// Define the line y=x-0.5
var L=line(-1,1,0.5)

// Or by joining two points. We define M as a function so it will update when C or A are dragged.
var M=()=>C&A;

// Points can also be found by intersecting two lines. We similarly define D as a function for interactive updates.
var D=()=>L^M;


console.log('Test2');


// We now use the graph function to create an SVG object that visualises our algebraic elements. The graph function accepts
// an array of items that it will render in order. It can render points, lines, labels, colors, line segments and polygons.
element.append(this.graph([
A, "A", // Render point A and label it.
B, "B", // Render point B and label it.
C, "C", // Render point C and label them.
L, "L", M, "M", // Render and label lines.
D, "D", // Intersection point of L and M
0xff0000, // Set the color to red.
[B,C], // Render line segment from B to C.
0xffcccc, // Set the color to light red.
[A,B,C] // render polygon ABC.
],{grid:true}));

});

});


Nothing displays in the notebook and I get an error code of:
ReferenceError: "Algebra is not defined"



I thought that the require would handle the loading of the library and as such I should be able to use Algebra, which is defined in that library. Why can I not do this, what is the correct form for loading external libraries into jupyter notebooks?







javascript python jupyter-notebook ipython






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 12 '18 at 11:53







Hugo Hadfield

















asked Nov 16 '18 at 16:10









Hugo HadfieldHugo Hadfield

3111




3111













  • Please, do not edit the answer into a question, rather post it as an answer.

    – Suma
    Dec 12 '18 at 2:28



















  • Please, do not edit the answer into a question, rather post it as an answer.

    – Suma
    Dec 12 '18 at 2:28

















Please, do not edit the answer into a question, rather post it as an answer.

– Suma
Dec 12 '18 at 2:28





Please, do not edit the answer into a question, rather post it as an answer.

– Suma
Dec 12 '18 at 2:28












1 Answer
1






active

oldest

votes


















0














Fixed this, it was to do with the names of the objects that the library exports and how the require loads them, the fixed code:



    %%javascript

require.config({paths: {Algebra: 'https://unpkg.com/ganja.js@1.0.99/ganja'}});
require(['Algebra'],function(Algebra){add_graph_to_notebook(Algebra)});

function add_graph_to_notebook(Algebra){
var output = Algebra(2,0,1,()=>{

// We work in dual space so we define our points to be bivectors. Ganja.js overloads scientific notation to specify basis blades.
// For readability we create a function that converts 2D euclidean coordinates to their 3D bivector representation.
var point = (x,y)=>1e12-x*1e02+y*1e01;

// Similarly, we can define lines directly. The euclidean line ax + by + c can be specified so :
var line = (a,b,c)=>a*1e1+b*1e2+c*1e0;

// Define 3 points.
var A=point(-1,1), B=point(-1,-1), C=point(1,-1);

// Define the line y=x-0.5
var L=line(-1,1,0.5)

// Or by joining two points. We define M as a function so it will update when C or A are dragged.
var M=()=>C&A;

// Points can also be found by intersecting two lines. We similarly define D as a function for interactive updates.
var D=()=>L^M;

// We now use the graph function to create an SVG object that visualises our algebraic elements. The graph function accepts
// an array of items that it will render in order. It can render points, lines, labels, colors, line segments and polygons.
return this.graph([
A, 'A', // Render point A and label it.
B, 'B', // Render point B and label it.
C, 'C', // Render point C and label them.
L, 'L', M, 'M', // Render and label lines.
D, 'D', // Intersection point of L and M
0xff0000, // Set the color to red.
[B,C], // Render line segment from B to C.
0xffcccc, // Set the color to light red.
[A,B,C] // render polygon ABC.
],{grid:true});

});

console.log(output);
element.append(output)
}





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341588%2floading-external-javascript-libraries-in-jupyter-notebooks%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Fixed this, it was to do with the names of the objects that the library exports and how the require loads them, the fixed code:



        %%javascript

    require.config({paths: {Algebra: 'https://unpkg.com/ganja.js@1.0.99/ganja'}});
    require(['Algebra'],function(Algebra){add_graph_to_notebook(Algebra)});

    function add_graph_to_notebook(Algebra){
    var output = Algebra(2,0,1,()=>{

    // We work in dual space so we define our points to be bivectors. Ganja.js overloads scientific notation to specify basis blades.
    // For readability we create a function that converts 2D euclidean coordinates to their 3D bivector representation.
    var point = (x,y)=>1e12-x*1e02+y*1e01;

    // Similarly, we can define lines directly. The euclidean line ax + by + c can be specified so :
    var line = (a,b,c)=>a*1e1+b*1e2+c*1e0;

    // Define 3 points.
    var A=point(-1,1), B=point(-1,-1), C=point(1,-1);

    // Define the line y=x-0.5
    var L=line(-1,1,0.5)

    // Or by joining two points. We define M as a function so it will update when C or A are dragged.
    var M=()=>C&A;

    // Points can also be found by intersecting two lines. We similarly define D as a function for interactive updates.
    var D=()=>L^M;

    // We now use the graph function to create an SVG object that visualises our algebraic elements. The graph function accepts
    // an array of items that it will render in order. It can render points, lines, labels, colors, line segments and polygons.
    return this.graph([
    A, 'A', // Render point A and label it.
    B, 'B', // Render point B and label it.
    C, 'C', // Render point C and label them.
    L, 'L', M, 'M', // Render and label lines.
    D, 'D', // Intersection point of L and M
    0xff0000, // Set the color to red.
    [B,C], // Render line segment from B to C.
    0xffcccc, // Set the color to light red.
    [A,B,C] // render polygon ABC.
    ],{grid:true});

    });

    console.log(output);
    element.append(output)
    }





    share|improve this answer




























      0














      Fixed this, it was to do with the names of the objects that the library exports and how the require loads them, the fixed code:



          %%javascript

      require.config({paths: {Algebra: 'https://unpkg.com/ganja.js@1.0.99/ganja'}});
      require(['Algebra'],function(Algebra){add_graph_to_notebook(Algebra)});

      function add_graph_to_notebook(Algebra){
      var output = Algebra(2,0,1,()=>{

      // We work in dual space so we define our points to be bivectors. Ganja.js overloads scientific notation to specify basis blades.
      // For readability we create a function that converts 2D euclidean coordinates to their 3D bivector representation.
      var point = (x,y)=>1e12-x*1e02+y*1e01;

      // Similarly, we can define lines directly. The euclidean line ax + by + c can be specified so :
      var line = (a,b,c)=>a*1e1+b*1e2+c*1e0;

      // Define 3 points.
      var A=point(-1,1), B=point(-1,-1), C=point(1,-1);

      // Define the line y=x-0.5
      var L=line(-1,1,0.5)

      // Or by joining two points. We define M as a function so it will update when C or A are dragged.
      var M=()=>C&A;

      // Points can also be found by intersecting two lines. We similarly define D as a function for interactive updates.
      var D=()=>L^M;

      // We now use the graph function to create an SVG object that visualises our algebraic elements. The graph function accepts
      // an array of items that it will render in order. It can render points, lines, labels, colors, line segments and polygons.
      return this.graph([
      A, 'A', // Render point A and label it.
      B, 'B', // Render point B and label it.
      C, 'C', // Render point C and label them.
      L, 'L', M, 'M', // Render and label lines.
      D, 'D', // Intersection point of L and M
      0xff0000, // Set the color to red.
      [B,C], // Render line segment from B to C.
      0xffcccc, // Set the color to light red.
      [A,B,C] // render polygon ABC.
      ],{grid:true});

      });

      console.log(output);
      element.append(output)
      }





      share|improve this answer


























        0












        0








        0







        Fixed this, it was to do with the names of the objects that the library exports and how the require loads them, the fixed code:



            %%javascript

        require.config({paths: {Algebra: 'https://unpkg.com/ganja.js@1.0.99/ganja'}});
        require(['Algebra'],function(Algebra){add_graph_to_notebook(Algebra)});

        function add_graph_to_notebook(Algebra){
        var output = Algebra(2,0,1,()=>{

        // We work in dual space so we define our points to be bivectors. Ganja.js overloads scientific notation to specify basis blades.
        // For readability we create a function that converts 2D euclidean coordinates to their 3D bivector representation.
        var point = (x,y)=>1e12-x*1e02+y*1e01;

        // Similarly, we can define lines directly. The euclidean line ax + by + c can be specified so :
        var line = (a,b,c)=>a*1e1+b*1e2+c*1e0;

        // Define 3 points.
        var A=point(-1,1), B=point(-1,-1), C=point(1,-1);

        // Define the line y=x-0.5
        var L=line(-1,1,0.5)

        // Or by joining two points. We define M as a function so it will update when C or A are dragged.
        var M=()=>C&A;

        // Points can also be found by intersecting two lines. We similarly define D as a function for interactive updates.
        var D=()=>L^M;

        // We now use the graph function to create an SVG object that visualises our algebraic elements. The graph function accepts
        // an array of items that it will render in order. It can render points, lines, labels, colors, line segments and polygons.
        return this.graph([
        A, 'A', // Render point A and label it.
        B, 'B', // Render point B and label it.
        C, 'C', // Render point C and label them.
        L, 'L', M, 'M', // Render and label lines.
        D, 'D', // Intersection point of L and M
        0xff0000, // Set the color to red.
        [B,C], // Render line segment from B to C.
        0xffcccc, // Set the color to light red.
        [A,B,C] // render polygon ABC.
        ],{grid:true});

        });

        console.log(output);
        element.append(output)
        }





        share|improve this answer













        Fixed this, it was to do with the names of the objects that the library exports and how the require loads them, the fixed code:



            %%javascript

        require.config({paths: {Algebra: 'https://unpkg.com/ganja.js@1.0.99/ganja'}});
        require(['Algebra'],function(Algebra){add_graph_to_notebook(Algebra)});

        function add_graph_to_notebook(Algebra){
        var output = Algebra(2,0,1,()=>{

        // We work in dual space so we define our points to be bivectors. Ganja.js overloads scientific notation to specify basis blades.
        // For readability we create a function that converts 2D euclidean coordinates to their 3D bivector representation.
        var point = (x,y)=>1e12-x*1e02+y*1e01;

        // Similarly, we can define lines directly. The euclidean line ax + by + c can be specified so :
        var line = (a,b,c)=>a*1e1+b*1e2+c*1e0;

        // Define 3 points.
        var A=point(-1,1), B=point(-1,-1), C=point(1,-1);

        // Define the line y=x-0.5
        var L=line(-1,1,0.5)

        // Or by joining two points. We define M as a function so it will update when C or A are dragged.
        var M=()=>C&A;

        // Points can also be found by intersecting two lines. We similarly define D as a function for interactive updates.
        var D=()=>L^M;

        // We now use the graph function to create an SVG object that visualises our algebraic elements. The graph function accepts
        // an array of items that it will render in order. It can render points, lines, labels, colors, line segments and polygons.
        return this.graph([
        A, 'A', // Render point A and label it.
        B, 'B', // Render point B and label it.
        C, 'C', // Render point C and label them.
        L, 'L', M, 'M', // Render and label lines.
        D, 'D', // Intersection point of L and M
        0xff0000, // Set the color to red.
        [B,C], // Render line segment from B to C.
        0xffcccc, // Set the color to light red.
        [A,B,C] // render polygon ABC.
        ],{grid:true});

        });

        console.log(output);
        element.append(output)
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 12 '18 at 11:53









        Hugo HadfieldHugo Hadfield

        3111




        3111
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341588%2floading-external-javascript-libraries-in-jupyter-notebooks%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues