Skip to content
On this page

Importing Other Notebooks in Rational Notebooks

Rational provides a powerful feature that lets you quickly reuse code by importing named cells from other notebooks. This guide will walk you through the process of importing cells from other notebooks into your Rational notebook.

Basics of Importing Cells

To import a named cell from another notebook, you use the import statement. The syntax is as follows:

javascript
import {cellName} from '@productKey/pageName'

You replace cellName with the name of the cell you want to import, and replace @productKey/notebookName with the product key and name of the notebook from which you're importing.

Benefits of Importing Cells

While it might be tempting to simply copy-paste the code from one notebook to another, there are several advantages to importing cells.

Automatic Handling of Dependencies

If the cell you're importing depends on other cells in its original notebook, these dependencies are automatically handled when you import the cell. You don't need to worry about copying the dependent cells or dealing with conflicts with existing cells in your notebook.

Keeping Code Up-to-Date

When you import a cell, your notebook always uses the latest version of that cell. If the original notebook is updated, your notebook automatically reflects those updates. However, be aware that this could also break your notebook if the updates in the original notebook are not backward compatible.

Rewriting Imported Code with import - with

Rational allows you to inject dependencies into imported cells using import - with. This lets you "rewrite" the imported cells to fit your needs, without duplicating code. You can inject both static and dynamic definitions.

Nuances of Imports

There are a few things to keep in mind when importing cells:

  • Imported cells are lazily evaluated: they don't run until you reference them in your notebook.
  • You can only import named cells, and you must explicitly name each cell you want to import.
  • You can import the same notebook multiple times and references will resolve exactly, as long as the versions of the imported notebooks match.

Importing from Notebooks Across Products

You can import notebooks from the same product or a different product in Rational. When you import from a different product, you need to ensure that your end-users have access to it.

To import a notebook from a different product, you specify the product key in the import statement:

javascript
import {cellName} from '@differentProductKey/pageName'

Replace differentProductKey with the product key of the product that contains the notebook you want to import.

The name of a notebook can be set in the notebook settings.