Skip to content

Commit cb6bc81

Browse files
author
Benjamin Abel
committed
[isort] First implementation based on code_prettify
Actually I didn't add keyboard shortcuts because I can't find any combination that works on my machine.
1 parent e8d66d2 commit cb6bc81

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Sort imports using isort
2+
3+
This nbextension sorts imports in notebook code cells.
4+
5+
Under the hood, it uses a call to the current notebook kernel to reformat the code. The conversion run by the kernel uses Python's package [isort](https://github.com/timothycrosley/isort) by [Timothy Edmund Crosley](https://github.com/timothycrosley).
6+
7+
The nbextension provides
8+
9+
- a toolbar button (configurable to be added or not)
10+
11+
**pre-requisites:** of course, you must have the corresponding package installed:
12+
13+
```
14+
pip install isort [--user]
15+
```
16+
17+
## Options
18+
19+
All options are provided by the [KerneExecOnCells library](kernel_exec_on_cell.js). There are a few nbextension-wide options, configurable using the [jupyter_nbextensions_configurator](https://github.com/Jupyter-contrib/jupyter_nbextensions_configurator) or by editing the `notebook` section config file directly. The options are as follows:
20+
21+
- `isort.add_toolbar_button`: Whether to add a toolbar button to transform the selected cell(s). Defaults to `true`.
22+
23+
- `isort.button_icon`: A font-awesome class defining the icon used for the toolbar button and actions. See [fontawesome.io/icons] for available icon classes. Defaults to `fa-sort`.
24+
25+
- `isort.show_alerts_for_errors`: Whether to show alerts for errors in the kernel calls. Defaults to `false`.
26+
27+
- `isort.button_label`: Toolbar button label text. Also used in the actions' help text. Defaults to `Sort imports with isort`.
28+
29+
- `isort.kernel_config_map_json`: The value of this key is a string which can be parsed into a json object giving the config for each kernel language.
30+
31+
## Internals
32+
33+
Under the hood, this nbextension uses the [kerneexeconcells library](kernel_exec_on_cell.js), a shared library for creating Jupyter nbextensions which transform code cell text using calls to the active kernel.
34+
35+
See the [shared README](REAME.md) and [kerneexeconcells library](kernel_exec_on_cell.js) for the internal model used by the nbextension.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Jupyter-Contrib Team.
2+
// Distributed under the terms of the Modified BSD License.
3+
// Authors: @benjaminabel
4+
// Based on: code_prettify extension
5+
6+
define(['./kernel_exec_on_cell'], function(kernel_exec_on_cell) {
7+
'use strict';
8+
9+
var mod_name = 'isort';
10+
11+
// gives default settings
12+
var cfg = {
13+
add_toolbar_button: true,
14+
register_hotkey: false,
15+
show_alerts_for_errors: false,
16+
button_icon: 'fa-sort',
17+
button_label: 'Sort imports with isort',
18+
kbd_shortcut_text: 'Sort imports in' // ' current cell(s)'
19+
};
20+
21+
cfg.kernel_config_map = { // map of parameters for supported kernels
22+
"python": {
23+
"library": [
24+
"import isort",
25+
"import json",
26+
"def _isort_refactor_cell(src):",
27+
" try:",
28+
" tree = isort.SortImports(file_contents=src).output",
29+
" except Exception:",
30+
" return src ",
31+
" else:",
32+
" return str(tree)[:-1]",
33+
].join('\n'),
34+
"prefix": "print(json.dumps(_isort_refactor_cell(u",
35+
"postfix": ")))"
36+
}
37+
};
38+
39+
var converter = new kernel_exec_on_cell.define_plugin(mod_name, cfg);
40+
converter.load_ipython_extension = converter.initialize_plugin;
41+
return converter;
42+
43+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Type: Jupyter Notebook Extension
2+
Name: isort formatter
3+
Description: Sort imports in python files using isort
4+
Link: README_isort.md
5+
Main: isort.js
6+
Compatibility: Jupyter 4.x, 5.x
7+
Parameters:
8+
9+
- name: isort.add_toolbar_button
10+
description: Add a toolbar button to convert the selected cell(s)
11+
input_type: checkbox
12+
default: true
13+
14+
- name: isort.button_icon
15+
description: |
16+
Toolbar button icon: afont-awesome class defining the icon used for the
17+
toolbar button. See http://fontawesome.io/icons/ for available icons.
18+
input_type: text
19+
default: 'fa-sort'
20+
21+
- name: isort.button_label
22+
description: Toolbar button label text
23+
input_type: text
24+
default: 'Sort imports with isort'
25+
26+
- name: isort.kernel_config_map_json
27+
description: |
28+
kernel_config_map_json:
29+
json defining library calls required to load the kernel-specific
30+
converting modules, and the prefix & postfix for the json-format string
31+
required to make the converting call.
32+
input_type: textarea
33+
default: |
34+
{
35+
"python": {
36+
"library": "import json, isort\ndef _isort_refactor_cell(src):\n try:\n tree = isort.SortImports(file_contents=src).output\n except Exception:\n return src \n else:\n return str(tree)[:-1]",
37+
"prefix": "print(json.dumps(_isort_refactor_cell(u",
38+
"postfix": ")))"
39+
}
40+
}

0 commit comments

Comments
 (0)