Created
April 25, 2020 23:42
-
-
Save dimidd/239b194404a79fbfec76ef52f52e85d7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import nbformat | |
import functools | |
import sys | |
def get_key(obj, key): | |
if isinstance(obj, dict): | |
return obj.get(key, None) | |
elif isinstance(obj, (list, tuple)) and key < len(obj): | |
return obj[key] | |
else: | |
return None | |
def dig(obj, *keys): | |
return functools.reduce(get_key, keys, obj) | |
def is_png(c): | |
if c['source'] != '': | |
return False | |
return dig(c, 'outputs', 0, 'data', 'image/png') | |
file = sys.argv[1] | |
nb = nbformat.read(file, 4) | |
for c in filter(is_png, nb.cells): | |
c['metadata']['editable'] = False | |
c['metadata']['jupyter'] = {} | |
c['metadata']['jupyter']['source_hidden'] = True | |
nbformat.write(nb, open(file, 'wt')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment