Last active
July 2, 2019 11:44
-
-
Save paduel/3e60552215cb5df084ba56ce73343114 to your computer and use it in GitHub Desktop.
Easily add new snippets for jupyter nbextension.
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
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