在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:moble/jupyter_boilerplate开源软件地址:https://github.com/moble/jupyter_boilerplate开源编程语言:JavaScript 99.5%开源软件介绍:Jupyter notebook snippets menuAdds a customizable menu item to Jupyter notebooks to insert snippets, boilerplate, and examples of code. This notebook extension adds a menu item (or multiple menu items, if
desired) after the The new menu comes with a default value relevant for python
programming — especially scientific computing — though this is fully
user-configurable as detailed below. The default menu is named
So, for example, if you are editing a code cell and want to import
matplotlib for use in the notebook, you can just click the import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline The inserted text will be selected, so that you can delete it by pressing backspace or delete, or you can just select another snippet to replace it -- and just to highlight what was inserted. Note that many of the snippets involve variable names prefixed with
Similarly, some strings are intended to be replaced, such as the axis labels in plots. These are there to show you what can be done, and to remind you to put informative labels in your plots. If you don't want, e.g., a title on your plot, just remove that line. InstallationTo install this extension alone (without the main collection of nbextensions), run the following from the command line: git clone git://github.com/moble/jupyter_boilerplate
jupyter nbextension install jupyter_boilerplate
jupyter nbextension enable jupyter_boilerplate/main You can then disable the extension if you want with jupyter nbextension disable jupyter_boilerplate/main Basic menu customizationThe default menu might have too many irrelevant items for you, or may not have something you would find useful. You can easily customize it in the jupyter_nbextensions_configurator, which you almost certainly have if you installed this extension the normal way, through jupyter_contrib_nbextensions. Usually, you can get to the configurator by pointing your browser to http://127.0.0.1:8888/nbextensions, though you may have to modify the URL if you use a more complicated jupyter server. On the configurator page, you will see a number of options (as well as this README) that should be fairly self-explanatory, allowing you to remove any of the default menu items, or add a custom menu within the "Snippets" menu. The custom menu is written in JSON, and a simple (and useless) example is given that should be easy to modify as needed. Advanced menu customizationIt is also possible to extensively customize the menus in far more
complex ways using your You can find the path to echo $(jupyter --config-dir)/custom/custom.js For Mac and linux users, the result is probably
The customization process is best explained through examples, which
are available in the The theory behind this customization is that the menu is represented by a nested JavaScript array (which is just like a python list). So to change the menu, you just need to change that array. And each menu item inside this array is represented by a JavaScript "object" (which is just like a python dictionary). So to change a menu item, you just have to change that object. Again, this makes more sense when looking at example, as follows. Add a custom sub-menu with simple snippetsSuppose you want to make a new sub-menu with your favorite snippets at
the bottom of require(["nbextensions/snippets_menu/main"], function (snippets_menu) {
console.log('Loading `snippets_menu` customizations from `custom.js`');
var horizontal_line = '---';
var my_favorites = {
'name' : 'My favorites',
'sub-menu' : [
{
'name' : 'Menu item text',
'snippet' : ['new_command(3.14)',],
},
{
'name' : 'Another menu item',
'snippet' : ['another_new_command(2.78)',],
},
],
};
snippets_menu.options['menus'] = snippets_menu.default_menus;
snippets_menu.options['menus'][0]['sub-menu'].push(horizontal_line);
snippets_menu.options['menus'][0]['sub-menu'].push(my_favorites);
console.log('Loaded `snippets_menu` customizations from `custom.js`');
}); Now, if you refresh your notebook, you'll see a new menu item named "My
favorites". Hover over it, and it will pop up a sub-menu with two more
options. Click the first one, and it will insert We discuss how all this works below. But first, we need to slightly generalize the example above to work with more complicated snippets. More complicated snippetsThe example above inserted simple one-line snippets of code. Those snippets didn't have any quotation marks (single or double), backslashes, or newlines, which made everything easy. Unfortunately, JavaScript doesn't deal too well with strings. (There are no raw triple-quoted strings, like in python.) So there are just three things to remember when writing snippets.
This is all best described with another example. Let's change the first function above, to give it some more lines and some quotes: require(["nbextensions/snippets_menu/main"], function (snippets_menu) {
console.log('Loading `snippets_menu` customizations from `custom.js`');
var horizontal_line = '---';
var my_favorites = {
'name' : 'My $\\nu$ favorites',
'sub-menu' : [
{
'name' : 'Multi-line snippet',
'snippet' : ['new_command(3.14)',
'other_new_code_on_new_line("with a string!")',
'stringy(\'escape single quotes once\')',
"stringy2('or use single quotes inside of double quotes')",
'backslashy("This \\ appears as just one backslash in the output")',
'backslashy2("Here are \\\\ two backslashes")',],
},
{
'name' : 'TeX appears correctly $\\alpha_W e\\int_0 \\mu \\epsilon$',
'snippet' : ['another_new_command(2.78)',],
},
],
};
snippets_menu.options['menus'].push(snippets_menu.default_menus[0]);
snippets_menu.options['menus'][0]['sub-menu'].push(horizontal_line);
snippets_menu.options['menus'][0]['sub-menu'].push(my_favorites);
console.log('Loaded `snippets_menu` customizations from `custom.js`');
}); Note the code output by the first item contains all sorts of interesting
strings. Also, the menu title of the second item contains TeX, which will
display correctly, and is used in some of the default menus to show the
standard symbols for physical constants. For more examples, look at the
default menus stored in the How it works: Creating new menu itemsEach of the menu items above is a JavaScript object (like a python
The How it works: Splicing new menu items into the oldBesides just creating the menu items, we may want to join together previously created items. That's the purpose of this line in the code above: snippets_menu.options['menus'][0]['sub-menu'].push(my_favorites); This uses
the JavaScript If you think about this last point, you'll realize that snippets_menu.options['menus'].push(snippets_menu.default_menus[0]);
snippets_menu.options['menus'].push(my_favorites); This would place your favorites after the default snippets_menu.options['menus'].push(my_favorites);
snippets_menu.options['menus'].push(snippets_menu.default_menus[0]); (In general, to add a new element at a given index of an array, you could also just use the splice function.) This might be useful if you have one set of very frequently used
commands, and want immediate access, without going through various
levels of the usual menu. A useful example of this is
shown below. The Other menu manipulationsTo rearrange menu items, just use standard JavaScript techniques. The two most likely examples are deleting and rearranging menu items, but we'll also see that other manipulations are easy. We can also change where the new menus go, and what they look like. Deleting menu itemsTo delete an item, just snippets_menu.python.matplotlib['sub-menu'] Remember that require(["nbextensions/snippets_menu/main"], function (snippets_menu) {
console.log('Loading `snippets_menu` customizations from `custom.js`');
snippets_menu.python.matplotlib['sub-menu'].splice(1, 1); // Delete 1 element starting at position 1 of the sub-menu
console.log('Loaded `snippets_menu` customizations from `custom.js`');
}); The first Rearranging menu itemsFollowing the example above, suppose you don't want to delete the second setup item under "Matplotlib", but instead want to swap those first two items. To make this swap, you need to do the usual trick of storing one element in a temporary variable, and then reassign appropriately. The following code achieves this purpose: require(["nbextensions/snippets_menu/main"], function (snippets_menu) {
console.log('Loading `snippets_menu` customizations from `custom.js`');
var tmp = snippets_menu.python.matplotlib['sub-menu'][0];
snippets_menu.python.matplotlib['sub-menu'][0] = snippets_menu.python.matplotlib['sub-menu'][1];
snippets_menu.python.matplotlib['sub-menu'][1] = tmp;
console.log('Loaded `snippets_menu` customizations from `custom.js`');
}); Change direction of sub-menusEach sub-menu may be placed to the right or left side of the menu item
containing it. This is controlled by the But these are configurable properties. If, for example, you want to change the default menus to open on the right (which would be the more standard behavior), you can use this: snippets_menu.default_menus[0]['sub-menu-direction'] = 'right'; This may be particularly useful if we change the position of the menus, as in the next examples. Starting over with the menusEach of the menu items under the default require(["nbextensions/snippets_menu/main"], function (snippets_menu) {
console.log('Loading `snippets_menu` customizations from `custom.js`');
snippets_menu.default_menus[0]['sub-menu'].splice(3, 2); // Remove SymPy and pandas
snippets_menu.python.sympy['sub-menu-direction'] = 'left'; // Point new SymPy menus to left
snippets_menu.python.numpy['sub-menu-direction'] = 'left'; // Point new Numpy menus to left
snippets_menu.options['menus'].push(snippets_menu.default_menus[0]); // Start with the remaining "Snippets" menu
snippets_menu.options['menus'].push(snippets_menu.python.sympy); // Follow that with a new SymPy menu
snippets_menu.options['menus'].push(snippets_menu.python.numpy); // Follow that with a new Numpy menu
console.log('Loaded `snippets_menu` customizations from `custom.js`');
}); The default menu group is This gives us the original You can see that the two items are indeed removed from Changing the insertion pointYou might want to change the order of the menus in the navbar (that
top-level bar with "File", etc.). For example, it might feel
particularly natural to have "Help" as the last item, so maybe you'd
prefer to put the To help do this, there are two additional options available, which can
be set either in the configurator or in
So placing the require(["nbextensions/snippets_menu/main"], function (snippets_menu) {
console.log('Loading `snippets_menu` customizations from `custom.js`');
snippets_menu.default_menus[0]['menu-direction'] = 'left'; // Open top-level menu to the left...
snippets_menu.default_menus[0]['sub-menu-direction'] = 'right'; // ...and sub-menus to the right.
snippets_menu.options['menus'].push('---', snippets_menu.default_menus[0]); // Add horizontal line and default menus
snippets_menu.options['sibling'] = $("#insert_cell_below"); // Find the place at which to insert the new menus
console.log('Loaded `snippets_menu` customizations from `custom.js`');
}); Here's what that looks like: And of course, you can combine this selection of the insertion point with other techniques above, where you change the content of the menus. Multiple menus in separate locationsFinally, we have one more interesting example that brings together various
threads from the previous examples. It is possible to place multiple menus in
different locations. For example, suppose we want to combine two of the examples
above, where (1) we separated "SymPy" into its
own menu on the navbar, and (2) we placed the
To add these two separate menus, we place the first with the usual
approach, and then place the second with another function,
So, putting it all together, the code needed for this arrangement is as follows: require(["nbextensions/snippets_menu/main"], function (snippets_menu) {
console.log('Loading `snippets_menu` customizations from `custom.js`');
var sympy_menu = [snippets_menu.
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论