Skip to main content
Python cells allow you to execute arbitrary Python code in the context of the notebook session. You have access to the full Python standard library and common packages. You can access cell output variables that were set by other cell types. The cells support custom visualization and data processing capabilities. Additionally, you can integrate with external APIs and databases.

Variables

You can reference variables set by other python cells on the global scope.

Cell Results

All cells - with the exception of Markdown and Python cells, set their output variable ot a CellResult object.

Output

Ways to output information from a python cell:
  • Print to stdout/stderr
  • Display objects via IPython.display.display()
  • Return a display object
  • Return a CellResult object
  • Raise an exception

Examples

Execute arbitrary Python code for custom logic:

Cell Parameters (@param)

NOTE: See Form Cells for the preferred method of adding dynamic user inputs Python cells support inline parameter definitions using the @param comment syntax. Parameters create interactive form inputs that users can modify before running the cell, without editing the code directly. Use @param when you need to parameterize a single cell’s behavior. For inputs that need to be shared across multiple cells, use form cells instead.

Basic Syntax

Parameters must be defined at the top of the cell, after any import statements.

Supported Types

String

With additional options:
Provide a JSON array of choices:
The default value should be one of the choices. If omitted or invalid, the first choice is used.

Boolean

Number

For integers:
For floating-point numbers:

Secret

Creates a dropdown of available secrets in the workspace:

Connection

Creates a dropdown of available connections:

Parameter Options

Dynamic Dropdowns with Selectors

For dropdowns populated dynamically from data, use selectors. Selectors must be created in a prior cell using SpinContext.create_selector(). Cell 1: Create the selector
Cell 2: Use the selector in a parameter
Selectors cannot be created and used in the same cell.

Complete Example

Packages

The following python packages are pre-installed in the spin runtime container.

Installing additional packages and utility

Additional packages can be installed by using a shell cell in any notebook to run pip install. E.g.
Packages installed this way will persist across notebooks and sessions but may need to be reinstalled if the runtime is restarted or a new version is pulled.

Custom Runtime Images

To create a custom image with a python package (or additional other utility) pre-installed, use a custom Dockerfile e.g.