📜  jupyter nbconvert - Python (1)

📅  最后修改于: 2023-12-03 15:02:28.067000             🧑  作者: Mango

Jupyter nbconvert - Python

Jupyter nbconvert is a command-line tool that allows you to convert Jupyter notebooks (.ipynb files) to various other formats, including:

  • HTML
  • LaTeX
  • PDF
  • Markdown
  • ReStructured Text
  • executable script (Python, Julia, R, etc.)

This is particularly useful if you want to share your notebook with someone who doesn't have Jupyter installed, or if you want to include your notebook in a document or presentation.

Basic Usage

To use nbconvert, simply open a terminal or command prompt and navigate to the directory that contains your notebook. Then, type the following command:

jupyter nbconvert --to FORMAT notebook.ipynb

Replace "FORMAT" with the desired output format (e.g., "markdown", "html", "pdf"), and replace "notebook.ipynb" with the name of your notebook file.

By default, nbconvert will save the output file in the same directory as the original notebook, with the same name but with the appropriate file extension. You can specify a custom output file name and location using the "--output" option:

jupyter nbconvert --to FORMAT --output output_file OUTPUT_LOCATION/notebook.ipynb
Additional Options

Nbconvert has several other options that allow you to customize the output format, including:

  • "--template" : specify a custom template file
  • "--no-prompt" : exclude input and output prompts from the output
  • "--ExecutePreprocessor.timeout" : specify the maximum time (in seconds) to wait for code execution
  • "--TagRemovePreprocessor.remove_cell_tags" : remove cells with specific tags from the output
  • "--TagRemovePreprocessor.remove_all_outputs_tags" : remove all outputs from cells with specific tags

You can see a full list of options by typing "jupyter nbconvert --help".

Markdown Output

If you want to convert your notebook to Markdown format, you can use the following command:

jupyter nbconvert --to markdown notebook.ipynb

This will create a Markdown file with the same name as your notebook, but with the ".md" file extension.

Markdown output includes cell input and output, as well as markdown cells. Code cells are enclosed in triple backticks, like this:

print("Hello, world!")

Markdown cells are enclosed in HTML-style tags:

<h1>This is a heading</h1>
Conclusion

Jupyter nbconvert is a powerful tool that allows you to easily convert your notebooks to various formats. Whether you're sharing your work with others or incorporating it into a larger document or presentation, nbconvert can help you create professional-looking output with minimal effort.