Skip to content

Commit

Permalink
refactor: rename fix-first-nbcell to set-nb-cells (#49)
Browse files Browse the repository at this point in the history
* chore: put install cell before config cell
* feat: add IPython.display import statement
  • Loading branch information
redeboer authored Feb 6, 2022
1 parent dbe7bea commit 80d042f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
27 changes: 13 additions & 14 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,6 @@
always_run: true
pass_filenames: false

- id: fix-first-nbcell
name: Check and fix first cells in notebooks
description: >
Add or replace the first cell in a Jupyter notebook by adding install
statements and IPython configurations.
entry: fix-first-nbcell
exclude: >
(?x)^(
docs/adr/.*
)$
language: python
types:
- jupyter

- id: fix-nbformat-version
name: Set nbformat minor version to 4 and remove cell IDs
entry: fix-nbformat-version
Expand All @@ -41,3 +27,16 @@
language: python
types:
- jupyter

- id: set-nb-cells
name: Add or update default cells in a Jupyter notebook
description: >
Add or replace certain default cells in a Jupyter notebook.
entry: set-nb-cells
exclude: >
(?x)^(
docs/adr/.*
)$
language: python
types:
- jupyter
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ repos:
rev: ""
hooks:
- id: check-dev-files
- id: fix-first-nbcell
- id: fix-nbformat-version
- id: format-setup-cfg
- id: pin-nb-requirements
- id: set-nb-cells
```
then run `pre-commit autoupdate`. This example lists all available hooks
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ dev =
[options.entry_points]
console_scripts =
check-dev-files = repoma.check_dev_files:main
fix-first-nbcell = repoma.fix_first_nbcell:main
fix-nbformat-version = repoma.fix_nbformat_version:main
format-setup-cfg = repoma.format_setup_cfg:main
pin-nb-requirements = repoma.pin_nb_requirements:main
set-nb-cells = repoma.set_nb_cells:main

[options.packages.find]
where = src
Expand Down
38 changes: 27 additions & 11 deletions src/repoma/fix_first_nbcell.py → src/repoma/set_nb_cells.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Add install statements to first cell in a Jupyter notebook.
"""Add or update standard cells in a Jupyter notebook.
Notebook servers like Google Colaboratory and Deepnote do not install a package
automatically, so this has to be done through a code cell. At the same time,
Expand All @@ -10,11 +10,12 @@
Notebooks can be ignored by making the first cell a `Markdown cell
<https://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Working%20With%20Markdown%20Cells.html>`_
and starting its content with:
and writing the following `Markdown comment
<https://www.markdownguide.org/hacks/#comments>`_:
.. code-block:: markdown
<!-- ignore first cell -->
<!-- no-set-nb-cells -->
"""

import argparse
Expand Down Expand Up @@ -88,15 +89,15 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
),
type=str,
)
parser.add_argument(
"--no-config-cell",
action="store_true",
help="Do not add configuration cell.",
)
args = parser.parse_args(argv)

for filename in args.filenames:
_update_cell(
filename,
new_content=__CONFIG_CELL_CONTENT.strip("\n"),
new_metadata=__CONFIG_CELL_METADATA,
cell_id=0,
)
cell_id = 0
if args.add_install_cell:
cell_content = __INSTALL_CELL_CONTENT.strip("\n")
if args.extras_require:
Expand All @@ -111,7 +112,22 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
filename,
new_content=cell_content,
new_metadata=__INSTALL_CELL_METADATA,
cell_id=1,
cell_id=cell_id,
)
cell_id += 1
if not args.no_config_cell:
config_cell_content = __CONFIG_CELL_CONTENT
if "ipython" in args.additional_packages.lower():
config_cell_content = config_cell_content.replace(
"import os",
"import os\n\nfrom IPython.display import display # noqa:"
" F401",
)
_update_cell(
filename,
new_content=config_cell_content.strip("\n"),
new_metadata=__CONFIG_CELL_METADATA,
cell_id=cell_id,
)
_insert_autolink_concat(filename)
return 0
Expand Down Expand Up @@ -166,7 +182,7 @@ def _insert_autolink_concat(filename: str) -> None:


def _skip_notebook(
filename: str, ignore_statement: str = "<!-- ignore first cell -->"
filename: str, ignore_statement: str = "<!-- no-set-nb-cells -->"
) -> bool:
notebook = nbformat.read(filename, as_version=nbformat.NO_CONVERT)
for cell in notebook["cells"]:
Expand Down

0 comments on commit 80d042f

Please sign in to comment.