Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New notebook extension: code snippets #826

Merged
merged 5 commits into from
Jan 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/jupyter_contrib_nbextensions/nbextensions/snippets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Snippets
========

Code cell snippets.

This extension adds a drop-down menu to the IPython toolbar that allows easy
insertion of code snippet cells into the current notebook. The code snippets
are defined in a JSON file in `nbextensions/snippets/snippets.json` and an
example snippet is included with this extension.

![](snippets-demo.gif)

Adding new snippets
-------------------

Snippets are specified by adding a new JSON block to the list of existing snippets in `nbextensions/snippets/snippets.json`. For example, to add a new snippet that imports numpy, matplotlib, and a print statement, the JSON file should be modified from:

```json
{
"snippets" : [
{
"name" : "example",
"code" : [
"# This is an example snippet!",
"# To create your own, add a new snippet block to the",
"# snippets.json file in your jupyter nbextensions directory:",
"# /nbextensions/snippets/snippets.json",
"import this"
]
}
]
}
```

to this:

```json
{
"snippets" : [
{
"name" : "example",
"code" : [
"# This is an example snippet!",
"# To create your own, add a new snippet block to the",
"# snippets.json file in your jupyter nbextensions directory:",
"# /nbextensions/snippets/snippets.json",
"import this"
]
},
{
"name" : "some imports",
"code" : [
"import numpy as np",
"import matplotlib as mpl",
"print('spam')"
]
}
]
}
```
72 changes: 72 additions & 0 deletions src/jupyter_contrib_nbextensions/nbextensions/snippets/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Code cell snippets

define([
'jquery',
'base/js/namespace',
'base/js/dialog',
'base/js/utils',
'services/config'
], function($, Jupyter, dialog, utils, configmod) {
"use strict";

// create config object to load parameters
var base_url = utils.get_body_data("baseUrl");
var config = new configmod.ConfigSection('notebook', {base_url: base_url});

config.loaded.then(function() {
var dropdown = $("<select></select>").attr("id", "snippet_picker")
.css("margin-left", "0.75em")
.attr("class", "form-control select-xs")
.change(insert_cell);
Jupyter.toolbar.element.append(dropdown);
});

// will be called when the nbextension is loaded
function load_extension() {
config.load(); // trigger loading config parameters

$.getJSON("/nbextensions/snippets/snippets.json", function(data) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, should have picked this up before, but this path will fail on anyn server not using the default base_url of /, for example on jupyterhub. A better solution would be to use a relative path ./snippets.json

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the $.getJSON callback relies on the config.loaded already having added the select, so it might be better to chain them directly. I should have mentioned these before merging, really, sorry!

// Add the header as the top option, does nothing on click
var option = $("<option></option>")
.attr("id", "snippet_header")
.text("Snippets");
$("select#snippet_picker").append(option);

// Add options for each code snippet in the snippets.json file
$.each(data['snippets'], function(key, snippet) {
var option = $("<option></option>")
.attr("value", snippet['name'])
.text(snippet['name'])
.attr("code", snippet['code'].join('\n'));
$("select#snippet_picker").append(option);
});
})
.error(function(jqXHR, textStatus, errorThrown) {
// Add an error message if the JSON fails to load
var option = $("<option></option>")
.attr("value", 'ERROR')
.text('Error: failed to load snippets!')
.attr("code", "");
$("select#snippet_picker").append(option);
});

};

var insert_cell = function() {
var selected_snippet = $("select#snippet_picker").find(":selected");

if (selected_snippet.attr("name") != 'header') {
var code = selected_snippet.attr("code");
var new_cell = Jupyter.notebook.insert_cell_above('code');
new_cell.set_text(code);
new_cell.focus_cell();

$("option#snippet_header").prop("selected",true);
}
};

// return public methods
return {
load_ipython_extension : load_extension
};
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"snippets" : [
{
"name" : "example",
"code" : [
"# This is an example snippet!",
"# To create your own, add a new snippet block to the",
"# snippets.json file in your jupyter nbextensions directory:",
"# /nbextensions/snippets/snippets.json",
"import this"
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Type: IPython Notebook Extension
Compatibility: 4.x
Name: Snippets
Main: main.js
Description: Adds a drop-down menu to insert snippet cells into the current notebook.
Link: readme.md