Skip to content

Commit 0a24d76

Browse files
authored
Merge pull request #1019 from jfbercher/code_prettify
[code_prettify] added support for IPython magics, cf #1018
2 parents 51c04d3 + cf7562f commit 0a24d76

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

src/jupyter_contrib_nbextensions/nbextensions/code_prettify/2to3.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Name: 2to3 Converter
33
Description: Converts python2 code in a notebook's code cell to python3 code
44
Link: README_2to3.md
55
Main: 2to3.js
6-
Compatibility: Jupyter (4.x)
6+
Compatibility: Jupyter 4.x, 5.x
77
Parameters:
88

99
- name: 2to3.add_toolbar_button

src/jupyter_contrib_nbextensions/nbextensions/code_prettify/autopep8.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Name: Autopep8
33
Description: Use kernel-specific code to reformat/prettify the contents of code cells
44
Link: README_autopep8.md
55
Main: autopep8.js
6-
Compatibility: Jupyter (4.x)
6+
Compatibility: Jupyter 4.x, 5.x
77
Parameters:
88

99
- name: autopep8.add_toolbar_button

src/jupyter_contrib_nbextensions/nbextensions/code_prettify/code_prettify.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@ define(function(require, exports, module) {
2525

2626
cfg.kernel_config_map = { // map of parameters for supported kernels
2727
"python": {
28-
"library": "import json\nimport yapf.yapflib.yapf_api",
29-
"prefix": "print(json.dumps(yapf.yapflib.yapf_api.FormatCode(u",
30-
"postfix": ")[0]))"
28+
"library": ["import json",
29+
"def yapf_reformat(cell_text):",
30+
" import yapf.yapflib.yapf_api",
31+
" import re",
32+
" cell_text = re.sub('^%', '#%#', cell_text, flags=re.M)",
33+
" reformated_text = yapf.yapflib.yapf_api.FormatCode(cell_text)[0]",
34+
" return re.sub('^#%#', '%', reformated_text, flags=re.M)"].join("\n"),
35+
"prefix": "print(json.dumps(yapf_reformat(u",
36+
"postfix": ")))"
3137
},
3238
"r": {
3339
"library": "library(formatR)\nlibrary(jsonlite)",

src/jupyter_contrib_nbextensions/nbextensions/code_prettify/code_prettify.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Name: Code prettify
33
Description: Use kernel-specific code to reformat/prettify the contents of code cells
44
Link: README_code_prettify.md
55
Main: code_prettify.js
6-
Compatibility: Jupyter (4.x)
6+
Compatibility: Jupyter 4.x, 5.x
77
Parameters:
88

99
- name: code_prettify.add_toolbar_button
@@ -55,7 +55,7 @@ Parameters:
5555
default: |
5656
{
5757
"python": {
58-
"library": "import json\nimport yapf.yapflib.yapf_api",
58+
"library": "import json\ndef yapf_reformat(cell_text):\n import yapf.yapflib.yapf_api\n import re\n cell_text = re.sub('^%', '#%#', cell_text, flags=re.M)\n reformated_text = yapf.yapflib.yapf_api.FormatCode(cell_text)[0]\n return re.sub('^#%#', '%', reformated_text, flags=re.M)",
5959
"prefix": "print(json.dumps(yapf.yapflib.yapf_api.FormatCode(u",
6060
"postfix": ")[0]))"
6161
},

0 commit comments

Comments
 (0)