admin管理员组文章数量:1332353
I would like to save/export all of the output, formatted in the manner captured using Jupyter notebook's %%capture magic - to a PDF file.
Similar to what this question is trying to answer - however, I don't need to interact with the output in the file with a cursor, or even use html - just static output to PDF file that captures exactly the formatting of the cell output - only dumped to a PDF file:
Export Jupyter Notebook cell output that includes Plotly plots to an html file
There are other similar but unanswered questions: None having direct methodological answers in any form or manner:
How to export only the output in pdf in jupyter notebook?
This seems like a simple functionality for long/large outputs from a cell resulting in a lot of crunching and visualizations, however, I couldn't find it answered anywhere. I need to do it this way, since the content of the cell output will be very large, and thus best saved as a formatted PDF-like file on disk. It could be any other file format, if PDF is not possible, however, (1) the formatting same or very similar to the Jupyterlab output is important - to maintain clarity (2) the resulting saved file must be viewable in a viewer that can handle large output file size (which PDF compresses and PDF viewers can handle well).
%%capture magic obviously works very well - but I can't seem to find a way to easily go from "%%capture Output" to then save "Output" as a PDF file, instead of issuing "Output.show" in the following cell that displays to another cell/stdout in the notebook.
Can anyone help ?
I would like to save/export all of the output, formatted in the manner captured using Jupyter notebook's %%capture magic - to a PDF file.
Similar to what this question is trying to answer - however, I don't need to interact with the output in the file with a cursor, or even use html - just static output to PDF file that captures exactly the formatting of the cell output - only dumped to a PDF file:
Export Jupyter Notebook cell output that includes Plotly plots to an html file
There are other similar but unanswered questions: None having direct methodological answers in any form or manner:
How to export only the output in pdf in jupyter notebook?
This seems like a simple functionality for long/large outputs from a cell resulting in a lot of crunching and visualizations, however, I couldn't find it answered anywhere. I need to do it this way, since the content of the cell output will be very large, and thus best saved as a formatted PDF-like file on disk. It could be any other file format, if PDF is not possible, however, (1) the formatting same or very similar to the Jupyterlab output is important - to maintain clarity (2) the resulting saved file must be viewable in a viewer that can handle large output file size (which PDF compresses and PDF viewers can handle well).
%%capture magic obviously works very well - but I can't seem to find a way to easily go from "%%capture Output" to then save "Output" as a PDF file, instead of issuing "Output.show" in the following cell that displays to another cell/stdout in the notebook.
Can anyone help ?
Share Improve this question asked Nov 21, 2024 at 0:11 SolitonSoliton 311 silver badge4 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 2I've made a demonstration notebook to show is a step-by-step process of using nbformat to extract out one specified cell from a previously run notebook. The extracted cell will include the output. Then jupyter nbconvert
can be used to drop the cell with the input using the --no-input
flag in the jupyter nbconvert
command.
You can see the demonstration notebook here.
(This isn't only way this process could be performed, see my comments under the OP, especially my last one's for some pointers to other options.)
The key code is the following Python that uses nbformat to make a new notebook from the notebook you point it at by setting original_ntbk
:
number_cell_to_keep = 11 #keep in mind that Python uses zero indexing so lower by 1
# the following based on https://stackoverflow/a/78123424/8508004 and https://stackoverflow/a/79151141/8508004 and section '# Drop specified cell numbers from a notebook and name the produced one per settings' at https://github/fomightez/humap2-binder/blob/3e06708d4cab559d3711a101a963eec247603374/additional_nbs/standardizing_initial_data/Standardizing_identifier_order_in_humap2-provided_csv.ipynb#L2199
original_ntbk = "example_nb.ipynb"
from pathlib import Path
new_ntbk_name = f"{Path(original_ntbk).stem}_JUST_SELECTED_CELL{Path(original_ntbk).suffix}"
import nbformat as nbf
ntbk = nbf.read(original_ntbk, nbf.NO_CONVERT)
new_ntbk = ntbk
new_ntbk.cells = [cell for cell_index,cell in enumerate(ntbk.cells) if cell_index == number_cell_to_keep]
nbf.write(new_ntbk, new_ntbk_name, version=nbf.NO_CONVERT)
Then you can run the following in the next cell in the same notebook where you ran the above code:
!jupyter nbconvert --no-input --to pdf {new_ntbk_name}
The equivalent of that on the command line would be something like this:
jupyter nbconvert --no-input --to pdf new_ntbk.ipynb
Try it yourself:
Don't just take my word for it.
You can try the demo yourself where things are presently all set up to work, and you don't need to touch your own machine.
The demonstration notebook can be opened in remote served sessions without any need to sign: click here to launch a session featuring the demo notebook.
Or click this 'launch' badge to launch a session with this notebook opened already:
Static form of demo notebook is here, learn more about the environment used to develop it here.
Illustrated Starting Point & Result:
Starting with the cell shown below as part of a longer notbeook:
End up with the following PDF:
本文标签:
版权声明:本文标题:python 3.x - How to export all output from certain Jupyter cell, say, using %%capture magic, and save output to a PDF file? - St 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742321340a2452847.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
jupyter nbconvert
command shown in that answer is meant to be run on the command line. It isn't spelled out there as well as you'd like? You can make a comment and ask them to make it easier to understand. – Wayne Commented Nov 21, 2024 at 22:06.ipynb
is often sufficient. – Wayne Commented Nov 22, 2024 at 15:56