Skip to content
On this page

Reactivity in Rational Notebooks

Rational notebooks utilize the concept of reactivity, a powerful programming paradigm that allows data changes to propagate to other parts of your notebook that rely on that data. This guide will take a deep dive into reactivity in Rational notebooks, helping you to understand and effectively utilize this concept in your data products.

Cells: The Building Blocks

In a Rational notebook, the basic unit of work is a cell. Cells can hold various types of content including text, code, or even other media types. However, the true power of Rational's reactivity comes alive when cells contain code.

The Power of Code Cells

Unlike traditional scripts that execute in a linear manner, code in a Rational notebook is housed within cells that are highly interactive. Think of each cell as a standalone unit or a variable that can be referenced by other cells.

javascript
a = 1

In the example above, a is not just a variable in a script, it is a cell that holds the value 1.

Cells Interaction and Updates

The core of Rational's reactivity is the interaction between cells. When a cell’s value changes, any other cells that reference it will automatically update to reflect this change. This makes your notebook dynamic, with changes in one location rippling through the rest of the notebook.

javascript
b = a * 2

In this example, the cell that defines b depends on the value of a. If the value of a changes, b will automatically update.

Dependencies and Re-evaluation

Understanding how cells depend on each other is crucial to effectively using Rational notebooks. When the value of one cell (a dependency) changes, any cells that depend on it will automatically re-evaluate. This automatic propagation of changes is at the heart of reactivity.

Manual Re-evaluation

Rational also provides the capability to manually re-evaluate a cell. This can be done by clicking the reload button next to the cell. Manual re-evaluation is especially useful when a cell depends on external data or other factors that may change over time.

Asynchronous Programming with Promises

Rational has built-in support for Promises, which makes it easy to handle asynchronous operations in your notebook. If a cell's value is a Promise, Rational will automatically wait for the Promise to resolve before using its value.

javascript
data = fetch('https://api.example.com/data').then(response => response.json())

In this example, the data cell is fetching data from an API. Other cells that depend on data will not evaluate until the Promise resolves, ensuring that they have the most up-to-date data.

The Power of Generators

Rational's support for Generators opens up a world of possibilities for creating animations or continuously updating data. If a cell's value is a Generator, Rational will continuously update the cell as new values are yielded by the Generator.

javascript
random = {
  while (true) {
    yield Math.random();
    await new Promise(resolve => setTimeout(resolve, 1000));
  }
}

In this example, the random cell generates a new random number every second. Any cells that depend on random will update each second with the new random number, creating a dynamic and interactive experience.

Interactive Views

Rational provides powerful tools for creating interactive views within your notebook. These views can be created using HTML, SVG, or Canvas, and you can also use Rational's built-in libraries for data visualization.

Creating an Interactive View

Creating an interactive view in Rational is as simple as writing code in a cell. The reactivity of Rational ensures that your view will update as the data it depends on changes.

javascript
view = {
// Your code for creating the view goes here
}

In this example, view is a cell that contains the code for creating an interactive view. Any cells that depend on view will update when the view changes.