gagneurlab / gagneurlab/wBuild

Execute and render ipynb's similar to spin R / Rmd scripts

Open
#17 3 comments 1 reaction 1 assignee View on GitHub

@stefanches7 is already working on this.

Since Dec 6, 2019.

Dominant language
Python
Stars
4
Forks
6
PR merge metrics
No merged PRs in 30d

Description

It would be nice to be able to specify a similar header in the jupyter notebook as in R. wBuild would then parse this configuration and run the notebook. The notebook can be parametrized and executed using the following code

```python
import subprocess
import os

def jupyter_nbconvert(input_ipynb):
""" .ipynb -> .html
"""
subprocess.call(["jupyter",
"nbconvert",
input_ipynb,
"--to", "html"])

def render_ipynb(template_ipynb, rendered_ipynb, params=dict()):
"""Render the ipython notebook
Args:
template_ipynb: template ipython notebook where one cell defines the following metadata:
{"tags": ["parameters"]}
render_ipynb: output ipython notebook path
params: parameters used to execute the ipython notebook
"""
import papermill as pm # Render the ipython notebook

os.makedirs(os.path.dirname(rendered_ipynb), exist_ok=True)
pm.execute_notebook(
template_ipynb, # input template
rendered_ipynb,
kernel_name="python3", # default kernel
parameters=params
)
jupyter_nbconvert(rendered_ipynb)

rule compile_notebook:
"""Run the notebook and convert it to html
"""
input:
example='example_file_{some_param}.csv',
ipynb = "src/.ipynb"
output:
ipynb = "output/.ipynb",
html = "output/.html"
run:
render_ipynb(input.ipynb, output.ipynb,
params=dict(example=input.example,
some_param=wildcards.some_param))
jupyter_nbconvert(output.ipynb)
```

## Required packages
- papermill: https://github.com/nteract/papermill

## Related
- https://bitbucket.org/snakemake/snakemake/issues/529/support-running-from-ipython-notebooks
- https://github.com/jmeppley/snakemake_magic/blob/master/prototype/notebooks/snakemake%20magic.ipynb

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.