-
Notifications
You must be signed in to change notification settings - Fork 41
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
Change Black linelength defaults? #10
Comments
Hi I think that the Sorry to waste your time on this, |
Hey Don, Just curious if you have found a way to change the default parameters for black when using nb_black (especially max line length and single quotes)? I would also be interested in doing that. Thanks! |
configuring black itself doesn't work? https://black.readthedocs.io/en/stable/pyproject_toml.html |
Hi My use-case is creating blog entries from JupyterLab notebooks, where because my blog-engine (Pelican, built-texts theme, ipynb.markup plugin) has quite a narrow column width. So I just want the code in some Notebooks (but not all) formatted as narrow. For an example, see https://coolum001.github.io/jupyterblog.html I have hacked up a solution: in lab_black.py, there is a function defined My version of this now reads
When I stop being lazy, I might work out how to read parameters from the %load_ext command :) , but this works for me. Thanks again for the package |
Just to be completely accurate, the blog post referenced above was formatted with a lab_black.py locally in the same directory as the Notebook, with _format_code() defined as:
|
Note that the black configuration file works locally as well, so you can have completely different settings per folder by having that config file in there. |
Michael Didn't know that (obviously). I will give it a try, as being cleaner than having private versions of lab_black |
Hi
and a batch (command line) invocation of black works as expected, giving me the message:
However, %load_ext lab_black in a JupyterLab Notebook in the same directory, still converts single quotes to double. I suspect that the Black formatting is being done in a backend server or process, that can't see my directory to find my TOML file, because it has a different current working directory. This happens even when I run the So I might have to go back to my handcrafted lab_black.py file after all :( |
Ah that’s a pity. Maybe that could be changed in this package? It would be rather better if it worked exactly like called in a terminal.
…On Aug 8, 2019, 21:18 -0600, Don Cameron ***@***.***>, wrote:
Hi
I have a directory with a pyproject.toml file that looks like:
[tool.black]
skip-string-normalization = true
line-length = 60
and a batch (command line) invocation of black works as expected, giving me the message:
Using configuration from C:\Users\donrc\Documents\JupyterNotebooks\SymAlgebraExamplesNotebookProject\develop\pyproject.toml.
However, %load_ext lab_black in a JupyterLab Notebook in the same directory, still converts single quotes to double. I suspect that the Black formatting is being done in a backend server or process, that can't see my directory to find my TOML file, because it has a different current working directory.
This happens even when I run the jupyter lab command that started that session, from inside that directory.
So I might have to go back to my handcrafted lab_black.py file after all :(
Thanks
Don Cameron
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Hi, sorry for late reply and thank Michael for solutions so far, I will try to figure out why it cannot detect local configuration file |
I'd also love to see this extension respect I've gone the route above and manually edited the file in my Looking at the |
Here's one way: https://github.com/arokem/python-matlab-bridge/blob/master/pymatbridge/matlab_magic.py#L68 accept kwargs into The below is just a proof-of-concept patch, I don't use diff --git a/lab_black.py b/lab_black.py
index 9dbf6f4..bec76e3 100644
--- a/lab_black.py
+++ b/lab_black.py
@@ -23,10 +23,11 @@ from IPython.display import Javascript, display
__BF_SIGNATURE__ = "__BF_HIDDEN_VARIABLE_{}__"
if sys.version_info >= (3, 6, 0):
- from black import format_str, FileMode
+ import black
- def _format_code(code):
- return format_str(src_contents=code, mode=FileMode())
+ def _format_code(code, mode):
+
+ return black.format_str(src_contents=code, mode=mode)
else:
@@ -170,9 +171,10 @@ def _recover_magic_commands(cell, hidden_variables):
class BlackFormatter(object):
- def __init__(self, ip, is_lab):
+ def __init__(self, ip, is_lab, mode):
self.shell = ip
self.is_lab = is_lab
+ self.mode = mode
def __set_cell(self, unformatted_cell, cell, cell_id=None):
if self.is_lab:
@@ -215,7 +217,7 @@ class BlackFormatter(object):
# Transform magic commands into special variables
cell = _transform_magic_commands(unformatted_cell, hidden_variables)
- formatted_code = _format_code(cell)
+ formatted_code = _format_code(cell, self.mode)
# Recover magic commands
formatted_code = _recover_magic_commands(
@@ -230,10 +232,14 @@ class BlackFormatter(object):
black_formatter = None
-def load_ipython_extension(ip):
+def load_ipython_extension(ip, line_length=79):
global black_formatter
+ mode = black.FileMode(
+ target_versions={black.TargetVersion.PY38}, line_length=line_length,
+ )
+
if black_formatter is None:
- black_formatter = BlackFormatter(ip, is_lab=True)
+ black_formatter = BlackFormatter(ip, is_lab=True, mode=mode)
ip.events.register("post_run_cell", black_formatter.format_cell) |
@n8henrie 's solution is best if you may ned to configure the line length. Shorter way, if you will only ever need to change the line length rarely or never: replace Lines 26 to 29 in d4bd99e
with
setting |
Work seems to have slowed down, so I created a fork that addresses this issue and adds a few other features: https://github.com/n8henrie/jupyter-black |
Hi
First of all, thank you for this extension.
I plan to use nb_black and Jupyter Notebooks to generate blog posts, where code blocks are indented. This means I would like to set the line length to 65 (ie not the default).
I would also like to preserve single quote strings (a black option).
Can you indicate where in your code it might be possible to change the default parameters for black?
Second:
I am running Jupyter lab Version 0.35.3, in an environment as below
You will notice that Jupyterlab displays after each cell execution (I assume this is unintended behaviour)
I am running nb_black as below
Now I can't reproduce this behaviour in smaller Notebooks, so you may see fit to ignore this until I can :)
Third, invoking
%load_ext lab_black
seems to stick with the notebook. Even if you reload the notebook and delete the %load_ext cell, the black formatting is still applied to new cells. You may wish to mention that you turn it off by a %unload_ext lab_black call.Happy to supply more info if needed
Don Cameron
The text was updated successfully, but these errors were encountered: