-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write script and make it executable (#9)
* write script * rename script * Make blackbook script executable.
- Loading branch information
1 parent
99ee894
commit 863dd17
Showing
4 changed files
with
47 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file was deleted.
Oops, something went wrong.