Help to migrate legacy code snippets from Elyra
Daksol opened this issue · 2 comments
Daksol commented
- Using Jupyter Lab 3.2.2 on Windows 10
- Managing envt with conda
- I have installed jupyterlab-code-snippets v2.1
I started off using the Snippets functionality available in Elyra. But Elyra is a big installation, and the snippets is the only bit I am using. So moving to the jupytercalpoly/jupyterlab-code-snippets looked a good option.
I need some small help in migrating my existing Elyra snippets - these are saved as "one snippet per json file" in directory *USERPROFILE*\AppData\Roaming\jupyter\metadata\code-snippets
At this point I still have the Elyra snippets extension in place.
first attempt to import legacy snippets to jupytercalpoly snippets
- I found the sample jupytercalpoly snippets in directory
*USERPROFILE*\.jupyter\lab\user-settings\jupyterlab-code-snippetsall in a single filesnippets.jupyterlab-settings.- Snippets there consist of three which came pre-installed,
- And also contain an additional snippet I added via the Jupyter lab GUI as a test also is in that file.
- So I combined the legacy snippets into one file and reformatted them following the format of
snippets.jupyterlab-settings. I allocated each snippet a unique sequence number following on from those id numbers already there. - But when I run Jupyter Lab again, the snippets just added do not show up in the GUI. And the file
snippets.jupyterlab-settingshas been restored to its "factory setting" - same as when first installed.
second attempt
- OK - updating this note as I found some more info
- I have located what appears to be the right place for the jupytercalpoly snippets - at
*USERPROFILE*\AppData\Roaming\jupyter\nbextensions\snippets- So I updated the snippets.json file I found in that directory.
- Sad to say, those ones still do not show up - all I can see is the original three sample items
- These are:- Plotting_sine, Importing packages for data analytics, importing packages for machine learning
jahn96 commented
Hi,
Thank you for your interest in this extension and sorry for your inconvenience.
To migrate your code snippets,
- In your current directory, create a directory called snippets.
- Place all your legacy snippets in the directory.
- Run the python script below which will print your snippets in the correct JSON schema.
- Go to (Advanced Settings > Code Snippet Manager > User Preferences) in JupyterLab and copy and paste the output from the python script.
import os
import json
import glob
def extract_id(json):
try:
return json['id']
except KeyError:
return 0
snippets = []
counter = 0
for filepath in glob.glob(os.path.join('snippets', '*.json')):
with open(filepath) as f:
content = json.load(f)
content['id'] = counter
snippets.append(content)
counter+=1
snippets.sort(key=extract_id)
print('{"snippets": [\n')
for snip in snippets:
if not('tags' in snip):
snip["tags"] = []
if snippets.index(snip) == len(snippets)-1:
print(json.dumps(snip, indent=4, sort_keys=True))
else:
print(json.dumps(snip, indent=4, sort_keys=True), end=",\n")
print("]\n}\n")
Daksol commented
Thanks. That worked for me.