You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Running is code will take all the `.ipynb` notebooks in a folder and copy them to a template folder after removing all code from within the code chunks.
It works for specific folders now, and is written in R and bash, but can be generalized further.
It converts the `.ipynb` notebooks to `.qmd` files temporarily to do this, and `.ipynb` files require something in the code chunks, which for now is `#`.
It also will redo relative filepaths so that images from the tutorials folder show up in these tutorial templates.
Thank you Jacqueline Nolis for the regular expression code: <https://twitter.com/skyetetra/status/1454472378243698694>
```{r}
## libraries
library(tidyverse) ## install.packages(tidyverse)
library(here) ## install.packages(here)
library(glue) ## install.packages(glue)
## quarto: install the command line interface (CLI) from https://quarto.org
## list all tutorials
ipynb_files <- list.files(here("tutorials"), pattern = ".ipynb")
ipynb_files <- tools::file_path_sans_ext(ipynb_files)
## for loop
for (f in ipynb_files) { ## f = ipynb_files[3]
## convert to .qmd in template directory
system(glue("quarto convert tutorials/{f}.ipynb --output tutorials-templates/{f}.qmd"))
## remove code from code chunks (leaving only #), redo image filepaths, overwrite the .qmd
read_file(glue("tutorials-templates/{f}.qmd")) %>%
str_replace_all("```\\{python(.*)\\}[\\S\\s]*?```","```\\{python\\1\\}\n#\n\n```") %>%
write_file(glue("tutorials-templates/{f}.qmd"))
## convert template to .ipynb
system(glue("quarto convert tutorials-templates/{f}.qmd --output tutorials-templates/{f}.ipynb"))
## delete temporary .qmd
file.remove(glue("tutorials-templates/{f}.qmd"))
}
```
Note: without the added `#` in the code chunks, they disappeared entirely in the .ipynb files, [like this example](https://github.com/NASA-Openscapes/2021-Cloud-Hackathon/blob/4c6c8aa2deb484bdf11d91c6f0379b8b230ac382/tutorials-templates/Additional_Resources__Direct_S3_Access__gdalvrt.ipynb). An additional space did not alleviate, so we'll do comments for now.