-
Notifications
You must be signed in to change notification settings - Fork 806
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/jupyter_contrib_nbextensions/nbextensions/snippets/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
72
src/jupyter_contrib_nbextensions/nbextensions/snippets/main.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
// 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 | ||
}; | ||
}); |
Binary file added
BIN
+421 KB
src/jupyter_contrib_nbextensions/nbextensions/snippets/snippets-demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions
14
src/jupyter_contrib_nbextensions/nbextensions/snippets/snippets.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
] | ||
} |
6 changes: 6 additions & 0 deletions
6
src/jupyter_contrib_nbextensions/nbextensions/snippets/snippets.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!