Skip to content

Instantly share code, notes, and snippets.

@JupyterJones
Last active September 19, 2018 04:46
Show Gist options
  • Save JupyterJones/49e75750fb2ed83bb72caa4b4560c0ee to your computer and use it in GitHub Desktop.
Save JupyterJones/49e75750fb2ed83bb72caa4b4560c0ee to your computer and use it in GitHub Desktop.
Create a snippet - jupyter notebook snippet extensions
{"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"
]}
]}
@JupyterJones
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment