diff --git a/packages/labextension/README.md b/packages/labextension/README.md index 1e4aabe18..51fa59657 100644 --- a/packages/labextension/README.md +++ b/packages/labextension/README.md @@ -6,15 +6,26 @@ This extension adds a few [Jupytext](https://github.com/mwouts/jupytext) command ## Installation -Please [install Jupytext](https://github.com/mwouts/jupytext/blob/master/README.md#installation) first. Make sure Jupyter is configured to use Jupytext's contents manager. Then, install the extension with: +Please [install Jupytext](https://github.com/mwouts/jupytext/blob/master/README.md#installation) first. The lab extension is bundled with the Python package. + +If you prefer to use the extension that is distributed on npm, run: ```bash jupyter labextension install jupyterlab-jupytext ``` +In all cases, the extension will work only if Jupytext's content manager is active in Jupyter - make sure Markdown documents and scripts have a notebook icon. + # How to develop this extension -We started developping the extension following the [xkcd extension tutorial](https://jupyterlab.readthedocs.io/en/stable/developer/xkcd_extension_tutorial.html). Follow the instructions there to create a conda environment in which you will be able to develop the extension. In that environment, install JupyterLab's plugin manager, and the extension with +We started developping the extension following the [xkcd extension tutorial](https://jupyterlab.readthedocs.io/en/stable/developer/xkcd_extension_tutorial.html). Follow the instructions there to create a conda environment in which you will be able to develop the extension. + +We also recommend to use `node` in version 11 at least. To this aim, you can use node from `conda-forge`: +```bash +conda install nodejs -c conda-forge +``` + +In that environment, install JupyterLab's plugin manager, and the extension with ```bash jlpm install jupyter labextension install . --no-build @@ -36,13 +47,13 @@ Refresh the JupyterLab interface and see your changes in action. Bump the version in `package.json`. -Build the new version of `jupyterlab-jupytext-xxx.tgz` with +Build the new version of `jupyterlab-jupytext-xxx.tgz` with ```bash npm pack ``` -and remove any previous version. That package will be included in the Jupytext Python package. +Remove any previous version. And update the link to the new version in `setup.py` at the project root. If you wish, you may also update the package on npm with diff --git a/packages/labextension/jupyterlab-jupytext-0.1.1.tgz b/packages/labextension/jupyterlab-jupytext-0.1.1.tgz index 85ce7f1c4..4a4da1ad7 100644 Binary files a/packages/labextension/jupyterlab-jupytext-0.1.1.tgz and b/packages/labextension/jupyterlab-jupytext-0.1.1.tgz differ diff --git a/packages/labextension/src/index.ts b/packages/labextension/src/index.ts index 40215efba..288274b50 100644 --- a/packages/labextension/src/index.ts +++ b/packages/labextension/src/index.ts @@ -10,6 +10,8 @@ import "../style/index.css"; interface JupytextSection { formats?: string; + notebook_metadata_filter?: string; + cell_metadata_filter?: string; } const JUPYTEXT_FORMATS = [ @@ -83,9 +85,9 @@ const extension: JupyterLabPlugin<void> = { ) as nbformat.ILanguageInfoMetadata; const jupytext_formats = lang ? jupytext.formats.replace( - "," + lang.file_extension.substring(1) + ":", - ",auto:" - ) + "," + lang.file_extension.substring(1) + ":", + ",auto:" + ) : jupytext.formats; if (formats == "custom") @@ -168,8 +170,67 @@ const extension: JupyterLabPlugin<void> = { }, command: "help:open", category: "Jupytext", - rank: JUPYTEXT_FORMATS.length + rank: JUPYTEXT_FORMATS.length + 1 }); + + // Metadata in text representation + app.commands.addCommand("jupytext_metadata", { + label: "Include Metadata", + isToggled: () => { + if (!notebook_tracker.currentWidget) + return true; + + if (!notebook_tracker.currentWidget.context.model.metadata.has("jupytext")) + return true; + + const jupytext: JupytextSection = (notebook_tracker.currentWidget.context.model.metadata.get("jupytext") as unknown) as JupytextSection; + + if (jupytext.notebook_metadata_filter === '-all') + return false; + + return true; + }, + isEnabled: () => { + if (!notebook_tracker.currentWidget) + return false; + + if (!notebook_tracker.currentWidget.context.model.metadata.has("jupytext")) + return false; + + const jupytext: JupytextSection = (notebook_tracker.currentWidget.context.model.metadata.get("jupytext") as unknown) as JupytextSection; + + if (jupytext.notebook_metadata_filter === undefined) + return true; + + if (jupytext.notebook_metadata_filter === '-all') + return true; + + return false; + }, + execute: () => { + console.log("Jupytext: toggling YAML header"); + if (!notebook_tracker.currentWidget) + return; + + if (!notebook_tracker.currentWidget.context.model.metadata.has("jupytext")) + return; + + const jupytext: JupytextSection = (notebook_tracker.currentWidget.context.model.metadata.get("jupytext") as unknown) as JupytextSection; + + if (jupytext.notebook_metadata_filter) { + delete jupytext.notebook_metadata_filter; + if (jupytext.cell_metadata_filter === '-all') + delete jupytext.cell_metadata_filter; + return + } + + jupytext.notebook_metadata_filter = '-all' + if (jupytext.cell_metadata_filter === undefined) + jupytext.cell_metadata_filter = '-all'; + } + }); + + palette.addItem({ command: "jupytext_metadata", rank: JUPYTEXT_FORMATS.length + 1, category: "Jupytext" }); } };