diff --git a/create_JupyterLab_kernel.sh b/create_JupyterLab_kernel.sh index 4961eaa4f5aa4cedd0d2364aed4382a052e3c035..a503ff0deb35b0d604fd5884c52f4cd57c65fa0a 100644 --- a/create_JupyterLab_kernel.sh +++ b/create_JupyterLab_kernel.sh @@ -56,7 +56,7 @@ cat <<EOF > $KERNEL_PATH/$KERNEL_NAME/kernel.json EOF # ... and add EBRAINS logo to kernel -cp $LAB_KERNEL_ROOT/../etc/logo-64x64-python.png $KERNEL_PATH/$KERNEL_NAME/logo-64x64.png +cp $(dirname ${BASH_SOURCE[0]})/etc/logo-64x64-python.png $KERNEL_PATH/$KERNEL_NAME/logo-64x64.png # (5) create startup script for R kernel cat $KERNEL_PATH/bin/env.sh > $KERNEL_PATH/bin/env_r.sh @@ -75,5 +75,5 @@ cat <<EOF > $KERNEL_PATH/${KERNEL_NAME}-R/kernel.json EOF # ... and add EBRAINS logo and kernel.js to kernel -cp $LAB_KERNEL_ROOT/../etc/logo-64x64-R.png $KERNEL_PATH/${KERNEL_NAME}-R/logo-64x64.png -cp $LAB_KERNEL_ROOT/../etc/kernel.js $KERNEL_PATH/${KERNEL_NAME}-R/ +cp $(dirname ${BASH_SOURCE[0]})/etc/logo-64x64-R.png $KERNEL_PATH/${KERNEL_NAME}-R/logo-64x64.png +cp $(dirname ${BASH_SOURCE[0]})/etc/kernel.js $KERNEL_PATH/${KERNEL_NAME}-R/ diff --git a/etc/kernel.js b/etc/kernel.js new file mode 100644 index 0000000000000000000000000000000000000000..076b58ab26c2d37bcdb4ff85868dc47b81e05bc1 --- /dev/null +++ b/etc/kernel.js @@ -0,0 +1,77 @@ +const cmd_key = /Mac/.test(navigator.platform) ? 'Cmd' : 'Ctrl'const edit_actions = [ + { + name: 'R Assign', + shortcut: 'Alt--', + icon: 'fa-long-arrow-left', + help: 'R: Inserts the left-assign operator (<-)', + handler(cm) { + cm.replaceSelection(' <- ') + }, + }, + { + name: 'R Pipe', + shortcut: `Shift-${cmd_key}-M`, + icon: 'fa-angle-right', + help: 'R: Inserts the magrittr pipe operator (%>%)', + handler(cm) { + cm.replaceSelection(' %>% ') + }, + }, + { + name: 'R Help', + shortcut: 'F1', + icon: 'fa-book', + help: 'R: Shows the manpage for the item under the cursor', + handler(cm, cell) { + const {anchor, head} = cm.findWordAt(cm.getCursor()) + const word = cm.getRange(anchor, head) + + const callbacks = cell.get_callbacks() + const options = {silent: false, store_history: false, stop_on_error: true} + cell.last_msg_id = cell.notebook.kernel.execute(`help(\`${word}\`)`, callbacks, options) + }, + }, +] + +const prefix = 'irkernel' + +function add_edit_shortcut(notebook, actions, keyboard_manager, edit_action) { + const {name, shortcut, icon, help, handler} = edit_action + + const action = { + icon, help, + help_index : 'zz', + handler: () => { + const cell = notebook.get_selected_cell() + handler(cell.code_mirror, cell) + }, + } + + const full_name = actions.register(action, name, prefix) + + Jupyter.keyboard_manager.edit_shortcuts.add_shortcut(shortcut, full_name) +} + +function render_math(pager, html) { + if (!html) return + const $container = pager.pager_element.find('#pager-container') + $container.find('p[style="text-align: center;"]').map((i, e) => + e.outerHTML = `\\[${e.querySelector('i').innerHTML}\\]`) + $container.find('i').map((i, e) => + e.outerHTML = `\\(${e.innerHTML}\\)`) + MathJax.Hub.Queue(['Typeset', MathJax.Hub, $container[0]]) +} + +define(['base/js/namespace'], ({ + notebook, + actions, + keyboard_manager, + pager, +}) => ({ + onload() { + edit_actions.forEach(a => add_edit_shortcut(notebook, actions, keyboard_manager, a)) + + pager.events.on('open_with_text.Pager', (event, {data: {'text/html': html}}) => + render_math(pager, html)) + }, +})) diff --git a/etc/logo-64x64-R.png b/etc/logo-64x64-R.png new file mode 100644 index 0000000000000000000000000000000000000000..eff82d271dda2efd03b717c2aadc0b79cebb749c Binary files /dev/null and b/etc/logo-64x64-R.png differ diff --git a/etc/logo-64x64-python.png b/etc/logo-64x64-python.png new file mode 100644 index 0000000000000000000000000000000000000000..a3f5177ca47e47a0ca251531d11d9cb97b60c5d6 Binary files /dev/null and b/etc/logo-64x64-python.png differ diff --git a/etc/logo-64x64.png b/etc/logo-64x64.png new file mode 100644 index 0000000000000000000000000000000000000000..f820c4cef82e602d6e1aaa50f526a58bebce9128 Binary files /dev/null and b/etc/logo-64x64.png differ