Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/astropenguin/jupyter-io
:zap: Saving and loading files directly into Jupyter notebooks
https://github.com/astropenguin/jupyter-io
io jupyter python
Last synced: about 1 month ago
JSON representation
:zap: Saving and loading files directly into Jupyter notebooks
- Host: GitHub
- URL: https://github.com/astropenguin/jupyter-io
- Owner: astropenguin
- License: mit
- Created: 2020-01-27T07:00:34.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-12-28T14:39:25.000Z (about 2 months ago)
- Last Synced: 2024-12-28T14:45:29.331Z (about 2 months ago)
- Topics: io, jupyter, python
- Language: Python
- Homepage: https://astropenguin.github.io/jupyter-io/v2.0.1
- Size: 2.76 MB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jupyter-io
[](https://pypi.org/project/jupyter-io/)
[](https://pypi.org/project/jupyter-io/)
[](https://pepy.tech/project/jupyter-io)
[](https://doi.org/10.5281/zenodo.14379974)
[](https://github.com/astropenguin/jupyter-io/actions)Saving and loading files directly into Jupyter notebooks
## Installation
```shell
pip install jupyter-io
```## File saving
jupyter-io provides the `in_notebook` function to directly save (i.e. embed) files to Jupyter notebooks.
Suppose you create a Matplotlib figure want to save it as a PDF file.
The following code will save the PDF file to your local environment:
```python
import matplotlib.pyplot as pltplt.plot([1, 2, 3])
plt.savefig("figure.pdf")
```
This should work in many cases, however, in a virtual environment like [Google Colaboratory](https://colab.research.google.com/), you will not be able to get the file once the Jupyter server is stopped.
By wrapping the file path by `in_notebook`, the PDF file will be directly saved to the Jupyter notebook and you will get a download link instead:
```python
import matplotlib.pyplot as plt
from jupyter_io import in_notebookplt.plot([1, 2, 3])
plt.savefig(in_notebook("figure.pdf"))
```
The download link works when the Jupyter server is stopped, and even when it does not exist.
This makes Jupyter notebooks more portable, for example, to share the output data other than images together with them.### More examples
To save a pandas series to a notebook:
```python
import pandas as pd
from jupyter_io import in_notebookser = pd.Series([1, 2, 3])
ser.to_csv(in_notebook("series.csv"))
```To save a general text to a notebook:
```python
from jupyter_io import in_notebookwith open(in_notebook("output.txt"), "w") as f:
f.write("1, 2, 3\n")
```## File loading
The file loading feature has not been implemented yet.