-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Programmatically run nbstripout and output new file. #57
Comments
There is a (as you rightly point out undocumented) way of doing it programmatically - do what is done in import io
from nbstripout import strip_output, read, write, NO_CONVERT
if __name__ == '__main__':
filename = ...
outfile = ...
with io.open(filename, 'r', encoding='utf8') as f:
nb = read(f, as_version=NO_CONVERT)
nb = strip_output(nb)
with io.open(outfile, 'w', encoding='utf8') as f:
write(nb, f) |
I tried the above in Python3.6 with Anaconda on Mac OsX and with just the import statement, I am getting the following error.
I looked at the nbstripout.py code and the code is specifically for Python3 yet this is where the error is coming from. Any ideas for a fix? |
Are you trying the import from within a notebook? This will not work because |
I was trying to do something similar and might have a solution. If you want to do this from within a notebook, it's possible to run nbstripout as a part a bash command in a cell: # Both original and the cleaned example are in the current working directory
notebook_path = './notebook.ipynb'
cleaned_path = './notebook_clean.ipynb'
# Bash commands within notebooks are preceeded by "!"
!cat {notebook_path} | nbstripout > {cleaned_path} I've been running a command like this in a cell to make it more versatile and provide some feedback: import os
from datetime import datetime
notebook_path = os.path.join(os.getcwd(),'notebook.ipynb')
cleaned_path = os.path.join(os.getcwd(),'notebook_clean.ipynb')
!cat {notebook_path} | nbstripout > {cleaned_path}
date_str = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print('{0} Cleaned file created at: {1}'.format(date_str, cleaned_path))
Note: I think there might be an error in readme.rst because the shell pipeline example doesn't use |
Thanks for the comments. I was running nbstripout with Python 3.6 as a script and not a notebook. |
@stanleyng8 I can not reproduce this issue: have tried with Python 3.6 from anaconda. Which nbstripout version are you using? |
Thanks Florian but I have decided not to pursue this avenue any further. |
I am trying to run nbstripout programmatically within a Python script and create a new file from the input file (and not in-place). The documentation doesn't have this use case covered. Is there a way I can do it?
The text was updated successfully, but these errors were encountered: