Using Python and R in Notebooks

This section covers the use of Python libraries, the Notebook kernel, the SisenseHelper Functions, using Python code, and data visualization.

Python Libraries

The libraries provided for the code language selected for the kernel are listed in the left navigation panel. The libraries need to be imported from a code block at least once and can be used throughout the Notebook.

Adding External Python Libraries

You can add external Python libraries from code blocks using the pip install command.

Once the installation of a library is complete, a note displays in the Console Output from pip stating that you may need to restart the kernel to use the updated packages. This note can be ignored - you will not need to restart the kernel to use the package.

Syntax Errors for pip Installation

Shell commands (like pip install) must be prefixed with an exclamation mark "!" when they are run from a notebook cell. For example, if you get a syntax error using pip to install the emoji library, make sure your command looks like this: !pip install emoji.

Watch this video about adding Python libraries:

Notebook Kernel

The kernel is a virtual machine that performs the computations required to execute code. It can run in the background to speed up computing.

It has three statuses:

  • Stopped
  • Starting
  • Running with Elapsed Time

The kernel does not automatically start when a Notebook is created. The kernel status will read "Stopped" until the very first Code block has been triggered to run.

When the first Code Block is triggered to run by a user clicking "Run Code", the kernel status changes to "Starting" and the code block should show a status stating "Starting kernel" with a loading icon.

Note:

Starting the first code block run may take a few minutes. Subsequent code runs will be quicker.


Once the kernel has been started, the Kernel Status shows a timer with the Elapsed Time to indicate how long the kernel has been running. You can click the square stop icon to stop the kernel. You will need to run a code block to start the kernel again.

Switching languages, for example from Python 3.9 to Python 3.7, stops the current kernel and starts a new one.

The kernel shuts down for 5 minutes after the notebook has been closed or after the browser tab has been closed.

Note:

If a notebook is left open, the kernel continues to run and may increase the CPU and memory use of your instance.


SisenseHelper Functions

There are 3 SisenseHelper functions that must be used in order to get the most out of Notebooks. They are the following:

Function Description
SisenseHelper.load_dataframe('SQL_Cell_Name') Load the output table of a SQL cell named in the function. Define a variable using this function.
SisenseHelper.save_dataframe(df) Save the dataframe into the Sisense Cloud so that the preview can be displayed and charts can be created using this output.
SisenseHelper.save_image(plt) Save the image plot into the Sisense Cloud so that the preview can be displayed and chart cells can be created using this image.

Adding Python and R to a Notebook

Adding a Code Cell

To gain further insights from the analysis done using SQL, you can use Python to do more complicated manipulations.

The outputs of a SQL block can be loaded into Code blocks using the SisenseHelper.load_dataframe() function with the name of the SQL block as the input. The SisenseHelper.save_dataframe() function can be used to save the final output as a dataframe:

  • Click + Code. The code block appears and already includes the SisenseHelper functions for reference. The load_dataframe() function already has the most recent SQL block name as the input, ('data_year' in the screenshot below).
  • To create a Sisense chart using the output of the code, SisenseHelper.save_dataframe() must be used with the desired dataframe used as the input.

    Once the Python output has been saved using SisenseHelper.save_dataframe(), the output can be used to create a Sisense Chart by clicking the Chart button.

Note:

SQL block outputs can be loaded into and used in Code blocks but Code blocks cannot be loaded into other Code blocks as dataframes at this time.

Watch this video on how to add Python code:

Visualizing with Python and R Plotting Libraries

Image Charts with Plotting Libraries

In addition to creating Sisense provided charts with the SisenseHelper.save_dataframe() function, analysts can take advantage of the plotting libraries available to Python and R by using a 3rd Sisense Helper function that will output a static image of the custom plot.

  • After creating a custom visualization using a plotting library like matplotlib, seaborn, ggplot and more.
  • Use SisenseHelper.save_image() to save and output your image chart

Only 1 image can be output per code block.
If the SisenseHelper.save_dataframe() function and the SisenseHelper.save_image() function are both found in the code cell, the save_dataframe() function will be the output.

Here is an example using the Chloroplethr plotting library with R:

Limitations

  • Interactive libraries, such as Plot.ly, are not supported in the Notebooks tab for Python or R.
  • Any chart created by Python or R cannot be used with the Adding Notebook SQL Charts to Dashboards feature, which takes a chart from a Notebook and adds it directly to a Dashboard in the Analytics tab. This includes the Sisense provided charts created with Python or R and also the custom plot images.