Skip to content
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

Linting #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 88
23 changes: 13 additions & 10 deletions lab_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def transform(self, lines):
hidden_variables.append(rhs)
call = __BF_SIGNATURE__.format(len(hidden_variables) - 1)
new_line = lhs + call + "\n"
lines_after = lines[end_line + 1 :]
lines_after = lines[end_line + 1:]

return lines_before + [new_line] + lines_after

Expand All @@ -86,7 +86,7 @@ def transform(self, lines):
hidden_variables.append(rhs)
call = __BF_SIGNATURE__.format(len(hidden_variables) - 1)
new_line = lhs + call + "\n"
lines_after = lines[end_line + 1 :]
lines_after = lines[end_line + 1:]

return lines_before + [new_line] + lines_after

Expand Down Expand Up @@ -114,7 +114,7 @@ def transform(self, lines):

lines_before = lines[:start_line]
new_line = indent + call + "\n"
lines_after = lines[end_line + 1 :]
lines_after = lines[end_line + 1:]

return lines_before + [new_line] + lines_after

Expand All @@ -123,10 +123,10 @@ def transform(self, lines):
# https://github.com/ipython/ipython/blob/1879ed27bb0ec3be5fee499ac177ad14a9ef7cfd/IPython/core/inputtransformer2.py#L439
"""Transform a help command found by the ``find()`` classmethod.
"""
piece = "".join(lines[self.start_line : self.q_line + 1])
indent, content = piece[: self.start_col], piece[self.start_col :]
piece = "".join(lines[self.start_line: self.q_line + 1])
indent, content = piece[: self.start_col], piece[self.start_col:]
lines_before = lines[: self.start_line]
lines_after = lines[self.q_line + 1 :]
lines_after = lines[self.q_line + 1:]

m = _help_end_re.search(content)
if not m:
Expand Down Expand Up @@ -179,15 +179,17 @@ def __set_cell(self, unformatted_cell, cell, cell_id=None):
self.shell.set_next_input(cell, replace=True)
else:
js_code = """
setTimeout(function() {
setTimeout(function writeToCell () {

var nbb_cell_id = %d;
var nbb_unformatted_code = %s;
var nbb_formatted_code = %s;
var nbb_cells = Jupyter.notebook.get_cells();

for (var i = 0; i < nbb_cells.length; ++i) {
if (nbb_cells[i].input_prompt_number == nbb_cell_id) {
if (nbb_cells[i].get_text() == nbb_unformatted_code) {
nbb_cells[i].set_text(nbb_formatted_code);
if (nbb_cells[i].input_prompt_number === nbb_cell_id) {
if (nbb_cells[i].get_text() === nbb_unformatted_code) {
nbb_cells[i].set_text(nbb_formatted_code);
}
break;
}
Expand All @@ -202,6 +204,7 @@ def __set_cell(self, unformatted_cell, cell, cell_id=None):
display(Javascript(js_code))

def format_cell(self, *args, **kwargs):
print("formatting")
try:
cell_id = len(self.shell.user_ns["In"]) - 1
if cell_id > 0:
Expand Down
11 changes: 10 additions & 1 deletion nb_black.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals

from lab_black import BlackFormatter, black_formatter, unload_ipython_extension
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you remove unload_ipython_extension, I guess notebook users are not able to unload the extension

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, true but I'm not sure unloading works as it should anyway.

If you clone my reload branch and run the Reload.ipynb, it looks as though format_cell() gets called once for each time you have loaded the extension and cells still get formatted after calling %unload_ext.

We could have a separate unload function in nb_black.py (which is what I have suggested in the latest commit in this branch).

from lab_black import BlackFormatter

black_formatter = None


def load_ipython_extension(ip):
global black_formatter
if black_formatter is None:
black_formatter = BlackFormatter(ip, is_lab=False)
ip.events.register("post_run_cell", black_formatter.format_cell)


def unload_ipython_extension(ip):
global black_formatter
if black_formatter:
ip.events.unregister("post_run_cell", black_formatter.format_cell)
black_formatter = None
9 changes: 7 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ def readme(file_name):
setup(
name="nb_black",
version="1.0.7",
description="A simple extension for Jupyter Notebook and Jupyter Lab to beautify Python code automatically using Black.",
description="A simple extension for Jupyter Notebook and Jupyter Lab "
"to beautify Python code automatically using Black.",
long_description=readme(file_name="README.md"),
keywords="black-formatter black-beautifier black jupyterlab-extension jupyter-notebook-extension",
keywords="black-formatter "
"black-beautifier "
"black "
"jupyterlab-extension "
"jupyter-notebook-extension",
url="https://github.com/dnanhkhoa/nb_black",
author="Khoa Duong",
author_email="dnanhkhoa@live.com",
Expand Down