Skip to content

Commit 09998d0

Browse files
feat: use jupyter data dirs to load snippets
1 parent 24c8a4e commit 09998d0

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ jupyter lab build
2222

2323
## Usage
2424

25-
In `jupyter_notebook_config.py`:
26-
27-
```python
28-
c.JupyterLabCodeSnippets.snippet_dir = "/path/to/snippets"
29-
```
25+
Add snippets in `[jupyter_data_dir]/code_snippets` (see: https://jupyter.readthedocs.io/en/latest/projects/jupyter-directories.html#id2)
3026

3127
In JupyterLab, use the "Code Snippets" menu to select the snippet:
3228

jupyterlab-code-snippets/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ def load_jupyter_server_extension(nb_app):
1616
nb_app: notebook.notebookapp.NotebookApp
1717
Notebook application instance
1818
"""
19-
snippet_dir = nb_app.config.get('JupyterLabCodeSnippets', {}).get('snippet_dir', '')
20-
# TODO: add snippets from jupyter paths?
2119

22-
loader = SnippetsLoader(snippet_dir)
20+
loader = SnippetsLoader()
2321
setup_handlers(nb_app.web_app, loader)

jupyterlab-code-snippets/loader.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,40 @@
22

33
from pathlib import PurePath
44

5+
from jupyter_core.paths import jupyter_path
6+
7+
import tornado
8+
59

610
class SnippetsLoader:
7-
def __init__(self, snippet_dir):
8-
self.snippet_dir = snippet_dir
11+
def __init__(self):
12+
self.snippet_paths = jupyter_path("code_snippets")
913

1014
def collect_snippets(self):
11-
# TODO: handle multiple directories
12-
root = self.snippet_dir
1315
snippets = []
14-
for dirpath, dirnames, filenames in os.walk(root):
15-
for f in filenames:
16-
fullpath = PurePath(dirpath).relative_to(root).joinpath(f)
17-
snippets.append(fullpath.parts)
16+
for root_path in self.snippet_paths:
17+
for dirpath, dirnames, filenames in os.walk(root_path):
18+
for f in filenames:
19+
fullpath = PurePath(dirpath).relative_to(root_path).joinpath(f)
20+
21+
if fullpath.parts not in snippets:
22+
snippets.append(fullpath.parts)
23+
1824
snippets.sort()
1925
return snippets
2026

2127
def get_snippet_content(self, snippet):
2228
try:
23-
path = os.path.join(self.snippet_dir, *snippet)
24-
with open(path) as f:
25-
content = f.read()
26-
except:
27-
content = ''
29+
for root_path in self.snippet_paths:
30+
path = os.path.join(root_path, *snippet)
31+
32+
# Prevent access to the entire file system when the path contains '..'
33+
accessible = os.path.realpath(path).startswith(root_path)
2834

29-
return content
35+
if accessible and os.path.isfile(path):
36+
with open(path) as f:
37+
return f.read()
38+
except:
39+
raise tornado.web.HTTPError(status_code=500)
3040

41+
raise tornado.web.HTTPError(status_code=404)

0 commit comments

Comments
 (0)