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
  • "None having direct methodological answers in any form or manner:" Not following why you mention that in regards to the answer referenced below it? That 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
  • As to your question....I think part of the issue is that there's a lot of ways to do this and you aren't providing examples and how they are failing to meet your needs. Also, are you aware of nbviewer that is the Jupyter Community's provided way of viewing 'static' notebooks? (It isn't PDF but it is better if you can share your stuff publicly or can set up your own hosting service.) – Wayne Commented Nov 21, 2024 at 22:11
  • The "jupyter nbconvert" converts the entire .ipynb file (all cells/outputs) to PDF or HTML - not the programmatic output from a specific cell of a notebook. That is the crux of the question. The need is the ability to generate a PDF-based report that is formatted exactly as the output from a cell, as is generated from Jupyter notebook cell. This serves the purpose of generating a "Portable Document Format" (PDF!) based report (arbitrarily large in size) from the notebook, instead of a Jupyter notebook just converted to PDF entirely. The question is not about generating an web/html report. – Soliton Commented Nov 22, 2024 at 1:04
  • "...not the programmatic output from a specific cell of a notebook. That is the crux of the question." As I pointed out, there are already pathways for this. It seems you are not seeing how to put it together. Using nbformat to do it programmatically or using JupyterLab's drag-and-drop in JupyterLab , it is straightforward to divide up a notebook so it it is only the output of a single cell and using the command referenced you can leave out the input code from that and convert it to PDF. Making web/html report very much parallels this and that is why I brought it up. Indeed, the question... – Wayne Commented Nov 22, 2024 at 15:49
  • <continued> you referenced yourself seemed to want to do that and you said yours was like that. It is straightforward once you have the web/html form of the output to convert it to PDF by opening in your browser and using the print option to save to a PDF. The web/html form is much more information rich than the PDF as for example in the link you reference, the Plotly plots would remain interactive. Combined with viewing the produced document in nbviewer, or if keeping it private is a concern nbpreview, a report in .ipynb is often sufficient. – Wayne Commented Nov 22, 2024 at 15:56
 |  Show 2 more comments

1 Answer 1

Reset to default 2

I'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:

本文标签: