jupyter / atom-notebook

[Deprecated] Jupyter Notebook, but inside Atom.
MIT License
306 stars 48 forks source link

Wrong kernel started #40

Open galou opened 8 years ago

galou commented 8 years ago

I have a problem opening an .ipynb file.

In the ipynb file:

 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },

"jupyter kernelspec list" gives:

  freecad_kernel    /home/gael/.local/share/jupyter/kernels/freecad_kernel
  python2           /home/gael/.local/share/jupyter/kernels/python2
  python3           /home/gael/.local/share/jupyter/kernels/python3

When I open the ipynb file, a FreeCAD window appears. This is the beginning of the log

NotebookEditor created for /home/gael/11-projects/2015-taros_demo/experiments/notebooks/test.ipynb
notebook-editor.js:258 kernelGateway.stderr [KernelGatewayApp] The Jupyter Kernel Gateway is running at: http://localhost:8892

notebook-editor.js:262 Kernel:  freecad_kernel
notebook-editor.js:258 kernelGateway.stderr INFO:tornado.access:200 GET /api/kernelspecs?1458219661160 (127.0.0.1) 184.27ms
...

In the definition of freecad_kernel (kernel.json), I have "language": "python".

The notebook was saved within the web browser launched from jupyter-notebook.

I would love to see atom-notebook running. It looks great and I don't like editing in the browser. Thanks for providing it! Thank you helping me solving this issue!

gnestor commented 8 years ago

Hi @galou. This is peculiar indeed. I would take a look at this line: https://github.com/jupyter/atom-notebook/blob/master/lib/notebook-editor.js#L261. In Atom, you can open the dev tools and set a breakpoint at this point in the execution (when opening a notebook file) and inspect the value of spec and kernelSpecs.kernelspecs to try to understand what it's spawning freecad kernel vs. python.

galou commented 8 years ago

I tried to modify notebook-editor.js without success. When I load the module as development module I receive the error Cannot find module 'pathwatcher'. What I did was

cd $rep_path
git clone https://github.com/jupyter/atom-notebook.git jupyter-notebook
cd ~/.atom/dev/packages
ln -s $rep_path/jupyter-notebook
atom -d

So far, my modifications:

diff --git a/lib/notebook-editor.js b/lib/notebook-editor.js
index d3bfe38..d576e45 100644
--- a/lib/notebook-editor.js
+++ b/lib/notebook-editor.js
@@ -244,6 +244,7 @@ export default class NotebookEditor {
     }

     launchKernelGateway() {
+      let name = this.state.getIn(['metadata', 'kernelspec', 'name']);
       let language = this.state.getIn(['metadata', 'kernelspec', 'language']);
       portfinder.basePort = 8888;
       portfinder.getPort({host: 'localhost'}, (err, port) => {
@@ -258,17 +259,34 @@ export default class NotebookEditor {
           console.log('kernelGateway.stderr ' + data);
           if (data.toString().includes('The Jupyter Kernel Gateway is running at')) {
             getKernelSpecs({baseUrl: `http://localhost:${port}`}).then((kernelSpecs) => {
-              let spec = Object.keys(kernelSpecs.kernelspecs).find(kernel => kernelSpecs.kernelspecs[kernel].spec.language === language);
-              console.log('Kernel: ', spec);
-              if (spec) {
+              let kernel_name = Object.keys(kernelSpecs.kernelspecs).find(kernel => kernelSpecs.kernelspecs[kernel].name === name);
+              let spec;
+              if (kernel_name) {
+                spec = kernelSpecs.kernelspecs[kernel_name].spec;
+              }
+              console.log('Kernel (found by name): ', kernel_name);
+              if (spec && spec.language === language) {
                 startNewKernel({
                   baseUrl: `http://localhost:${port}`,
                   wsUrl: `ws://localhost:${port}`,
-                  name: spec
+                  name: kernel_name
                 }).then((kernel) => {
                   this.session = kernel;
                 });
               }
+              else {
+                let spec = Object.keys(kernelSpecs.kernelspecs).find(kernel => kernelSpecs.kernelspecs[kernel].spec.language === language);
+                console.log('Kernel (found by language): ', spec);
+                if (spec) {
+                  startNewKernel({
+                    baseUrl: `http://localhost:${port}`,
+                    wsUrl: `ws://localhost:${port}`,
+                    name: spec
+                  }).then((kernel) => {
+                    this.session = kernel;
+                  });
+                }
+              }
             });
           }
         });

This is my first experience with Javascript and plugin development with Atom. I'm stuck right now. I tried to first load a kernel by name (while checking the language) and then, if unsuccessful, by language.

galou commented 7 years ago

Could someone help me further with this improvement? Thanks!

gnestor commented 7 years ago

@galou Thanks for taking a stab at this.

First, you will want to follow the dev install instructions (that's why you are seeing Cannot find module 'pathwatcher').

After that, you should be able to test you changes within Atom (be sure to reload Atom). You can always switch back to Atom's installed version of atom-notebook by running apm unlink in your atom-notebook directory.