Skip to content

feat: add script to convert jupyter-boilerplate snippets #9

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

Merged
merged 2 commits into from
Mar 31, 2020
Merged
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ In JupyterLab, use the "Snippets" menu to select the snippet:

<img width="570" alt="Schermafbeelding 2020-03-30 om 17 25 31" src="https://user-images.githubusercontent.com/46192475/77930697-8257fd00-72ab-11ea-8a77-36f45d6442d9.png">

## Convert snippets from jupyter-boilerplate format

See [jupyter-boilerplate-converter](jupyter-boilerplate-converter/README.md) on how to convert snippets from the
[jupyter-boilerplate](https://github.com/moble/jupyter_boilerplate) classic notebook extension (not available for
JupyterLab) to jupyterlab-snippets.

## Troubleshoot

Expand Down
50 changes: 50 additions & 0 deletions jupyter-boilerplate-converter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# jupyter-boilerplate-converter

Convert snippets from the [jupyter-boilerplate](https://github.com/moble/jupyter_boilerplate) classic notebook
extension (not available for JupyterLab) to [jupyterlab-snippets](../README.md).

## Usage

Run the following commands in the `jupyter-boilerplate-converter` directory.
```
$ npm install
$ npm run convert <path/to/boilerplate-snippet.js> <path/to/snippets/dir> [extension]
```

## Example

Convert built-in jupyter-boilerplate snippets.

Run the following commands in the `jupyter-boilerplate-converter` directory.

Download jupyter-boilerplate from github and list snippets:
```
$ git clone git://github.com/moble/jupyter_boilerplate
$ ls -l jupyter_boilerplate/snippets_submenus_python

astropy.js matplotlib.js numpy.js numpy_ufuncs.js python.js
scipy.js scipy_special.js sympy_assumptions.js h5py.js numba.js
numpy_polynomial.js pandas.js python_regex.js scipy_constants.js sympy.js
sympy_functions.js
```

Find JupyterLab data directories:
```
$ jupyter --paths

# Output in MacOs, this will be different on Linux and Windows
...
data:
/Users/<username>/Library/Jupyter
/Users/<username>/miniconda3/envs/<envname>/share/jupyter
/usr/local/share/jupyter
/usr/local/share/jupyter
/usr/share/jupyter
...
```

Convert one of the snippets to JupyterLab user-data directory (top directory in the list above):
```
$ npm install # only required to run once
$ npm run convert jupyter_boilerplate/snippets_submenus_python/numpy.js ~/Library/Jupyter .py
```
41 changes: 41 additions & 0 deletions jupyter-boilerplate-converter/convert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var requirejs = require('requirejs');
var fs = require('fs');
var sanitizeFilename = require('sanitize-filename');


const source = process.argv[2];
const destination = process.argv[3];
const extension = process.argv[4] || '';

requirejs.config({
nodeRequire: require
});
require('node-define');

sourceData = require(source);

if (!fs.existsSync(destination)){
fs.mkdirSync(destination);
}

function createLevel(path, data) {
if (!data.name) {
return
}
const newPath = `${path}/${sanitizeFilename(data.name)}`;

if (data.snippet) {
fs.writeFile(`${newPath}${extension}`, data.snippet.join('\n'), function (err) {
if (err) throw err;
});
} else if (data['sub-menu']) {
if (!fs.existsSync(newPath)){
fs.mkdirSync(newPath);
}
data['sub-menu'].forEach(submenu => {
createLevel(newPath, submenu);
});
}
}

createLevel(destination, sourceData);
44 changes: 44 additions & 0 deletions jupyter-boilerplate-converter/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions jupyter-boilerplate-converter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "jupyter-boilerplate-converter",
"version": "1.0.0",
"description": "jupyter-boilerplate snippets converter",
"scripts": {
"convert": "node ./convert.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "BSD-3-Clause",
"author": "QuantStack",
"devDependencies": {
"node-define": "^0.1.1",
"requirejs": "^2.3.6",
"sanitize-filename": "^1.6.3"
}
}