- Difficulty level: easy
- Time need to lean: 15 minutes or less
- Key points:
- SoS workflows can be embedded in Jupyter notebook
- Magic
%run
executes workflows defined in the current cell - Magic
%sosrun
executes workflows defined in the entire notebook - Magic
%runfile
executes workflows defined in specified file
SoS Notebook is an IDE for SoS workflow and allows the development and execution of workflows in a Jupyter environment.
SoS workflow is extended from Python 3.6. You can execute any Python statement using the SoS kernel. That is to say, you can use a SoS kernel just like a Python3 kernel.
For example, the following cell uses SoS to execute a python statement, which is considered as a simple SoS step without header.
In addition to regular Python statements, you can use SoS-specific syntax, functions, and statements in SoS cells.
For example, the following cell uses statement output
to specify step output, and a sh
function written in script format.
The statements are executed in a global SoS namespace so variables defined in another cell (greeting
) can be used here.
Technically speaking, we have justed executed a single SoS step in a global SoS namespace. Such steps are called scratch steps because they do not contain a header.
A workflow cell, namely an SoS cell with a header, cannot be executed directly. Running the cells will produce no output.
In contrast, a formal SoS step is defined as a step with a header. Formal SoS steps and workflows have to be executed by SoS magics or commands. As a matter of fact, nothing will happen if you execute the following cell in jupyter.
Magic %run
executes workflows defined in the current cell. SoS starts an external sos
process, execute the workflow and displays the output in the notebook. For example, the hello
workflow could be executed as follows:
The workflow is executed independently and does not share any variables in the SoS kernel. For example, you cannot use variable greeting
in the workflow,In
To pass variables to these workflows, you will have to define variables as parameters and pass them from command line.
Note that SoS expands { }
in the %sos
magic so the actual magic executed was %run --greeting
Hello world -v0
The verbosity argument -v
of magics %run
, %sosrun
and command sos run
accepts values
-v 0
: Display no system messages except errors-v 1
: Display errors and warnings, and a text-based progress bar-v 2 (default)
: Display errors, warnings, and informational messages-v 3
: Display additional debug messages-v 4
: Display very verbose trace messages for development purposes
A SoS notebook can have multiple workflow sections defined in multiple code cells. These sections constitute the content of the embedded SoS script of the notebook. For example, the following steps, defined in three separate cells, are all part of the embedded SoS script of this notebook.
An embed SoS script of a SoS notebook consists of SoS sections in all SoS cells of a notebook.
The easiest way to view the embedded script of a SoS notebook is to use the %preview --workflow
magic as follows (The option -n
lists the script in the notebook instead of the console panel). As you can see, the embedded script consists of steps from the entire notebook.
The %sosrun
magic execute workflows defined in the embedded SoS script of a notebook.
The %sosrun
magic can be used to execute any of the workflows defined in the notebook. For example, the following magic execute the workflow plot
defined in the above section. Because multiple workflows are defined in this notebook (hello_world
, and plot
), a workflow name is required for this magic.
The %runfile
magic executes a SoS script from specified file with specified option. Both SoS scripts (usually with extension .sos
) and SoS notebooks (with extension .ipynb
) are supported.
The third magic to execute SoS workflows in SoS Notebook is to use the %runfile
magic, which execute workflows from a specified external file. For example, instead of using magic %sosrun
, you can execute the current notebook with magic
The %sosrun
magic calls an external command sos
to execute workflows defined in the notebook. Although for the sake of convenience we will use magic %run
to execute workflows throughout this documentation, please remember that you can execute the notebook using command sos
from command line.
Alternatively, you can also write the workflow in a text file (usually with extension .sos
) and execute it with command sos run
:
You can execute a workflow using magics %run
, %sosrun
, and %runfile
in the background by adding a &
at the end of the magic. The workflow will be executed in a queue while you can continue to work the notebook.
SoS Notebook usually starts a workflow and waits until the workflow is completed. If the workflow takes a long time to execute, you can send workflows to a queue in which workflows will be executed one by one while you continue to work on the notebook. A status table will be displayed for each queued workflows and log messages and results will continue to send back to SoS Notebook.