Skip to content

Commit 2422b76

Browse files
authored
Merge pull request #1376 from artificialsoph/patch-1
Configure `code_prettify` extension to load custom yapf styles by default.
2 parents a64846d + 77f5e24 commit 2422b76

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/jupyter_contrib_nbextensions/nbextensions/code_prettify/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,20 @@ to use [autopep8] instead of [yapf] to reformat the python code.
239239
But, if you want two alternative prettifiers available for the same kernel
240240
language, we need to define separate plugins.
241241

242+
Custom Yapf Styles
243+
------------------
244+
245+
Using the default `yapf` engine, one may define a custom formatting style according to the [package documentation](https://github.com/google/yapf#formatting-style).
246+
247+
The `code_prettify` extension is configured to follow the default `yapf` ordering (minus the command line option) and will search for the formatting style in the following manner:
248+
249+
> 1. In the [style] section of a .style.yapf file in either the current directory or one of its parent directories.
250+
> 2. In the [yapf] section of a setup.cfg file in either the current directory or one of its parent directories.
251+
> 3. In the ~/.config/yapf/style file in your home directory.
252+
>
253+
> If none of those files are found, the default style is used (PEP8).
254+
255+
This means that one can set up a globa custom yapf style using `~/.config/yapf/style` or a project-specific one using the project directory.
242256

243257
History
244258
-------
@@ -267,6 +281,9 @@ History
267281
- [@jfbercher], january 2017
268282
- updated documentation
269283
- added autopep8 nbextension as a plugin using the shared library
284+
- [@artificialsoph], Jan 2018
285+
- updated documentation
286+
- changed default behavior to load custom yapf styles
270287

271288

272289
[2to3]: README_2to3.md

src/jupyter_contrib_nbextensions/nbextensions/code_prettify/code_prettify.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ define(['./kernel_exec_on_cell'], function(kernel_exec_on_cell) {
2626
"library": ["import json",
2727
"def yapf_reformat(cell_text):",
2828
" import yapf.yapflib.yapf_api",
29+
" from yapf import file_resources",
30+
" import os",
2931
" import re",
32+
" style_config = file_resources.GetDefaultStyleForDir(os.getcwd())",
3033
" cell_text = re.sub('^%', '#%#', cell_text, flags=re.M)",
31-
" reformated_text = yapf.yapflib.yapf_api.FormatCode(cell_text)[0]",
34+
" reformated_text = yapf.yapflib.yapf_api.FormatCode(cell_text, style_config=style_config)[0]",
3235
" return re.sub('^#%#', '%', reformated_text, flags=re.M)"].join("\n"),
3336
"prefix": "print(json.dumps(yapf_reformat(u",
3437
"postfix": ")))"

0 commit comments

Comments
 (0)