From e7a9f425fd9304a709d620e62eb1e757de796b35 Mon Sep 17 00:00:00 2001 From: jacobi petrucciani Date: Sun, 25 Jun 2023 18:24:25 -0400 Subject: [PATCH] add allowed_extensions setting --- README.md | 4 +++- js/src/index.js | 13 ++++++++++--- jupyterlab_templates/extension.py | 18 ++++++++++++++---- pyproject.toml | 2 +- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index cdbde6a..95bc037 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) diff --git a/js/src/index.js b/js/src/index.js index 28848d4..cfc39d4 100644 --- a/js/src/index.js +++ b/js/src/index.js @@ -103,22 +103,29 @@ function activate(app, menu, browser, launcher) { const {path} = browser.defaultBrowser.model; return new Promise((resolve) => { + const ext = data.filename.split(".").pop().toLowerCase(); + const isNotebook = ext === "ipynb"; app.commands .execute("docmanager:new-untitled", { + ext, path, - type: "notebook", + type: isNotebook ? "notebook" : "file", }) .then((model) => { app.commands .execute("docmanager:open", { - factory: "Notebook", + factory: isNotebook ? "Notebook" : null, path: model.path, }) .then((widget) => { // eslint-disable-next-line no-param-reassign widget.isUntitled = true; widget.context.ready.then(() => { - widget.model.fromString(data.content); + if (isNotebook) { + widget.model.fromString(data.content); + } else { + widget.content.editor._editor.setValue(data.content); + } resolve(widget); }); }); diff --git a/jupyterlab_templates/extension.py b/jupyterlab_templates/extension.py index 54cb90e..44997cc 100644 --- a/jupyterlab_templates/extension.py +++ b/jupyterlab_templates/extension.py @@ -5,7 +5,6 @@ # 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 @@ -13,13 +12,15 @@ 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 = {} @@ -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( ( @@ -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")) @@ -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()) diff --git a/pyproject.toml b/pyproject.toml index c5a6090..3cdf0fa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,7 +34,7 @@ classifiers = [ "License :: OSI Approved :: Apache Software License", ] dependencies = [ - "jupyterlab>=3.5" + "jupyterlab>=3.5", ] [project.optional-dependencies]