custom_js = """
// nbextensions snippets menu customization.
var json_data;
$.getJSON( "python_snippets.json", function(data) {
json_data = data;
});
require(["nbextensions/snippets_menu/main"], function (snippets_menu) {
console.log('Loading `snippets_menu` customizations from `custom.js`');
var horizontal_line = '---';
var snippets_json = json_data
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(snippets_json);
console.log('Loaded `snippets_menu` customizations from `custom.js`');
});
"""
import os
import glob
import re
home = os.path.expanduser("~")
jupyter_home = os.path.join(home,'.jupyter')
jupyter_custom = os.path.join(jupyter_home, 'custom')
test_str = '// nbextensions snippets menu customization.'
if 'custom' in os.listdir(jupyter_home):
file_names = list(os.walk(jupyter_custom))[0][-1]
custom_js_file = os.path.join(jupyter_custom,'custom.js')
if 'custom.js' in file_names:
# Get the contents of the custom.js file.
f = open(custom_js_file, 'r')
contents = f.read()
f.close()
# Test to see if the js code has been added.
if test_str in contents:
pass
# If the code hasn't been added then add it.
else:
f = open(custom_js_file, 'a+')
f.write(custom_js)
f.close()
# If there is not custom.js file create one and write code to it.
else:
f = open(custom_js_file, 'w')
f.write(custom_js)
f.close()
import json
ent =dict()
ent['name'] = 'Boiler-plate'
ent['snippet'] = ['import numpy as np','import pandas as pd']
d = dict()
d['name'] = 'Basic'
d['sub-menu'] = [ent]
json_file = json.dumps(d)
# json_file_name = os.path.join(jupyter_custom, 'python_snippets.json')
# f = open(json_file_name, 'w')
f = open('python_snippets.json', 'w')
f.write(json_file)
f.close()
%%javascript
// $.getJSON( "json_practice.json", function( data ) {
// // console.log( "JSON Data: " + data);
// $.each( data, function( key, val ) {
// // console.log(key + "value:: " + val );
// var json_data = key + "value:: " + val;
// var cell = Jupyter.notebook.insert_cell_below('markdown');
// cell.set_text(data);
// cell.render();
// });
// });
// $.getJSON( "json_practice.json", function(data) {
// console.log(data['name']);
// });
// var my_fav;
// $.getJSON( "json_practice.json", function(data) {
// my_fav = data;
// });
%%javascript
// nbextensions snippets menu customization.
var my_fav;
$.getJSON( "python_snippets.json", function(data) {
my_fav = data;
});
require(["nbextensions/snippets_menu/main"], function (snippets_menu) {
console.log('Loading `snippets_menu` customizations from `custom.js`');
var horizontal_line = '---';
var my_favorites = my_fav
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`');
});
import numpy as np
import pandas as pd
%%javascript
// Get the contents of your selected cell and loag it to console.
var ci = Jupyter.notebook.get_selected_cell().input;
var txt = ci.contents()[1].innerText;
console.log(txt);