Skip to content

Commit

Permalink
Write script and make it executable (#9)
Browse files Browse the repository at this point in the history
* write script

* rename script

* Make blackbook script executable.
  • Loading branch information
daffidwilde authored and Nikoleta-v3 committed Jan 25, 2019
1 parent 99ee894 commit 863dd17
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
url="",
license="The MIT License (MIT)",
description="`Black` for Jupyter notebooks.",
entry_points={"console_scripts": "blackbook=blackbook.__main__:main"},
)
29 changes: 28 additions & 1 deletion src/blackbook/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
from .main import gen_notebook_files_in_dir, format_notebook_content
""""""

from typing import Iterator

import re
import pathlib
import json

import black


def gen_notebook_files_in_dir(path: pathlib.Path) -> Iterator[pathlib.Path]:

return path.glob("**/*.ipynb")

def format_notebook_content(path: pathlib.Path) -> dict:
content = path.read_text()
nb = json.loads(content)

for cell in nb["cells"]:
try:
string = "".join(cell["source"])
formatted_string = black.format_str(string, line_length=black.DEFAULT_LINE_LENGTH)
cell["source"] = [s + "\n" for s in formatted_string.split("\n")][:-1]
except black.InvalidInput:
pass

return nb
18 changes: 18 additions & 0 deletions src/blackbook/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import json
import pathlib
import sys

import blackbook


def main(path=None):
if path is None:
path = pathlib.Path(sys.argv[1])

for notebook_path in blackbook.gen_notebook_files_in_dir(path):
nb = blackbook.format_notebook_content(notebook_path)

notebook_path.write_text(json.dumps(nb))

if __name__ == '__main__':
main()
28 changes: 0 additions & 28 deletions src/blackbook/main.py

This file was deleted.

0 comments on commit 863dd17

Please sign in to comment.