Skip to content

Commit

Permalink
T260: Add a pre-commit hook for cleaning ipynb files (#260)
Browse files Browse the repository at this point in the history
* Add a pre-commit hook for cleaning ipynb files

Add a Python script that cleans ipynb files by resetting execution
counts, removing outputs, and removing metadata.  Add the script as a
pre-commit hook.

This commit also contains the results of running the hook on all the
existing ipynb files in the repository.

* Include path name when cleanipynb.py fails

* Improve cleanipynb hooks

Extend cleanipynb with an argument that allows leaving output cells
as-is, and pass this argument when processing notebook files that are
immediate descendents of the notebooks/ folder so that example outputs
are retained but metadata are removed.  Another hook runs for notebooks
in the notebooks/helpers/ folder so that both outputs and metadata are
removed.

* Incorporate feedback

1. Reset execution counts to "null" when removing outputs and to an
   increasing integer series starting from one when leaving outputs.
2. Checkout all example notebooks (notebooks\*.ipynb) from develop to
   make sure no other changes beyond the final cleanipynb behavior were
   made.
3. Fix a bug in .pre-commit-config.yml where both cleanipynb hooks would
   be run for files in the notebooks\helpers\ folder.

* Fix documentation build

Link to the docs for the currently-running version of Python during the
doc build.
  • Loading branch information
isaacwaldron authored May 11, 2023
1 parent 7f9a44e commit 2967f11
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 216 deletions.
15 changes: 15 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,18 @@ repos:
^src/ansys/api/|
^protos
)
- repo: local
hooks:
- id: cleanipynb-helpers
name: cleanipynb-helpers
language: system
entry: python scripts/cleanipynb.py
files: notebooks/helpers/[^/]*\.ipynb$

- id: cleanipynb
name: cleanipynb
language: system
entry: python scripts/cleanipynb.py
args: [--leave-outputs]
files: notebooks/[^/]*\.ipynb$
3 changes: 2 additions & 1 deletion doc/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Sphinx documentation configuration file."""
from datetime import datetime
import os
import sys

from ansys_sphinx_theme import ansys_favicon, get_version_match, pyansys_logo_black

Expand Down Expand Up @@ -56,7 +57,7 @@

# Intersphinx mapping
intersphinx_mapping = {
"python": ("https://docs.python.org/dev", None),
"python": (f"https://docs.python.org/{'.'.join((str(i) for i in sys.version_info[:2]))}", None),
# kept here as an example
# "scipy": ("https://docs.scipy.org/doc/scipy/reference", None),
# "numpy": ("https://numpy.org/devdocs", None),
Expand Down
22 changes: 2 additions & 20 deletions notebooks/3d_comp_placement.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,7 @@
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
}
22 changes: 2 additions & 20 deletions notebooks/Cutout_region.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -248,25 +248,7 @@
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
}
26 changes: 3 additions & 23 deletions notebooks/Define_and_assign_bondwire_profiles.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
"cell_type": "code",
"execution_count": 1,
"id": "87bc6dac",
"metadata": {
"scrolled": true
},
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -298,25 +296,7 @@
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
}
26 changes: 3 additions & 23 deletions notebooks/Move_geometry_from_existing_layer_to_new_layer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
"cell_type": "code",
"execution_count": 1,
"id": "87bc6dac",
"metadata": {
"scrolled": true
},
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -177,25 +175,7 @@
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
}
26 changes: 3 additions & 23 deletions notebooks/PolygonOperations.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"cell_type": "code",
"execution_count": 1,
"id": "cd7e721c",
"metadata": {
"scrolled": true
},
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -113,25 +111,7 @@
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
}
26 changes: 3 additions & 23 deletions notebooks/Remove_all_geometry_and_layers_below_L2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
"cell_type": "code",
"execution_count": 1,
"id": "87bc6dac",
"metadata": {
"scrolled": true
},
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand Down Expand Up @@ -263,25 +261,7 @@
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
}
22 changes: 2 additions & 20 deletions notebooks/helpers/example_setup.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,7 @@
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
}
22 changes: 2 additions & 20 deletions notebooks/helpers/example_teardown.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,7 @@
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
}
22 changes: 2 additions & 20 deletions notebooks/helpers/plot.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,7 @@
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
}
28 changes: 5 additions & 23 deletions notebooks/template.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "cd7e721c",
"metadata": {},
"outputs": [],
Expand All @@ -12,7 +12,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "abd5a80d",
"metadata": {},
"outputs": [],
Expand All @@ -22,7 +22,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "3290a8f6",
"metadata": {},
"outputs": [],
Expand All @@ -31,25 +31,7 @@
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
}
Loading

0 comments on commit 2967f11

Please sign in to comment.