Comments (7)
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))
2017-06-23 12:57:40 Cleaned file created at: <current directory>/notebook_clean.ipynb
Note: I think there might be an error in readme.rst because the shell pipeline example doesn't use cat
while the docstring in nbstripout.py does. I can open a separate issue if needed.
from nbstripout.
There is a (as you rightly point out undocumented) way of doing it programmatically - do what is done in main()
. Something like this (untested):
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)
from nbstripout.
I tried the above in Python3.6 with Anaconda on Mac OsX and with just the import statement, I am getting the following error.
File "/Users/stanley/anaconda3/envs/py36/lib/python3.6/site-packages/nbstripout.py", line 96, in <module>
output_stream = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
AttributeError: 'OutStream' object has no attribute 'buffer'
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?
from nbstripout.
Are you trying the import from within a notebook? This will not work because sys.stdout
is a ipykernel.iostream.OutStream
in that case.
from nbstripout.
Thanks for the comments. I was running nbstripout with Python 3.6 as a script and not a notebook.
from nbstripout.
@stanleyng8 I can not reproduce this issue: have tried with Python 3.6 from anaconda. Which nbstripout version are you using?
from nbstripout.
Thanks Florian but I have decided not to pursue this avenue any further.
from nbstripout.
Related Issues (20)
- Should be agnosting on trailing blank lines HOT 9
- [Feature Request] Process Folders (Batch / Bulk) HOT 1
- Doesn't strip out pycharm metadata HOT 9
- New release HOT 2
- Replace cram with prysk HOT 2
- Read config from `setup.cfg` HOT 1
- Option to error on cell outputs exceeding `--max-size` HOT 4
- It is recommended to remove pytest-runnner from setup_requires in setup.py HOT 4
- Strip output_type=stderr only, with keep_output? HOT 4
- Possible nbstripout-fast integration HOT 6
- Prevent committing notebooks with errors in cell outputs HOT 6
- Specifying Python executable path in `nbstripout --install`
- 'nbstripout' is not recognized as an internal or external command, operable program or batch file. HOT 2
- Not compatible with `pre-commit-hooks/pretty-format-json` hook HOT 4
- `git config filter.nbstripout.extrakeys ` support for `attachments`? HOT 4
- Support setting defaults for command line arguments via git config
- No valid notebook detected HOT 4
- required = true by default or make doc more explicit about it HOT 5
- Support git-filter-repo HOT 2
- Any examples for --keep-id option? HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nbstripout.