- Difficulty level: easy
- Time need to lean: 10 minutes or less
- Key points:
- Magic
%previewpreviews variables and files in supported format - Output of magic
%previewcan be saved in notebook or in console panel
- Magic
Instant preview of intermediate results is extrmely useful for interactive data analysis. SoS provides rich and extensible preview features to
- preview variables in SoS and subkernels,
- preview files in many different formats,
- show preview results temporarily in the side panel or permanently in the main notebook
- generate interactive tables and plots for better presentation of data both in Jupyter notebook and in converted HTML reports
SoS provides a %preview magic to preview files and variables after the completion of a cell. Although most of the time the item specified is the name of either a file or a variable, the magic will actually show information about both a file and a variable if they happen to have the same name, as shown in the following example.
Preview in notebook or panel (options -n and -p)
By default, %preview displays results in the side panel if the side panel is open, and otherwise in the main notebook. You can override this behavior with options -p (--panel) or -n (--notebook) to always display results in the console panel or notebook. This is why we use -n most of the time in this tutorial to keep the preview results in this notebook.
For example, with option -p, the preview result will not be saved in notebook, even if the console panel is not open.
If option -w (--workflow) is specified, sos collects workflow steps from the current notebook and preview it. This would give you a better sense of what would be converted with magic %convert --to sos or executed with magic %sosrun.
The %preview magic previews the variables with the kernel using which the cell is evaluated. For example, even if you have var defined in both SoS and R kernel,
If you would like to preview variable in another kernel, you can specify it using option --kernel, for example, the following cell previews a variable r.val in a SoS kernel.
When a workflow or a task is executed remotely (see Remote Execution for details, result files might be on a remote host that is unavailable for local preview.
For example, you can execute a workflow on a remote host bcb to generate a file (mygraphic.png) and use %preview -r bcb to preview it.
If a data to be previewed is a Pandas DataFrame (or a csv or excel file which would be previewed as a DataFrame), SoS will preview it as a sortable and searchable table. For example, the following cell get a data.frame mtcar from the R kernel (as a pandas DataFrame) and preview it in the main notebook:
Compared to previewing the same variable in R, you have the addition features of
- sorting table by clicking the sort icon at the header of each column
- displaying a subset of rows that matches specified text in the input text
Note that SoS by default outputs the first 200 rows of the table. You can use option -l (--limit) to change this threshold.
The %preview magic can be used to preview files in a variety of formats.
SoS can preview figures in multiple formats including .png, .jpg, and .gif. For example, the following magic previews a png figure generated in R.
Note that SoS by default preview PDF files as an embeded object (iframe), but you can use option -s png to convert PDF to png to preview it as a image, if you have imagematick and Python package wand installed.
| Option | description |
|---|---|
--style png (-s png) |
Convert PDF to png before preview. All pages are combined to produce a sngle PNG figure if the pdf file contains multiple pages. This option requires Python module wand. |
--pages 2 3 |
With --style png, preview specified pages (page numbers starting from 1) from a multi-page PDF file. |
SoS uses a modular design and allows the definition of previewers in separate modules. For example, with the installation of module sos-bioinfo (and required packages such as pysam), you can preview bioinformatics-specific formats such as .VCF and .bam.