https://github.com/davidbrochart/nbbranch
Branching in the Jupyter Notebook
https://github.com/davidbrochart/nbbranch
Last synced: 5 months ago
JSON representation
Branching in the Jupyter Notebook
- Host: GitHub
- URL: https://github.com/davidbrochart/nbbranch
- Owner: davidbrochart
- Created: 2020-08-31T20:52:55.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-08-31T21:24:49.000Z (over 5 years ago)
- Last Synced: 2025-06-23T05:08:30.936Z (8 months ago)
- Language: Jupyter Notebook
- Size: 2.93 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# nbbranch: branching in the Jupyter Notebook
## What if you could branch over multiple cells?
You would argue, just merge the cells and use the built-in Python `if`/`elif`/`else` statements. Except that a cell can have an output (some text, an image, a widget...), and so by branching over multiple cells you can generate several outputs conditionally. In the context of [Voila](https://github.com/voila-dashboards/voila), this means you can e.g. have a dynamically generated dashboard.
## What does it look like?
`nbbranch` is implemented as IPython magic commands.
```python
%load_ext nbbranch
a = 1
#
%%IF a == 0:
print('foo')
# no output
%%ELIF a == 1:
print('bar1')
# prints "bar1"
%%IF True:
print('True')
# prints "True"
%ENDIF
#
%%IF
print('bar2')
# prints "bar2"
%%ELIF a == 2:
print('baz')
# no output
%%ELSE
print('ERROR')
# no output
%ENDIF
```
A few remarks:
- We use upper-case equivalents of built-in Python branching keywords (`IF`, `ELIF`, `ELSE`).
- These keywords must be prefixed with `%` if the reminder of the cell is empty, or `%%` otherwise (general magic syntax).
- Indentation is not required (this may change in the future).
- We need an `%ENDIF` to mark the end of a corresponding `%IF`.
- Between the outer-most `%IF`/`%ENDIF`, all cells must start with one of `%IF`, `%ELIF`, `%ELSE` or `%ENDIF`. If the cell is just the continuation of the cell above, its first line must be an empty `%%IF`.