Skip to content

Instantly share code, notes, and snippets.

@paduel
Last active July 2, 2019 11:44
Show Gist options
  • Save paduel/3e60552215cb5df084ba56ce73343114 to your computer and use it in GitHub Desktop.
Save paduel/3e60552215cb5df084ba56ce73343114 to your computer and use it in GitHub Desktop.
Easily add new snippets for jupyter nbextension.
def new_snippet(name, code):
'''Save a new snippet at json for Snippets nbextesion
Parameters
----------
name : str
Name that will appear in the drop-down list
code : str
String containing the lines of code to be saved.
'''
import json
path = '<change to your path>/jupyter/nbextensions/snippets/'
file= path + 'snippets.json'
with open(file) as json_file:
data = json.load(json_file)
code = code.split('\\n')
data['snippets'] += [{'name': name, 'code':code}]
with open(file, 'w') as outfile:
json.dump(data, outfile, indent=4, sort_keys=True)
print('Snippet saved.')
return
# Example :
code = """
def new_snippet(name, code):
'''Save a new snippet at json for Snippets nbextesion
Parameters
----------
name : str
Name that will appear in the drop-down list
code : str
String containing the lines of code to be saved.
'''
import json
path = '<change to your path>/jupyter/nbextensions/snippets/'
file= path + 'snippets.json'
with open(file) as json_file:
data = json.load(json_file)
code = code.split('\\n')
data['snippets'] += {'name': name, 'code':code}
with open(file, 'w') as outfile:
json.dump(data, outfile, indent=4, sort_keys=True)
print('Snippet saved.')
return
"""
new_snippet("New snippet",code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment