Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/smc_pyutil/smc_pyutil/jupyter_delete_output.py
Views: 285
1
# -*- coding: utf-8 -*-
2
3
# This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
4
# License: AGPLv3 s.t. "Commons Clause" – read LICENSE.md for details
5
6
# Based on See https://gist.github.com/damianavila/5305869
7
8
from __future__ import absolute_import
9
import sys, io, os
10
from nbformat.v4 import reads, writes
11
12
13
def remove_outputs(nb):
14
"""
15
Remove the outputs from a notebook.
16
"""
17
for cell in nb.cells:
18
if cell.cell_type == 'code':
19
cell.outputs = []
20
21
22
def main():
23
for fname in sys.argv[1:]:
24
nb = reads(io.open(fname, 'r').read())
25
remove_outputs(nb)
26
base, ext = os.path.splitext(fname)
27
new_ipynb = "%s-no-output%s" % (base, ext)
28
io.open(new_ipynb, 'w', encoding='utf8').write(writes(nb))
29
30
31
if __name__ == "__main__":
32
main()
33
34