Skip to content

Commit

Permalink
Merge pull request #177 from nteract/availableKernels
Browse files Browse the repository at this point in the history
caching available kernels into config
  • Loading branch information
rgbkrk committed Feb 25, 2016
2 parents d47e37d + f3a720c commit 3ba76d7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 34 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: '{}'
44 changes: 33 additions & 11 deletions lib/kernel-manager.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,36 @@ Kernel = require './kernel'

module.exports = KernelManager =
runningKernels: {}

getAvailableKernels: _.memoize ->
kernelsUpdatedOnce: false

getAvailableKernels: ->
kernels = _.pluck @getConfigJson('kernelspec', {kernelspecs:{}}).kernelspecs, 'spec'
@updateKernels() unless @kernelsUpdatedOnce
kernels

updateKernels: ->
saveKernelsToConfig = (out) =>
try
out = child_process.spawnSync('jupyter',['kernelspec','list', '--json']).stdout.toString()
catch
out = child_process.spawnSync('ipython',['kernelspec','list', '--json']).stdout.toString()
_.pluck(JSON.parse(out).kernelspecs, 'spec')

kernelspec = JSON.parse(out)
catch e
unless @getAvailableKernels().length
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
"""
if kernelspec?
@setConfigJson 'kernelspec', kernelspec
atom.notifications.addInfo 'Hydrogen Kernels updated:',
detail: (_.pluck @getAvailableKernels(), 'display_name').join('\n')

@kernelsUpdatedOnce = true
child_process.exec 'jupyter kernelspec list --json --log-level=CRITICAL', (e, stdout, stderr) ->
return saveKernelsToConfig stdout unless e
child_process.exec 'ipython kernelspec list --json --log-level=CRITICAL', (e, stdout, stderr) ->
saveKernelsToConfig stdout

getRunningKernels: ->
return _.clone(@runningKernels)

Expand All @@ -26,12 +48,12 @@ module.exports = KernelManager =
else
return language

getConfigJson: (key) ->
return {} unless value = atom.config.get "Hydrogen.#{key}"
getConfigJson: (key, _default = {}) ->
return _default unless value = atom.config.get "Hydrogen.#{key}"
try
return JSON.parse value
return JSON.parse value
catch error
atom.notifications.addError "Your Hydrogen config is broken: #{key}", detail: error
atom.notifications.addError "Your Hydrogen config is broken: #{key}", detail: error

setConfigJson: (key, value, merge=false) ->
value = _.merge @getConfigJson(key), value if merge
Expand Down
25 changes: 2 additions & 23 deletions lib/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +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: '{}'
config: require './config'


subscriptions: null
Expand All @@ -57,6 +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()

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

0 comments on commit 3ba76d7

Please sign in to comment.