diff --git a/README_autopep8.md b/README_autopep8.md new file mode 100644 index 0000000..08b210c --- /dev/null +++ b/README_autopep8.md @@ -0,0 +1,27 @@ +Python code prettifying unsing autopep8 +======================================= + +This extension reformats/prettifies code in a notebook's code cell, uniquely for python language, using the autopep8 package. + +Under the hood, it uses the KerneExecOnCells library, shared with `code_prettify`and `2to3` +The nbextension provides + +- a toolbar button (configurable to be added or not) +- a keyboard shortcut for reformatting the current code-cell (default shortcut + is `Alt-A`, can also be configured not to add the keyboard shortcut). +- a keyboard shortcut for reformatting the whole notebook (default shortcut + is `Alt-Shift-A`, can also be configured not to add the keyboard shortcut). +Syntax shall be correct. The nbextension will also point basic syntax errors. + +prerequisites +------------- + +Of course, you must have the necessary kernel-specific packages installed for +the prettifier call to work: + +- for the default python implementation, the + [autopep8](https://github.com/hhatto/autopep8) module is required: + + pip install autopep8 + + Others you might consider using include [autopep8](https://github.com/hhatto/autopep8). \ No newline at end of file diff --git a/autopep8.js b/autopep8.js new file mode 100644 index 0000000..761b656 --- /dev/null +++ b/autopep8.js @@ -0,0 +1,38 @@ +// Copyright (c) Jupyter-Contrib Team. +// Distributed under the terms of the Modified BSD License. +// Authors: @kenkoooo, @jfbercher and @jcb91 + +define(function(require, exports, module) { + 'use strict'; + + var kernel_exec_on_cell = require('./kernel_exec_on_cell'); + + var mod_name = 'autopep8'; + + // gives default settings + var cfg = { + add_toolbar_button: true, + hotkeys: { + process_selected: 'Alt-A', + process_all: 'Alt-Shift-A', + }, + register_hotkey: true, + show_alerts_for_errors: true, + button_icon: 'fa-cog', + button_label: 'Prettify (using autopep8)', + kbd_shortcut_text: 'Prettify' // ' current cell(s)' + }; + + cfg.kernel_config_map = { // map of parameters for supported kernels + "python": { + "library": "import json\nimport autopep8", + "prefix": "print(json.dumps(autopep8.fix_code(u", + "postfix": ")))" + } + }; + + var prettifier = new kernel_exec_on_cell.define_plugin(mod_name, cfg); + prettifier.load_ipython_extension = prettifier.initialize_plugin; + return prettifier; + + }); diff --git a/autopep8.yaml b/autopep8.yaml new file mode 100644 index 0000000..62d0827 --- /dev/null +++ b/autopep8.yaml @@ -0,0 +1,62 @@ +Type: Jupyter Notebook Extension +Name: Autopep8 +Description: Use kernel-specific code to reformat/prettify the contents of code cells +Link: README_autopep8.md +Main: autopep8.js +Compatibility: Jupyter (4.x) +Parameters: + +- name: autopep8.add_toolbar_button + description: Add a toolbar button to prettify the selected cell(s) + input_type: checkbox + default: true + +- name: autopep8.button_icon + description: | + Toolbar button icon: afont-awesome class defining the icon used for the + toolbar button and actions. + See http://fontawesome.io/icons/ for available icons. + input_type: text + default: 'fa-cog' + +- name: autopep8.button_label + description: Toolbar button label text + input_type: text + default: 'Code prettify' + +- name: autopep8.register_hotkey + description: | + Register hotkeys to prettify the selected code cell(s), or all code cells + in the notebook + input_type: checkbox + default: true + +- name: autopep8.hotkeys.process_selected + description: Hotkey to use to prettify the selected cell(s) + input_type: hotkey + default: 'Alt-A' + +- name: autopep8.hotkeys.process_all + description: Hotkey to use to prettify the whole notebook + input_type: hotkey + default: 'Alt-Shift-A' + +- name: autopep8.show_alerts_for_errors + description: Show alerts for errors in the kernel prettifying calls + input_type: checkbox + default: true + +- name: autopep8.kernel_config_map_json + description: | + json object defining library calls required to load the kernel-specific + prettifying modules, and the prefix & postfix for the json-format string + required to make the prettifying call. + input_type: json_object + default: | + { + "python": { + "library": "import json\nimport autopep8", + "prefix": "print(json.dumps(autopep8.fix_code(u", + "postfix": ")))" + } + }