Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add allowed_extensions setting, allowing additional file extensions in templates #215

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ jobs:
run: |
make build

- name: Artifact
uses: actions/upload-artifact@v3
with:
name: wheel-${{ matrix.os }}-${{ github.sha }}
path: dist/*.whl
if: ${{ always() }}

- name: Lint
run: |
make lint
Expand Down
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
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ classifiers = [
"License :: OSI Approved :: Apache Software License",
]
dependencies = [
"jupyterlab>=3.5"
"jupyterlab>=3.5",
"notebook>=6",
]

[project.optional-dependencies]
Expand Down