Skip to content

Instantly share code, notes, and snippets.

@servadestroya
Created September 13, 2020 18:28
Show Gist options
  • Save servadestroya/b219047e0a02ca92cfc26e212bcd7144 to your computer and use it in GitHub Desktop.
Save servadestroya/b219047e0a02ca92cfc26e212bcd7144 to your computer and use it in GitHub Desktop.
Functions to set attributes of a jupyter notebooks
#!/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