jupytercalpoly / jupyterlab-code-snippets

Save, reuse, and share code snippets using JupyterLab Code Snippets!
https://jupyterlab-code-snippets-documentation.readthedocs.io
Other
164 stars 25 forks source link

Help to migrate legacy code snippets from Elyra #220

Closed Daksol closed 2 years ago

Daksol commented 2 years ago

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


second attempt

jahn96 commented 2 years ago

Hi, Thank you for your interest in this extension and sorry for your inconvenience.

To migrate your code snippets,

  1. In your current directory, create a directory called snippets.
  2. Place all your legacy snippets in the directory.
  3. Run the python script below which will print your snippets in the correct JSON schema.
  4. 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 2 years ago

Thanks. That worked for me.