Skip to content

Commit

Permalink
Async kernels update
Browse files Browse the repository at this point in the history
  • Loading branch information
slavaGanzin committed Feb 25, 2016
1 parent d595537 commit 152e42e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 62 deletions.
59 changes: 59 additions & 0 deletions lib/config.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module.exports =
languageMappings:
title: "Language Mappings"
description: 'Some packages may change the name of the grammar for
a language (e.g. "Python" -> "Python Django"). That
leaves Hydrogen unable to figure out what kernel to
use for your code.
This field should be valid JSON mapping a nonstandard
language name to a standard one, e.g.
```{"Python Django": "python", "Ruby (Rails)": "ruby"}```'
type: 'string'
default: '{}'
grammarToKernel:
description: 'JSON mappings between specific kernel and a
language/grammar. This value is updated automatically
by the "switch kernel" command. If you switch from
python2 to python3, python3 will be used the next time
you open a Python file. You probably shouldn\'t
change this by hand.'
type: 'string'
default: '{}'
kernelspec:
title: "Kernel Spec"
description: 'This field is autogenerated on every launch in up-to-date environments,
also you can regenerate this with hydrogen:update-kernels command.
```
Kernel specs as reported by:
$ jupyter kernelspecs list --json
or
$ ipython kernelspecs list --json
```
If this commands fails in your environment you can use this field to specify your kernels, like:
```
{
"kernelspecs": {
"ijavascript": {
"spec": {
"display_name": "IJavascript",
"env": {},
"argv": [
"node",
"/home/user/node_modules/ijavascript/lib/kernel.js",
"--protocol=5.0",
"{connection_file}"
],
"language": "javascript"
},
"resources_dir": "/home/user/node_modules/ijavascript/images"
}
}
}
```'
type: 'string'
default: '{}'
30 changes: 18 additions & 12 deletions lib/kernel-manager.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,35 @@ child_process = require 'child_process'
Kernel = require './kernel'

module.exports = KernelManager =
availableKernels: []
runningKernels: {}

kernelsUpdatedOnce: false

getAvailableKernels: ->
@availableKernels = @getConfigJson 'kernelspecs', @availableKernels
@updateKernels() unless @availableKernels.length
@availableKernels
kernels = _.pluck @getConfigJson('kernelspec', []).kernelspecs, 'spec'
@updateKernels() unless @kernelsUpdatedOnce
kernels

updateKernels: ->
@kernelsUpdatedOnce = true
save = (out) =>
try
kernelspec = JSON.parse(out).kernelspecs
kernelspec = JSON.parse(out)
catch e
atom.notifications.addError "Can't parse neither 'ipython kernelspecs nor 'jupyter kernelspecs'"
unless @getAvailableKernels().length
return atom.notifications.addError """
Can't parse neither 'ipython kernelspecs nor 'jupyter kernelspecs'
""", detail: """Use kernelspec option in Hydrogen options OR update
your ipython/jupyter to version that supports kernelspec option:
$ jupyter kernelspec list --json || ipython kernelspec list --json
"""

@availableKernels = _.pluck kernelspec, 'spec'
@setConfigJson 'kernelspecs', @availableKernels
@setConfigJson 'kernelspec', kernelspec
atom.notifications.addInfo 'Hydrogen Kernels updated:',
detail: (_.pluck @availableKernels, 'display_name').join('\n')
detail: (_.pluck @getAvailableKernels(), 'display_name').join('\n')

child_process.exec 'jupyter kernelspec list --json', (e, stdout, stderr) ->
child_process.exec 'jupter kernelspec list --json', (e, stdout, stderr) ->
return save stdout unless e
child_process.exec 'ipython kernelspec list --json', (e, stdout, stderr) ->
child_process.exec 'iython kernelspec list --json', (e, stdout, stderr) ->
save stdout

getRunningKernels: ->
Expand Down
52 changes: 2 additions & 50 deletions lib/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,7 @@ WatchLanguagePicker = require './watch-language-picker'
AutocompleteProvider = require './autocomplete-provider'

module.exports = Hydrogen =
config:
languageMappings:
title: "Language Mappings"
description: 'Some packages may change the name of the grammar for
a language (e.g. "Python" -> "Python Django"). That
leaves Hydrogen unable to figure out what kernel to
use for your code.
This field should be valid JSON mapping a nonstandard
language name to a standard one, e.g.
{"Python Django": "python", "Ruby (Rails)": "ruby"}'
type: 'string'
default: '{}'
grammarToKernel:
description: 'JSON mappings between specific kernel and a
language/grammar. This value is updated automatically
by the "switch kernel" command. If you switch from
python2 to python3, python3 will be used the next time
you open a Python file. You probably shouldn\'t
change this by hand.'
type: 'string'
default: '{}'
kernelspecs:
title: "Kernel Specs"
description: 'Kernel specs as reported by:
$ jupyter kernelspecs list --json
E.g.:
{
"ijavascript": {
"spec": {
"display_name": "IJavascript",
"env": {},
"argv": [
"node",
"/home/user/node_modules/ijavascript/lib/kernel.js",
"--protocol=5.0",
"{connection_file}"
],
"language": "javascript"
},
"resources_dir": "/home/user/node_modules/ijavascript/images"
}
}'
type: 'string'
default: '{}'
config: require './config'


subscriptions: null
Expand All @@ -83,7 +35,7 @@ module.exports = Hydrogen =
'hydrogen:select-watch-kernel': => @showWatchLanguagePicker()
'hydrogen:add-watch': => @watchSidebar.addWatchFromEditor()
'hydrogen:remove-watch': => @watchSidebar.removeWatch()
'hydrogen:update-kernels': -> KernelManager.updateKernels true
'hydrogen:update-kernels': -> KernelManager.updateKernels()

@subscriptions.add atom.commands.add 'atom-workspace',
'hydrogen:clear-results': => @clearResultBubbles()
Expand Down

0 comments on commit 152e42e

Please sign in to comment.