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

Add pre-commit hooks to this repo #4

Closed
wants to merge 13 commits into from
Prev Previous commit
Next Next commit
added --all argument
  • Loading branch information
anakin87 committed Sep 19, 2022
commit 40e6e50d5192fdbc2b8b19bc74ed32437ddbc1a4
5 changes: 1 addition & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,4 @@ repos:
entry: python scripts/generate_markdowns.py
language: system
types: [jupyter]

- repo: meta
hooks:
- id: identity

2 changes: 1 addition & 1 deletion Contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Here's what you need to do to add or edit tutorials 👇:
2. If you're creating a new tutorial, follow the [naming convention](#naming-convention-for-file-names) for file names.
3. Edit an existing tutorial or create a new one in the `/tutorials` folder by editing or creating `.ipynb` files.
4. Pre-commit hooks will ensure the `markdowns` folder reflects your changes but you can update the docs at any time:
- Run `python /scripts/generate_markdowns.py`. This generates or updates the relevant markdown file in `/markdowns`.
- Run `python /scripts/generate_markdowns.py --all`. This generates or updates the relevant markdown file in `/markdowns`.
5. Create a pull request.
6. Wait for the [CI](#ci-continuous-integration) checks to pass.
These checks pass if the relevant markdown files are created.
Expand Down
19 changes: 12 additions & 7 deletions scripts/generate_markdowns.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import argparse
import sys
import glob
from pathlib import Path
from typing import Sequence

Expand Down Expand Up @@ -156,9 +157,7 @@
}

notebook_tutorials_dir = Path(__file__).parent.parent / "tutorials"
print(notebook_tutorials_dir)
markdown_tutorials_dir = Path(__file__).parent.parent / "markdowns"
print(markdown_tutorials_dir)

md_exporter = MarkdownExporter(exclude_output=True)

Expand All @@ -183,16 +182,22 @@ def generate_markdown_from_notebook(nb_path: str):


def main(argv: Sequence[str] = sys.argv):
print("here I am!")
parser = argparse.ArgumentParser()
parser.add_argument("filenames", nargs="*", help="Filenames to check.")
parser.add_argument("filenames", nargs="*", help="Notebooks filenames")
parser.add_argument(
"--all",
action="store_true",
help="Generate markdown version for all the notebooks",
)
args = parser.parse_args(argv)
print(args)

for filename in args.filenames:
filenames = args.filenames
if args.all:
filenames = glob.glob(f"{notebook_tutorials_dir}/*.ipynb")

for filename in filenames:
filepath = Path(filename)
if filepath.parent == notebook_tutorials_dir and filepath.suffix == ".ipynb":
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't seem to work on macOS, filepath is relative to the root of the repo while notebook_tutorials_dir is absolute.

I had the same problem in Haystack, I think we can hardcode paths here and use relatives everywhere.

print(filepath)
generate_markdown_from_notebook(str(filepath))

return 0
Expand Down