Skip to content

Commit cb802b1

Browse files
Merge pull request #9 from mariobuikhuizen/convert
feat: add script to convert jupyter-boilerplate snippets
2 parents 7cc2ed4 + 89857c5 commit cb802b1

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ In JupyterLab, use the "Snippets" menu to select the snippet:
2828

2929
<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">
3030

31+
## Convert snippets from jupyter-boilerplate format
32+
33+
See [jupyter-boilerplate-converter](jupyter-boilerplate-converter/README.md) on how to convert snippets from the
34+
[jupyter-boilerplate](https://github.com/moble/jupyter_boilerplate) classic notebook extension (not available for
35+
JupyterLab) to jupyterlab-snippets.
3136

3237
## Troubleshoot
3338

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# jupyter-boilerplate-converter
2+
3+
Convert snippets from the [jupyter-boilerplate](https://github.com/moble/jupyter_boilerplate) classic notebook
4+
extension (not available for JupyterLab) to [jupyterlab-snippets](../README.md).
5+
6+
## Usage
7+
8+
Run the following commands in the `jupyter-boilerplate-converter` directory.
9+
```
10+
$ npm install
11+
$ npm run convert <path/to/boilerplate-snippet.js> <path/to/snippets/dir> [extension]
12+
```
13+
14+
## Example
15+
16+
Convert built-in jupyter-boilerplate snippets.
17+
18+
Run the following commands in the `jupyter-boilerplate-converter` directory.
19+
20+
Download jupyter-boilerplate from github and list snippets:
21+
```
22+
$ git clone git://github.com/moble/jupyter_boilerplate
23+
$ ls -l jupyter_boilerplate/snippets_submenus_python
24+
25+
astropy.js matplotlib.js numpy.js numpy_ufuncs.js python.js
26+
scipy.js scipy_special.js sympy_assumptions.js h5py.js numba.js
27+
numpy_polynomial.js pandas.js python_regex.js scipy_constants.js sympy.js
28+
sympy_functions.js
29+
```
30+
31+
Find JupyterLab data directories:
32+
```
33+
$ jupyter --paths
34+
35+
# Output in MacOs, this will be different on Linux and Windows
36+
...
37+
data:
38+
/Users/<username>/Library/Jupyter
39+
/Users/<username>/miniconda3/envs/<envname>/share/jupyter
40+
/usr/local/share/jupyter
41+
/usr/local/share/jupyter
42+
/usr/share/jupyter
43+
...
44+
```
45+
46+
Convert one of the snippets to JupyterLab user-data directory (top directory in the list above):
47+
```
48+
$ npm install # only required to run once
49+
$ npm run convert jupyter_boilerplate/snippets_submenus_python/numpy.js ~/Library/Jupyter .py
50+
```
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var requirejs = require('requirejs');
2+
var fs = require('fs');
3+
var sanitizeFilename = require('sanitize-filename');
4+
5+
6+
const source = process.argv[2];
7+
const destination = process.argv[3];
8+
const extension = process.argv[4] || '';
9+
10+
requirejs.config({
11+
nodeRequire: require
12+
});
13+
require('node-define');
14+
15+
sourceData = require(source);
16+
17+
if (!fs.existsSync(destination)){
18+
fs.mkdirSync(destination);
19+
}
20+
21+
function createLevel(path, data) {
22+
if (!data.name) {
23+
return
24+
}
25+
const newPath = `${path}/${sanitizeFilename(data.name)}`;
26+
27+
if (data.snippet) {
28+
fs.writeFile(`${newPath}${extension}`, data.snippet.join('\n'), function (err) {
29+
if (err) throw err;
30+
});
31+
} else if (data['sub-menu']) {
32+
if (!fs.existsSync(newPath)){
33+
fs.mkdirSync(newPath);
34+
}
35+
data['sub-menu'].forEach(submenu => {
36+
createLevel(newPath, submenu);
37+
});
38+
}
39+
}
40+
41+
createLevel(destination, sourceData);

jupyter-boilerplate-converter/package-lock.json

+44
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "jupyter-boilerplate-converter",
3+
"version": "1.0.0",
4+
"description": "jupyter-boilerplate snippets converter",
5+
"scripts": {
6+
"convert": "node ./convert.js",
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"license": "BSD-3-Clause",
10+
"author": "QuantStack",
11+
"devDependencies": {
12+
"node-define": "^0.1.1",
13+
"requirejs": "^2.3.6",
14+
"sanitize-filename": "^1.6.3"
15+
}
16+
}

0 commit comments

Comments
 (0)