Last active
September 19, 2018 04:46
Create a snippet - jupyter notebook snippet extensions
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
{"snippets":[ | |
{ "name" : "Add Snippet", "code" :[ | |
"!cp /home/jack/.local/share/jupyter/nbextensions/snippets/snippets.json snippets-temp.json", | |
"name = \" <place name here>\" ", | |
"codes =\"\"\" ", | |
"<place code here> ", | |
"\"\"\" ", | |
"count = 0 ", | |
"for line in codes.split('\\n'): ", | |
" if len(line) > 0: ", | |
" count = count +1 ", | |
"print(count) ", | |
"f0 = open('snippets-temp.json', 'r').readlines()", | |
"f1 = open('/home/jack/.local/share/jupyter/nbextensions/snippets/snippets.json', 'w')", | |
"for line in f0: ", | |
" if line[0:2] != \"]}\": ", | |
" f1.write(line) ", | |
"f1.write(' ,{ \"name\" : \"'+name+'\", \"code\" :[')", | |
"codes =codes.split(\"\\n\") ", | |
"cnt = 0 ", | |
"for line in codes: ", | |
" if len(line) > 0: ", | |
" cnt = cnt+1 ", | |
" print (\"::\",cnt,line) ", | |
" if cnt != count: ", | |
" f1.write(' \"'+line+'\",\\n')", | |
" if cnt == count: ", | |
" f1.write(' \"'+line+'\"\\n')", | |
"f1.write(' ]}')", | |
"f1.write(']}') ", | |
"f1.close() " | |
]} | |
,{ "name" : "View snippet.jsons", "code" :[ " ", | |
"filein = '/home/jack/.local/share/jupyter/nbextensions/snippets/snippets.json'", | |
"import json", | |
"from pprint import pprint", | |
"with open(filein) as f:", | |
" data = json.load(f)", | |
"pprint(data)" | |
]} | |
,{ "name" : "date/filename", "code" :[ " ", | |
"import time", | |
"DATE = time.strftime('%Y-%m%d%H%M%S')", | |
"filename = DATE+_.jpg" | |
]} | |
]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nbextensions/snippets/snippets.json is the extension to use when adding snippets to Jupyter Notebook .
this can replace the snippets.json . The first snippet is 'Make Snippet' .
when the snippet is used: enter a snippet name, and paste code between the triple apostrophes.
when you execute the cell the new snippet is entered into the json file . You will have to reload the notebook before it is available.
BACK up the original JUST-in-CASE