Skip to content

Commit

Permalink
Merge pull request scipy#21998 from agriyakhetarpal/fix/jupytext-note…
Browse files Browse the repository at this point in the history
…book-conversion

MAINT, DOC: Use Jupytext's API, and fix cross-platform usage for `doc/convert_notebooks.py`
  • Loading branch information
rgommers authored Dec 5, 2024
2 parents 43fc97e + c5228f5 commit 3333569
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions doc/convert_notebooks.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import glob
import os
import shutil
import jupytext

def call_jupytext(md_files, _contents_path, _contents_cache_path):
is_cached = os.path.exists(_contents_cache_path)
if not is_cached:
for md_file in md_files:
basename = os.path.splitext(os.path.basename(md_file))[0]
output_name = os.path.join(_contents_path, f"{basename}.ipynb")
os.system(f"python3 -m jupytext --output {output_name} {md_file}")
nb = jupytext.read(md_file)
jupytext.write(jupytext.read(md_file), output_name, fmt="ipynb", version=4)
return True

is_dirty = False

for md_file in md_files:
basename = os.path.splitext(os.path.basename(md_file))[0]
output_path = os.path.join(_contents_path, f"{basename}.ipynb")
cached_output_path = os.path.join(_contents_cache_path, f"{basename}.ipynb")
cmd_execution_time = os.path.getctime(cached_output_path)
md_file_modification_time = os.path.getmtime(md_file)
if cmd_execution_time <= md_file_modification_time:
os.system(f"python3 -m jupytext --output {output_path} {md_file}")
cmd_execution_time = os.stat(cached_output_path).st_mtime
md_file_modification_time = os.stat(md_file).st_mtime
if cmd_execution_time < md_file_modification_time:
nb = jupytext.read(md_file)
jupytext.write(nb, output_path, fmt="ipynb", version=4)
is_dirty = True
else:
shutil.copyfile(
Expand All @@ -32,6 +36,7 @@ def call_jupytext(md_files, _contents_path, _contents_cache_path):
if __name__ == '__main__':
_contents_cache_path = os.path.join("build", "_contents")
_contents_path = os.path.join("source", "_contents")

os.makedirs(os.path.expanduser(_contents_path), exist_ok=True)

md_files = glob.glob("source/tutorial/stats/*.md")
Expand Down

0 comments on commit 3333569

Please sign in to comment.