From d0e89331b599518fb193af8ab9a9969c70bfc56b Mon Sep 17 00:00:00 2001 From: Benjamin Abel Date: Sun, 21 Jan 2018 23:36:55 +0100 Subject: [PATCH] [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. --- README_isort.md | 35 +++++++++++++++++++++++++++++++++++ isort.js | 43 +++++++++++++++++++++++++++++++++++++++++++ isort.yaml | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 README_isort.md create mode 100644 isort.js create mode 100644 isort.yaml diff --git a/README_isort.md b/README_isort.md new file mode 100644 index 0000000..7aa9bec --- /dev/null +++ b/README_isort.md @@ -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. diff --git a/isort.js b/isort.js new file mode 100644 index 0000000..a9d71ba --- /dev/null +++ b/isort.js @@ -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; + + }); diff --git a/isort.yaml b/isort.yaml new file mode 100644 index 0000000..da0ff47 --- /dev/null +++ b/isort.yaml @@ -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": ")))" + } + }