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 nbrmd packages installed, and re-start jupyter, i.e. run

pip install nbrmd --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.

Can I edit the python file?

Yes, please! That's the precise purpose for the nbsrc package. When you're done, please reload the notebook, i.e. refresh your notebook in the browser. Note that the url should have only the notebook name (no additional #), like http://localhost:8888/notebooks/GitHub/nbrmd/tests/python_notebook_sample.ipynb.

As mentioned above, reloading the .ipynb with actually load updated inputs from the python script.

It is not required to restart the current kernel. Reloading may remove a few outputs (those corresponding to inputs you have changed), but it will preserve the python variables.

Python notebook in Jupyter Python script

How do you represent notebooks as scripts?

.R scripts follow the standard for that language.

Designing a comfortable standard for .py scripts is not trivial. The current format is documented here.

Command line conversion

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

Install it with

pip install nbsrc --upgrade

and then 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.4.0.tar.gz (7.2 kB view hashes)

Uploaded Source

Built Distribution

nbsrc-0.4.0-py3-none-any.whl (7.8 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