Skip to content

Commit

Permalink
Merge pull request #216 from finos/allow_more_extensions
Browse files Browse the repository at this point in the history
add allowed_extensions setting
  • Loading branch information
timkpaine authored Jun 28, 2023
2 parents e1e0b4b + e7a9f42 commit 30937a1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jupyter server extension enable --py jupyterlab_templates
install the server extension, and add the following to `jupyter_notebook_config.py`

```python3
c.JupyterLabTemplates.allowed_extensions = ["*.ipynb"]
c.JupyterLabTemplates.template_dirs = ['list', 'of', 'template', 'directories']
c.JupyterLabTemplates.include_default = True
c.JupyterLabTemplates.include_core_paths = True
Expand All @@ -52,7 +53,8 @@ If `include_default = True` the `notebook_templates` directory under the [jupyte


### Flags
- `template_dirs`: a list of absolute directory paths. All `.ipynb` files in any *subdirectories* of these paths will be listed as templates
- `allowed_extensions`: a list of extensions to allow templates for. (optional, default `["*.ipynb"]`)
- `template_dirs`: a list of absolute directory paths. All files matching `allowed_extensions` in any *subdirectories* of these paths will be listed as templates
- `include_default`: include the default Sample template (default True)
- `include_core_paths`: include jupyter core paths (see: jupyter --paths) (default True)

Expand Down
13 changes: 10 additions & 3 deletions js/src/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 14 additions & 4 deletions jupyterlab_templates/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@
# This file is part of the jupyterlab_templates library, distributed under the terms of
# the Apache License 2.0. The full license can be found in the LICENSE file.
#
import fnmatch
import json
import os
import os.path
import jupyter_core.paths
import tornado.web

from io import open
from fnmatch import fnmatch
from jupyter_server.base.handlers import JupyterHandler
from jupyter_server.utils import url_path_join


class TemplatesLoader:
def __init__(self, template_dirs):
def __init__(self, template_dirs, allowed_extensions=None):
self.template_dirs = template_dirs
self.allowed_extensions = allowed_extensions or ["*.ipynb"]

def get_templates(self):
templates = {}
Expand All @@ -35,7 +36,12 @@ def get_templates(self):
# Skip top level
continue

for filename in fnmatch.filter(filenames, "*.ipynb"):
_files = [
x
for x in filenames
if any(fnmatch(x, y) for y in self.allowed_extensions)
]
for filename in _files:
if ".ipynb_checkpoints" not in dirname:
files.append(
(
Expand Down Expand Up @@ -109,6 +115,10 @@ def load_jupyter_server_extension(nb_server_app):
"template_dirs", []
)

allowed_extensions = nb_server_app.config.get("JupyterLabTemplates", {}).get(
"allowed_extensions", ["*.ipynb"]
)

if nb_server_app.config.get("JupyterLabTemplates", {}).get("include_default", True):
template_dirs.insert(0, os.path.join(os.path.dirname(__file__), "templates"))

Expand All @@ -131,7 +141,7 @@ def load_jupyter_server_extension(nb_server_app):
)
nb_server_app.log.info("Search paths:\n\t%s" % "\n\t".join(template_dirs))

loader = TemplatesLoader(template_dirs)
loader = TemplatesLoader(template_dirs, allowed_extensions=allowed_extensions)
nb_server_app.log.info(
"Available templates:\n\t%s"
% "\n\t".join(t for t in loader.get_templates()[1].keys())
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ classifiers = [
"License :: OSI Approved :: Apache Software License",
]
dependencies = [
"jupyterlab>=3.5"
"jupyterlab>=3.5",
]

[project.optional-dependencies]
Expand Down

0 comments on commit 30937a1

Please sign in to comment.