Skip to content

Commit

Permalink
Version 1.0.1 of the jupyterlab-jupytext extension
Browse files Browse the repository at this point in the history
- A click on a selected format toggles the pairing (#289)
- The extension now uses `JupyterFrontEnd` and `JupyterFrontEndPlugin` from `@jupyterlab/application` rather than `JupyterLab` and `JupyterLabPlugin` for compatibility with JupyterLab 1.0.
  • Loading branch information
mwouts committed Jul 17, 2019
1 parent b46ca10 commit 822a1d3
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 51 deletions.
16 changes: 16 additions & 0 deletions packages/labextension/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 1.0.1 (2019-07-18)

- A click on a selected format toggle the pairing (#289)
- Use `JupyterFrontEnd` and `JupyterFrontEndPlugin` from `@jupyterlab/application` rather than `JupyterLab` and `JupyterLabPlugin` for compatibility with JupyterLab 1.0.

# 1.0.0 (2019-07-06)

- First extension compatible with JupyterLab 1.0

# 0.19.0 (2019-07-06)

- Last extension compatible with JupyterLab 0.35

# 0.1.0 (2018-10-02)

- Initial release of the extension
44 changes: 28 additions & 16 deletions packages/labextension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,51 @@ jupyter labextension install [email protected]

# How to develop this extension

We developed 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 assume that you have activated the conda environment described in [CONTRIBUTING.md](https://github.com/mwouts/jupytext/blob/master/CONTRIBUTING.md). In addition to that environment, you will need `npm`. Install it with

```bash
conda install nodejs
```

In that environment, install JupyterLab's plugin manager, and the extension with
```bash
# Go to the extension folder
cd packages/labextension

# Cleanup
rm lib node_modules yarn.lock

# Install JupyterLab's plugin manager
jlpm install

# Package the extension
npm pack
```

Then you can rebuild the Jupytext python package (with `python setup.py sdist bdist_wheel`) and reinstall it (`pip install dist\jupytext-XXX.tar.gz`).

Alternatively, if you prefer to develop iteratively, you could install a development version of the extension with

```bash
jupyter labextension install . --no-build
```

Then, in another shell on the same environment, start JupyterLab:
Then start JupyterLab in watch mode in another shell on the same environment:
```bash
jupyter lab --watch
```

Finally, make changes to the extension and rebuild it (in the first shell) with:
And finally, make changes to the extension and rebuild it (in the first shell) with:
```bash
jlpm run build
```

Refresh the JupyterLab interface and see your changes in action.

# How to publish a new version of the extension

Bump the version in `package.json`.

Build the new version of `jupyterlab-jupytext-xxx.tgz` with

```bash
npm pack
```
Bump the version in `package.json`, rebuild the extension with `npm pack`. Include the new extension in Git and `setup.py`, and delete the previous version.

and remove any previous version. That package will be included in the Jupytext Python package.

If you wish, you may also update the package on npm with
If you wish, you can also publish the package on [npm](https://www.npmjs.com) with

```bash
npm publish --access=public
```

Binary file removed packages/labextension/jupyterlab-jupytext-1.0.0.tgz
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/labextension/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jupyterlab-jupytext",
"version": "1.0.0",
"version": "1.0.1",
"description": "A JupyterLab extension for Jupytext",
"keywords": [
"jupyter",
Expand Down
73 changes: 40 additions & 33 deletions packages/labextension/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JupyterLab, JupyterLabPlugin } from "@jupyterlab/application";
import { JupyterFrontEnd, JupyterFrontEndPlugin } from "@jupyterlab/application";

import { ICommandPalette } from "@jupyterlab/apputils";

Expand Down Expand Up @@ -43,15 +43,41 @@ const JUPYTEXT_FORMATS = [
}
];

function get_selected_format(notebook_tracker: INotebookTracker): string {
if (!notebook_tracker.currentWidget) return null;

if (
!notebook_tracker.currentWidget.context.model.metadata.has(
"jupytext"
)
)
return "none";

const jupytext: JupytextSection = (notebook_tracker.currentWidget.context.model.metadata.get(
"jupytext"
) as unknown) as JupytextSection;
if (!jupytext.formats) return "none";

const lang = notebook_tracker.currentWidget.context.model.metadata.get(
"language_info"
) as nbformat.ILanguageInfoMetadata;
return lang
? jupytext.formats.replace(
"," + lang.file_extension.substring(1) + ":",
",auto:"
)
: jupytext.formats;
};

/**
* Initialization data for the jupyterlab-jupytext extension.
*/
const extension: JupyterLabPlugin<void> = {
const extension: JupyterFrontEndPlugin<void> = {
id: "jupyterlab-jupytext",
autoStart: true,
requires: [ICommandPalette, INotebookTracker],
activate: (
app: JupyterLab,
app: JupyterFrontEnd,
palette: ICommandPalette,
notebook_tracker: INotebookTracker
) => {
Expand All @@ -65,33 +91,13 @@ const extension: JupyterLabPlugin<void> = {
label: args["label"],
isToggled: () => {
if (!notebook_tracker.currentWidget) return false;

if (
!notebook_tracker.currentWidget.context.model.metadata.has(
"jupytext"
)
)
return formats == "none";

const jupytext: JupytextSection = (notebook_tracker.currentWidget.context.model.metadata.get(
"jupytext"
) as unknown) as JupytextSection;
if (!jupytext.formats) return formats == "none";

const lang = notebook_tracker.currentWidget.context.model.metadata.get(
"language_info"
) as nbformat.ILanguageInfoMetadata;
const jupytext_formats = lang
? jupytext.formats.replace(
"," + lang.file_extension.substring(1) + ":",
",auto:"
)
: jupytext.formats;
const jupytext_formats = get_selected_format(notebook_tracker)

if (formats == "custom")
return (
jupytext_formats &&
[
"none",
"ipynb,auto:light",
"ipynb,auto:percent",
"ipynb,auto:hydrogen",
Expand All @@ -117,6 +123,11 @@ const extension: JupyterLabPlugin<void> = {
return formats != "ipynb,md" && formats != "ipynb,Rmd";
},
execute: () => {
const jupytext: JupytextSection = (notebook_tracker.currentWidget.context.model.metadata.get(
"jupytext"
) as unknown) as JupytextSection;
const jupytext_formats = get_selected_format(notebook_tracker);
const target_format = jupytext_formats === formats ? "none": formats;
console.log("Jupytext: executing command=" + command);
if (formats == "custom") {
alert(
Expand All @@ -125,11 +136,7 @@ const extension: JupyterLabPlugin<void> = {
return;
}

const jupytext: JupytextSection = (notebook_tracker.currentWidget.context.model.metadata.get(
"jupytext"
) as unknown) as JupytextSection;

if (formats == "none") {
if (target_format == "none") {
if (
!notebook_tracker.currentWidget.context.model.metadata.has(
"jupytext"
Expand All @@ -148,12 +155,12 @@ const extension: JupyterLabPlugin<void> = {
return;
}

// set desired format
if (jupytext) jupytext.formats = formats;
// set the desired format
if (jupytext) jupytext.formats = target_format;
else
notebook_tracker.currentWidget.context.model.metadata.set(
"jupytext",
{ formats }
{ target_format }
);
}
});
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
'jupytext/nbextension/jupytext_menu.png',
'jupytext/nbextension/jupytext_menu_zoom.png',
'jupytext/nbextension/jupytext.yml']),
('share/jupyter/lab/extensions', ['packages/labextension/jupyterlab-jupytext-1.0.0.tgz'])],
('share/jupyter/lab/extensions', ['packages/labextension/jupyterlab-jupytext-1.0.1.tgz'])],
entry_points={'console_scripts': ['jupytext = jupytext.cli:jupytext_cli']},
tests_require=['pytest'],
install_requires=['nbformat>=4.0.0', 'pyyaml', 'mock;python_version<"3"'],
Expand Down

0 comments on commit 822a1d3

Please sign in to comment.