Skip to content

Commit

Permalink
See if tutorial autogeneration works
Browse files Browse the repository at this point in the history
  • Loading branch information
zain-sohail committed Oct 11, 2023
1 parent a7cae25 commit b4c5fb5
Show file tree
Hide file tree
Showing 4 changed files with 392 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: documentation building
name: update requirements.txt

on:
# Triggers the workflow on push but only for the main branch
push:
branches: [ workflow_trials ]
paths: [pyproject.toml]

jobs:
pytest:
Expand Down
78 changes: 78 additions & 0 deletions .github/workflows/renaming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import pathlib
import shutil
import subprocess


def create_new_directory_with_modified_files(directory_path, new_directory_path):
try:
# Create a Path object for the source directory
source_directory = pathlib.Path(directory_path)

# Ensure the source directory exists
if not source_directory.is_dir():
raise FileNotFoundError(f"The source directory '{directory_path}' does not exist.")

# Create a Path object for the new directory
new_directory = pathlib.Path(new_directory_path)

# Create the new directory if it doesn't exist
new_directory.mkdir(parents=True, exist_ok=True)

# Loop through files in the source directory
for file_path in source_directory.iterdir():
if file_path.is_file():
# Remove spaces and change hyphens to underscores in the filename
new_name = file_path.name.replace(" ", "").replace("-", "_")

# Construct the path for the new file in the new directory
new_file_path = new_directory / new_name

# Copy the file to the new directory with the modified name
shutil.copy2(file_path, new_file_path)

print(f"Copied '{file_path}' to '{new_file_path}'")

print("File copying completed successfully.")

except Exception as e:
print(f"An error occurred: {str(e)}")


def convert_ipynb_to_rst(directory_path):
try:
# Create a Path object for the directory
directory = pathlib.Path(directory_path)

# Ensure the directory exists
if not directory.is_dir():
raise FileNotFoundError(f"The directory '{directory_path}' does not exist.")

# Loop through files in the directory
for file_path in directory.iterdir():
if file_path.is_file() and file_path.suffix == ".ipynb":
# Construct the command to run nbconvert
command = f"jupyter nbconvert --to rst {str(file_path)}"

# Run nbconvert using subprocess
subprocess.run(command, shell=True, check=True)

print(f"Converted '{file_path}' to .rst")

print("Conversion to .rst completed successfully.")

except Exception as e:
print(f"An error occurred during conversion: {str(e)}")


if __name__ == "__main__":
# Specify the source directory path where you want to process files
source_directory_path = "tutorial/"

# Specify the new directory path where you want to create modified files
new_directory_path = "docs/tutorial/"

# Call the function to create a new directory with modified filenames
create_new_directory_with_modified_files(source_directory_path, new_directory_path)

# Run nbconvert to convert .ipynb files to .rst in the new directory
convert_ipynb_to_rst(new_directory_path)
38 changes: 38 additions & 0 deletions .github/workflows/tutorial_build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: autogenerate docs from tutorials

on:
# Triggers the workflow on push but only for the main branch
push:
branches: [ workflow_trials ]
paths: ['**.ipynb']

jobs:
pytest:
runs-on: ubuntu-latest
steps:
# Check out repo and set up Python
- name: Check out the repository
uses: actions/checkout@v4
with:
lfs: true

- name: "Setup Python, Poetry and Dependencies"
uses: packetcoders/action-setup-cache-python-poetry@main
with:
python-version: 3.8
poetry-version: 1.2.2

# Generates and commits a requirements.txt used by readthedocs
# Missing is to run the notebooks for now!
- name: run python script
run: poetry run python docs/tutorial/renaming.py

- name: remove ipynbs
run: rm docs/tutorial/*.ipynb

- name: Commit changes
uses: EndBug/add-and-commit@v9
with:
default_author: github_actions
message: 'Updating tutorial rsts for docs'
add: 'docs/tutorial/'
288 changes: 274 additions & 14 deletions tutorial/1 - Binning fake data.ipynb

Large diffs are not rendered by default.

0 comments on commit b4c5fb5

Please sign in to comment.