Skip to main content

A reactive Python kernel for Jupyter notebooks.

Project description

IPyflow

Checked with mypy Code style: black License: BSD3 Binder

TL;DR

Reactive Python notebooks for Jupyter[Lab]:

  1. pip install ipyflow
  2. Pick Python 3 (ipyflow) from the kernel dropdown.
  3. Use ctrl+shift+enter (or cmd+shift+enter on Mac) to execute a cell and everything that (recursively) depends on it:

About

IPyflow is a next-generation Python kernel for Jupyter and other notebook interfaces that tracks dataflow relationships between symbols and cells during a given interactive session, thereby making it easier to reason about notebook state. Here is a video of the JupyterCon talk introducing it (and corresponding slides).

If you'd like to skip the elevator pitch and skip straight to installation / activation instructions jump to quick start below; otherwise, keep reading to learn about IPyflow's features.

Features

IPyflow ships with classic Jupyter + JupyterLab extensions that provide the following user-facing features.

Execution Suggestions

IPyflow understands exactly what data changed in your notebook, when it changed, and when it got referenced. This allows it to provide execution suggestions whenever running some upstream cell changes data that downstream cells depend on. To keep the execution state consistent with the code in cells, rerun the turquoise-highlighted cells, and avoid the red-highlighted cells:

A turquoise-highlighted input with red-highlighted output just means that the output may be out-of-sync.

Reactivity

IPyflow enables opt-in reactive execution for both Jupyter and JupyterLab (similar to Observable or Pluto.jl, wherein execution of cell C results in automatic re-execution of all of C's dependents). Use ctrl+shift+enter (on Mac, cmd+shift+enter also works) to execute a cell and all of the cells that (recursively) depend on it reactively:

You can also run the magic command %flow mode reactive in any cell to enable reactivity as the default execution mode:

Disable by running %flow mode normal.

If you'd like to make reactivity the default behavior (not recommended, unless you do not mind being surprised occasionally), you can add this to your IPython profile (default location typically at ~/.ipython/profile_default/ipython_config.py):

c = get_config()
c.ipyflow.exec_mode = "reactive"

Syntax Extensions

Prefixing a symbol with $ in a load context will cause the referencing cell to re-execute itself, whenever the aforementioned symbol is updated (regardless of execution mode):

You can also use the $ syntax in store contexts, which triggers cells that reference the corresponding symbol to re-execute, regardless of whether the reference is similarly $-prefixed:

Finally, you can also prefix with $$ to trigger a cascading reactive update to all dependencies in the chain, recursively:

Integration with ipywidgets

IPyflow's reactive execution engine, as well as its APIs (see "State API" below) are fully compatible with ipywidgets, allowing cells to respond to slider changes, button clicks, and other events:

This functionality can be paired with other extensions like stickyland to build fully reactive dashboards on top of JupyterLab + IPyflow.

State API

IPyflow must understand the underlying execution state at a deep level in order to provide its features. It exposes an API for interacting with some of this state, including a code function for obtaining the code necessary to reconstruct some symbol:

# Cell 1
from ipyflow import code

# Cell 2
x = 0

# Cell 3
y = x + 1

# Cell 4
code(y)

# Output:
"""
# Cell 2
x = 0

# Cell 3
y = x + 1
"""

You can also see the cell (1-indexed) and statement (0-indexed) of when a symbol was last updated with the timestamp function:

from ipyflow import timestamp
timestamp(y)
# Timestamp(cell_num=3, stmt_num=0)

To see dependencies and dependents of a particular symbol, use the deps and users fuctions, respectively:

from ipyflow import deps, users

deps(y)
# [<x>]

users(x)
# [<y>]

If you want to elevate a symbol to the representation used internally by IPyflow, use the lift function (at your own risk, of course):

from ipyflow import lift

y_sym = lift(y)
y_sym.timestamp
# Timestamp(cell_num=3, stmt_num=0)

Finally, IPyflow also comes with some rudimentary support for watchpoints:

# Cell 1
from ipyflow import watchpoints

def watchpoint(obj, position, symbol_name):
    if obj <= 42:
        return
    cell, line = position
    print(f"{symbol_name} = {obj} exceeds 42 at {cell=}, {line=}")

# Cell 2
y = 14
watchpoints(y).add(watchpoint)

# Cell 3
y += 10

# Cell 4
y += 20
# y = 44 exceeds 42 at cell=4, line=1

Quick Start

To install, run:

pip install ipyflow

To run an IPyflow kernel in JupyterLab, select "Python 3 (ipyflow)" from the list of available kernels in the Launcher tab. For classic Jupyter, similarly select "Python 3 (ipyflow)" from the list of notebook types in the "New" dropdown dialogue.

JupyterLab Entrypoint: Classic Jupyter Entrypoint:

Similarly, you can switch to / from IPyflow from an existing notebook by navigating to the "Change kernel" file menu item in either JupyterLab or classic Jupyter:

JupyterLab Kernel Switcher: Classic Jupyter Kernel Switcher:

Colab, VSCode, and other Interfaces

Reactivity and other frontend features are not yet working in interfaces like Colab or VSCode, but you can still use IPyflow's dataflow API on these surfaces by initializing your notebook session with the following code:

%pip install ipyflow
%load_ext ipyflow

Citing

IPyflow started its life under the name nbsafety, which provided the initial suggestions and slicing functionality.

For the execution suggestions:

@article{macke2021fine,
  title={Fine-grained lineage for safer notebook interactions},
  author={Macke, Stephen and Gong, Hongpu and Lee, Doris Jung-Lin and Head, Andrew and Xin, Doris and Parameswaran, Aditya},
  journal={Proceedings of the VLDB Endowment},
  volume={14},
  number={6},
  pages={1093--1101},
  year={2021},
  publisher={VLDB Endowment}
}

For the dynamic slicer (used for reactivity and for the code function, for example):

@article{shankar2022bolt,
  title={Bolt-on, Compact, and Rapid Program Slicing for Notebooks},
  author={Shankar, Shreya and Macke, Stephen and Chasins, Andrew and Head, Andrew and Parameswaran, Aditya},
  journal={Proceedings of the VLDB Endowment},
  volume={15},
  number={13},
  pages={4038--4047},
  year={2022},
  publisher={VLDB Endowment}
}

We don't have a paper written yet for the syntax extensions that implement the reactive algebra, but in the mean time, you can cite the IPyflow repo directly for that and anything else not covered by the previous publications:

@misc{ipyflow,
  title = {{IPyflow: A Next-Generation, Dataflow-Aware IPython Kernel}},
  howpublished = {\url{https://github.com/ipyflow/ipyflow}},
  year = {2022},
}

Acknowledgements

IPyflow would not have been possible without the amazing academic collaborators listed on the above papers. Its reactive execution features are inspired by those of other excellent tools like Hex notebooks, Pluto.jl, and Observable. This work has benefited from the support of folks from a number of companies -- both in the form of direct financial contributions (Databricks, Hex) as well as indirect moral support and encouragement (Ponder, Meta). And of course, IPyflow rests on the foundations built by the incredible Jupyter community.

License

Code in this project licensed under the BSD-3-Clause License.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

ipyflow-0.0.192.tar.gz (109.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ipyflow-0.0.192-py2.py3-none-any.whl (86.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file ipyflow-0.0.192.tar.gz.

File metadata

  • Download URL: ipyflow-0.0.192.tar.gz
  • Upload date:
  • Size: 109.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for ipyflow-0.0.192.tar.gz
Algorithm Hash digest
SHA256 86eb065dec4e5b0457c06e9094bc03229421f1266d3a8ddbc98292c9a940c990
MD5 7788d56ceae4d65aa431ef75b511c769
BLAKE2b-256 3ae7f48242590d572996c525d1b0b4def7c2ba8076fb40a83971f6155d7d358a

See more details on using hashes here.

File details

Details for the file ipyflow-0.0.192-py2.py3-none-any.whl.

File metadata

  • Download URL: ipyflow-0.0.192-py2.py3-none-any.whl
  • Upload date:
  • Size: 86.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for ipyflow-0.0.192-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8f0314f0f3f488485b454cbf374fb05110207c3cc018f929176377d4839b25ba
MD5 0f482293087b0c594c63ca9f67db3f87
BLAKE2b-256 2bc044c77da54bc839c34ac388481b5ebda3f089c3151d34173f0f9dfc5a783f

See more details on using hashes here.

Supported by

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