Skip to content

use preprocessor to strip solutions #79

@mathause

Description

@mathause

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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions