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

use preprocessor to strip solutions #79

Open
mathause opened this issue Jan 24, 2023 · 1 comment
Open

use preprocessor to strip solutions #79

mathause opened this issue Jan 24, 2023 · 1 comment

Comments

@mathause
Copy link
Contributor

For the python intro course I found a nice preprocessor which can strip notebook cells that contain a certain string (e.g. # solution). This could be used to have notebooks with and without solutions. Might need some changes to the structure of the project.

https://git.iac.ethz.ch/ip_python/ip_python/-/blob/main/intro/strip_solutions

Code under details.


#!/bin/bash

for f in code_with_solutions/*.ipynb; do
    [[ -e $f ]] || continue

    f=$(basename ${f})


    fN_i=code_with_solutions/${f}
    # need a relative path
    fN_o=code/${f}

    if [[ ${fN_i} -nt ${fN_o} ]]; then
        # remove all cells that contain "# solution"
        jupyter nbconvert \
          --to notebook \
          --ClearOutputPreprocessor.enabled=True \
          --RegexRemovePreprocessor.enabled=True \
          --RegexRemovePreprocessor.patterns="['# solution.*']" \
          ${fN_i} \
          --output ../${fN_o}
    else
      echo "skipping ${fN_i}"
    fi
done
@mathause
Copy link
Contributor Author

mathause commented Jan 24, 2023

Unfortunately there seems to be no official way to add a cell (at least I found nothing). But the following python snippet should work:

import nbformat
import nbformat.notebooknode
from nbconvert import NotebookExporter

from traitlets.config import Config

fN_in = "code_with_solution.ipynb"
fN_out = "code.ipynb"

fN_in = "code_with_solutions/1.1_numpy_intro.ipynb"

# =====

# create preprocessors
c = Config()
c.NotebookExporter.preprocessors = [
  'nbconvert.preprocessors.ClearOutputPreprocessor',
  'nbconvert.preprocessors.RegexRemovePreprocessor',
]

c.RegexRemovePreprocessor.patterns='# solution.*'

notebook_exporter = NotebookExporter(config=c)

new_cell = {
    'cell_type': 'markdown',
    'metadata': {},
    'source': f'> Notebook auto-generated from _`{fN_in}`_'
}

node = nbformat.notebooknode.from_dict(new_cell)

notebook = notebook = nbformat.read(fN_in, as_version=4)

# add the id to the new cell
notebook.cells = [node] + notebook.cells

__, notebook = nbformat.validator.normalize(notebook)

(body, resources) = notebook_exporter.from_notebook_node(notebook)

with open(fN_out, "w") as f:
    f.writelines(body)

NOTE: RegexRemovePreprocessor needs to be added as well, see https://nbconvert.readthedocs.io/en/latest/nbconvert_library.html#Using-different-preprocessors

EDIT: now with the preprocessors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant