How to use the nbstripout._utils.strip_output function in nbstripout

To help you get started, we’ve selected a few nbstripout examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kynan / nbstripout / nbstripout / _nbstripout.py View on Github external
print("'{}' is not a valid notebook".format(filename), file=sys.stderr)
            sys.exit(1)
        except FileNotFoundError:
            print("Could not strip '{}': file not found".format(filename), file=sys.stderr)
            sys.exit(1)
        except Exception:
            # Ignore exceptions for non-notebook files.
            print("Could not strip '{}'".format(filename), file=sys.stderr)
            raise

    if not args.files and input_stream:
        try:
            with warnings.catch_warnings():
                warnings.simplefilter("ignore", category=UserWarning)
                nb = read(input_stream, as_version=NO_CONVERT)
            nb = strip_output(nb, args.keep_output, args.keep_count, extra_keys)
            if args.dry_run:
                output_stream.write('Dry run: would have stripped input from '
                                    'stdin\n')
            else:
                with warnings.catch_warnings():
                    warnings.simplefilter("ignore", category=UserWarning)
                    write(nb, output_stream)
                output_stream.flush()
        except NotJSONError:
            print('No valid notebook detected', file=sys.stderr)
            sys.exit(1)
github kynan / nbstripout / nbstripout / _nbstripout.py View on Github external
else:
        # Wrap input/output stream in UTF-8 encoded text wrapper
        # https://stackoverflow.com/a/16549381
        if sys.stdin:
            input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
        output_stream = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', newline='')

    for filename in args.files:
        if not (args.force or filename.endswith('.ipynb')):
            continue
        try:
            with io.open(filename, 'r', encoding='utf8') as f:
                with warnings.catch_warnings():
                    warnings.simplefilter("ignore", category=UserWarning)
                    nb = read(f, as_version=NO_CONVERT)
            nb = strip_output(nb, args.keep_output, args.keep_count, extra_keys)
            if args.dry_run:
                output_stream.write('Dry run: would have stripped {}\n'.format(
                    filename))
                continue
            if args.textconv:
                with warnings.catch_warnings():
                    warnings.simplefilter("ignore", category=UserWarning)
                    write(nb, output_stream)
                output_stream.flush()
            else:
                with io.open(filename, 'w', encoding='utf8', newline='') as f:
                    with warnings.catch_warnings():
                        warnings.simplefilter("ignore", category=UserWarning)
                        write(nb, f)
        except NotJSONError:
            print("'{}' is not a valid notebook".format(filename), file=sys.stderr)

nbstripout

Strips outputs from Jupyter and IPython notebooks

MIT
Latest version published 1 month ago

Package Health Score

89 / 100
Full package analysis

Similar packages