jupyter_io package#

Submodules#

Module contents#

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")
savefig_in_notebook(fig: Figure | None = None, filename: Path | str = 'figure.pdf', **kwargs: Any) None[source]#

Save matplotlib figure in a notebook as a file.

Warning

This function is deprecated and will be removed in a future release. Use jupyter_io.in_notebook instead like:

fig.savefig(in_notebook(filename))
Parameters:
  • fig (Figure | None) – Matplotlib Figure object to be saved.

  • filename (Path | str) – Filename with explicit extension (e.g., figure.pdf).

  • **kwargs (Any) – Arguments to be passed to matplotlib savefig().

Return type:

None

savefile_in_notebook(f: Any, filename: Path | str, encoding: str = 'utf-8') None[source]#

Save file object (I/O object) in a notebook as a file.

Warning

This function is deprecated and will be removed in a future release. Use jupyter_io.in_notebook instead like:

with open(in_notebook(filename), 'w') as g:
    g.write(f.read())
Parameters:
  • f (Any) – File object (I/O object) to be saved.

  • filename (Path | str) – Filename of the saved file.

  • encoding (str) – Text encoding. It is only used if io is a text IO.

Return type:

None

savetable_in_notebook(table: DataFrame | Series[Any], filename: Path | str = 'table.csv', **kwargs: Any) None[source]#

Save pandas DataFrame or Series in a notebook as a file.

Warning

This function is deprecated and will be removed in a future release. Use jupyter_io.in_notebook instead like:

table.to_csv(in_notebook(filename))
Parameters:
  • table (DataFrame | Series[Any]) – pandas DataFrame of Series object to be saved.

  • filename (Path | str) – Filename with explicit extension (e.g., table.csv).

  • **kwargs (Any) – Arguments to be passed to table.to_<extension>().

Return type:

None

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")