Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 693
R.D. Avatar asked Aug 21 '18 20:08

R.D.


People also ask

What does %% do in Jupyter Notebook?

Both ! and % allow you to run shell commands from a Jupyter notebook. % is provided by the IPython kernel and allows you to run "magic commands", many of which include well-known shell commands. ! , provided by Jupyter, allows shell commands to be run within cells.

How do you use Jupyter extensions?

After installing, open Jupyter Notebook. You will see a new tab, “Nbextensions” will appear in the menu (as shown in the image). When you click it, you will see a list of available extensions that you can use to increase your productivity of Jupyter Notebook.

What is the extension of the Python Jupyter Notebook file?

Jupyter notebooks have the file extension “. ipynb”.


1 Answers

A bit janky but try this

from IPython.core.display import Javascript
import json

#list of dicts
list_of_dicts = [{"x": 1, "y": 5}, {"x": 2, "y":12}, {"x": 3, "y": 27}]

#convert to json string
json_list = json.dumps(list_of_dicts)

#run in cell
#This will run in the console check your browser
Javascript("""
var json_list = {};
window.variable = json_list;
console.log(json_list);""".format(json_list))

#or as a function
#replace console_log with what you need
def run_in_console(data):
    json_list = json.dumps(data)
    return Javascript("""
        var json_list = {};
        window.variable = json_list;
        console.log(json_list);""".format(json_list))
like image 184
fcsr Avatar answered Oct 01 '22 12:10

fcsr