Created
September 13, 2020 18:28
-
-
Save servadestroya/b219047e0a02ca92cfc26e212bcd7144 to your computer and use it in GitHub Desktop.
Functions to set attributes of a jupyter notebooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
ipynb_set_authors () { | |
local authors="[]" | |
local file="$1" | |
for author in "${@:2}"; do | |
authors="$(jq --arg author "$author" '. + [{"name": $author}]' <(echo "$authors"))" || return $? | |
done | |
jq_edit "$file" --argjson authors "$authors" '.metadata.authors = $authors' | |
} | |
ipynb_set_title () { | |
local file="$1" | |
local title="$2" | |
jq_edit "$file" --arg title "$title" '.metadata.title = $title' | |
} | |
# Only writes to file if the jq command succeeds | |
jq_edit () { | |
local file="$1" | |
local modified="$(mktemp)" | |
jq "${@:2}" < "$file" > "$modified" || return $? | |
mv "$modified" "$file" || return $? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment