Skip to main content

Jupyter notebooks from/to python and R scripts

Project description

Python and R scripts as Jupyter notebooks

Pypi Pypi Build Status codecov.io pyversions

This package provides companion scripts (.py or .R extension) to your Jupyter notebooks, that are always synchronized with the notebook.

With this you will be able to

  • set the .py or .R script under version control
  • modify the script outside of Jupyter, and easily merge multiple contributions to the notebook using standard, text merge tools
  • reload the latest version of the notebook from the .py or .R script. Outputs for the cells with unchanged input are taken from the .ipynb file.

How do I activate the companion script?

  • generate a jupyter config, if you don't have one yet, with jupyter notebook --generate-config
  • edit the config and include the below:
c.NotebookApp.contents_manager_class = 'nbrmd.RmdFileContentsManager'
c.ContentsManager.default_nbrmd_formats = ['.ipynb', '.py']

Then, make sure you have the nbsrc and nbrmd packages installed, and re-start jupyter, i.e. run

pip install nbrmd nbsrc --upgrade
jupyter notebook

Per notebook configuration

With the above configuration, every notebook will have a companion .py file.

If you prefer that the companion script be generated only for a few notebooks, then remove the c.ContentsManager.default_nbrmd_formats line from Jupyter's configuration, and instead edit the notebook metadata as follows:

{
  "kernelspec": {
    "name": "python3",
    (...)
  },
  "language_info": {
    (...)
  },
  "nbrmd_formats": [".ipynb", ".py"]
}

Accepted formats are: .ipynb, .Rmd, .py and .R.

In case you want both .py and .Rmd, please note that the order matters: the first non-.ipynb extension is the one used as the reference source for notebook inputs.

Command line conversion

The package also provides a nbsrc script that converts Jupyter notebooks to R or python scripts, and vice-versa.

Use it as:

nbsrc jupyter.ipynb         # this prints the `.py` or `.R` alternative
nbsrc jupyter.ipynb -i      # this creates a jupyter.py or jupyter.R file
nbsrc jupyter.py    -i      # and this, a jupyter.ipynb file
nbsrc jupyter.py    -i -p   # update the jupyter.ipynb file and preserve outputs that correspond to unchanged inputs

Alternatively, the nbsrc package provides two nbconvert exporters that you can use with

nbconvert jupyter.ipynb --to pynotebook
nbconvert jupyter.ipynb --to rnotebook

And if I convert twice?

Round trip conversion of scripts is identity. Round trip conversion of Jupyter notebooks preserves the source, not outputs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nbsrc-0.3.0.tar.gz (6.5 kB view hashes)

Uploaded Source

Built Distribution

nbsrc-0.3.0-py3-none-any.whl (7.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page