Friday, 7 September 2018

Access data object in Jupyter extension

I've created a javascript extension for my Jupyter notebook that will plot some data for me. Right now I have the data hardcoded within the extension. My question: is there a way to access a data object that exists within the notebook?

For example, below is some sample code for an extension:

define([
    'base/js/namespace'
], function(
    Jupyter
) {
  function test_second_extension() {

    var handler = function () {
      console.log(
          'This is the current notebook application instance:',
          Jupyter.notebook
      );
      var data = [{"x": 1, "y": 5}, {"x": 2, "y":12}, {"x": 3, "y": 27}];
      console.log(data);
    };

    var action = {
        icon: 'fa-comment-o', // a font-awesome class used on buttons, etc
        help    : 'Print notebook instance',
        help_index : 'zz',
        handler : handler
    };
    var prefix = 'test_second_extension';
    var action_name = 'show-alert';

    var full_action_name = Jupyter.actions.register(action, action_name, prefix); // returns 'my_extension:show-alert'
    Jupyter.toolbar.add_buttons_group([full_action_name]);
  }

  return {
    load_ipython_extension: test_second_extension
  };
});

And this is what I have in a Python3 Jupyter cell:

import pandas as pd
data = pd.read_json('[{"x": 1, "y": 5}, {"x": 2, "y":12}, {"x": 3, "y": 27}]')

Is there a way to access the data object that is created in the Jupyter cell from within the extension, instead of hardcoding it?



from Access data object in Jupyter extension

No comments:

Post a Comment