Skip to content

Commit

Permalink
Added 'Include Metadata' command in jupyterlab-jupytext
Browse files Browse the repository at this point in the history
Extension rebuilt using node from conda-forge
#213
  • Loading branch information
mwouts committed Jun 21, 2019
1 parent 317e732 commit 83f1f9b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
19 changes: 15 additions & 4 deletions packages/labextension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
Binary file modified packages/labextension/jupyterlab-jupytext-0.1.1.tgz
Binary file not shown.
69 changes: 65 additions & 4 deletions packages/labextension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import "../style/index.css";

interface JupytextSection {
formats?: string;
notebook_metadata_filter?: string;
cell_metadata_filter?: string;
}

const JUPYTEXT_FORMATS = [
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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" });
}
};

Expand Down

0 comments on commit 83f1f9b

Please sign in to comment.