Skip to content
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

[isort] First implementation based on code_prettify #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions README_isort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Sort imports using isort

This nbextension sorts imports in notebook code cells.

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).

The nbextension provides

- a toolbar button (configurable to be added or not)

**pre-requisites:** of course, you must have the corresponding package installed:

```
pip install isort [--user]
```

## Options

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:

- `isort.add_toolbar_button`: Whether to add a toolbar button to transform the selected cell(s). Defaults to `true`.

- `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`.

- `isort.show_alerts_for_errors`: Whether to show alerts for errors in the kernel calls. Defaults to `false`.

- `isort.button_label`: Toolbar button label text. Also used in the actions' help text. Defaults to `Sort imports with isort`.

- `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.

## Internals

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.

See the [shared README](REAME.md) and [kerneexeconcells library](kernel_exec_on_cell.js) for the internal model used by the nbextension.
43 changes: 43 additions & 0 deletions isort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Jupyter-Contrib Team.
// Distributed under the terms of the Modified BSD License.
// Authors: @benjaminabel
// Based on: code_prettify extension

define(['./kernel_exec_on_cell'], function(kernel_exec_on_cell) {
'use strict';

var mod_name = 'isort';

// gives default settings
var cfg = {
add_toolbar_button: true,
register_hotkey: false,
show_alerts_for_errors: false,
button_icon: 'fa-sort',
button_label: 'Sort imports with isort',
kbd_shortcut_text: 'Sort imports in' // ' current cell(s)'
};

cfg.kernel_config_map = { // map of parameters for supported kernels
"python": {
"library": [
"import isort",
"import json",
"def _isort_refactor_cell(src):",
" try:",
" tree = isort.SortImports(file_contents=src).output",
" except Exception:",
" return src ",
" else:",
" return str(tree)[:-1]",
].join('\n'),
"prefix": "print(json.dumps(_isort_refactor_cell(u",
"postfix": ")))"
}
};

var converter = new kernel_exec_on_cell.define_plugin(mod_name, cfg);
converter.load_ipython_extension = converter.initialize_plugin;
return converter;

});
40 changes: 40 additions & 0 deletions isort.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Type: Jupyter Notebook Extension
Name: isort formatter
Description: Sort imports in python files using isort
Link: README_isort.md
Main: isort.js
Compatibility: Jupyter 4.x, 5.x
Parameters:

- name: isort.add_toolbar_button
description: Add a toolbar button to convert the selected cell(s)
input_type: checkbox
default: true

- name: isort.button_icon
description: |
Toolbar button icon: afont-awesome class defining the icon used for the
toolbar button. See http://fontawesome.io/icons/ for available icons.
input_type: text
default: 'fa-sort'

- name: isort.button_label
description: Toolbar button label text
input_type: text
default: 'Sort imports with isort'

- name: isort.kernel_config_map_json
description: |
kernel_config_map_json:
json defining library calls required to load the kernel-specific
converting modules, and the prefix & postfix for the json-format string
required to make the converting call.
input_type: textarea
default: |
{
"python": {
"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]",
"prefix": "print(json.dumps(_isort_refactor_cell(u",
"postfix": ")))"
}
}