jupyter_io.io module#

in_notebook(file: TPathLike, /, *, prefix: str = 'Download: ', suffix: str = '') TPathLike[source]#

Save a file to a Jupyter notebook as a data-embedded download link.

This function is intended to be used together with file saving by another library, in a manner of wrapping the path of the file. It will return the path of a temporary file for temporary file saving. When the code cell running that saved the file is completed, the temporary file will be automatically converted to a download link with the file data embedded in it, and the link will be displayed.

Parameters:
  • file (TPathLike) – Path of the file to be saved. Even if an absolute or relative path is given, only the name part will be used for file saving.

  • prefix (str) – Prefix of the download link.

  • suffix (str) – Suffix of the download link.

Returns:

Path of the temporary file until it will be saved to a Jupyter notebook.

Raises:

RuntimeError – Raised if current interactive shell does not exist.

Return type:

TPathLike

Examples

To save a Matplotlib figure into a notebook:

import matplotlib.pyplot as plt

plt.plot([1, 2, 3])
plt.savefig(in_notebook("plot.pdf"))

To save a pandas series into a notebook:

import pandas as pd

ser = pd.Series([1, 2, 3])
ser.to_csv(in_notebook("series.csv"))

To save a general text into a notebook:

with open(in_notebook("output.txt"), "w") as f:
    f.write("1, 2, 3\n")
to_notebook(file: Path | str, /, *, prefix: str = 'Download: ', suffix: str = '') None[source]#

Save a file to a Jupyter notebook as a data-embedded download link.

Parameters:
  • file (Path | str) – Path of the file to be saved.

  • prefix (str) – Prefix of the download link.

  • suffix (str) – Suffix of the download link.

Return type:

None

Examples

To save a Matplotlib figure into a notebook:

import matplotlib.pyplot as plt

plt.plot([1, 2, 3])
plt.savefig("plot.pdf")
to_notebook("plot.pdf")

To save a pandas series into a notebook:

import pandas as pd

ser = pd.Series([1, 2, 3])
ser.to_csv("series.csv")
to_notebook("series.csv")

To save a general text into a notebook:

with open("output.txt", "w") as f:
    f.write("1, 2, 3\n")

to_notebook("output.txt")