Every Mode report contains an integrated notebook-style environment where you can use either Python or R to further explore and visualize your query results. Whenever a report is run, any code included in that report’s notebook will also run.

Use moveable code blocks and markdown cells for exploratory data analysis, visualization, and collaboration. The integrated Notebook environment supports Python 3.7 and R version 3.6.1. Notebooks are pre-loaded with a variety of Python libraries and R packages. You can add the results of output cells to reports, or share a link to the Notebook directly.

Popular use cases:

  • Data Manipulation - Cleaning, aggregating, and summarizing data

  • Statistics - Simple things like distributions, regressions, and trend lines, as well as some advanced tasks like predictive modeling and machine learning.

  • Advanced Visualization - Python and R have many visualization libraries, enabling analysts to quickly build charts including heatmaps, small multiples, and distributions.

Using the Notebook

To get started using the Notebook:

  1. Open an existing report or create a new report and run one or more SQL queries from the Query Editor.
  2. Click New Notebook. Your query results will automatically be loaded into a datasets object.
  3. On the right side panel, click the dropdown to select the language you want to work in, either Python 3.7 or R.

notebook language

Key elements of the Mode Notebook:

  • Toolbar - Where you can manipulate and run your Notebook, restart the session, export, and more.
  • Cells - Compose code and view results in a Code cell, or contextualize your work with a Markdown cell.
  • Resources Panel - The right side panel provides resources to help you including keyboard shortcuts, external documentation, and supported libraries/packages.
  • Status Indicator - Where you are notified about your Notebook session status.


Notebook Toolbar

  1. Run Notebook - Runs all input cells in the Notebook in sequence (from top to bottom)
  2. Restart Session - Stops any current computations running in the Notebook. Restarts the session, thus clearing all the variables, libraries imported, etc. that were defined. However, code in input cells will be available to re-run after the Notebook restarts
  3. Run Cell - Runs code in the selected cell
  4. Add New Cell - Adds new input cell above or below the current cell
  5. Move Cell Up - Moves the current input or markdown cell up
  6. Move Cell Down - Moves the current cell down
  7. Delete Cell - Permanently removes cell from the notebook
  8. Fold Cell - Folds (hides) the current cell. Folded cells can still run
  9. Freeze Cell - Freezes the current input cell so that no changes are allowed; also prevents this cell from running
  10. Markdown/Code dropdown - Allows you to select the type for the current input cell (as code or markdown)
  11. Add output to Report Builder - Adds the output of the selected cell to the Report Builder
  12. Export Notebook - Exports all markdown and input cells as a .py or .r file

Working with cells

There are two types of cells in the Notebook:

Markdown - Markdown cells allow you to add context to your analysis. Markdown cell contains text formatted using Markdown and displays its output in-place when it’s run.

Code - Input Python or R code into the IN section of the cell. When this cell runs, any corresponding output (including visualizations) will be shown in in the OUT section.


  • When you run your notebook, cells are executed in the order they are displayed, starting from the top cell.
  • To select or change a cell’s type, go to the dropdown menu in the top toolbar and choose Code or Markdown.
  • To run a cell, select it and press Shift + Return. Or click Run Cell in the toolbar.
  • The number next to the cell label will increment by one every time code in the cell is successfully run.
  • To see available methods for an object, type the name of the object followed by . and then press tab.

Notebook Status

The status indicator, located in the bottom right corner of the browser window, will notify you if there is an issue with your session. It may prompt you to restart the kernel.

  • Setting up notebook - Displayed when opening up a new Notebook, or after re-starting your session.
  • Ready - Notebook is ready to go.
  • Running - Your code is executing.
  • Loading dataframes - This message may display for larger datasets while dataframe information is loaded into the Notebook.
  • Notebook has encountered an unexpected error - Your session has crashed and will need to be restarted.
  • There was a problem with your session - Your session has terminated and you need to click Restart to get things working again.
  • Cell is still running. Hang tight! - This can appear when code being run includes long-running, computationally intense functions. The Notebook is still online.
  • Notebook is having trouble, try running again - The Notebook is experiencing problems. Please try running your code again to fix the issue.

Accessing query results

The Notebook has access to the results of every query in your report. However, the way you access those results differs depending on the language you’re using. In each case, all query results are delivered to the Notebook as a custom object called datasets. datasets contains objects of the following type:

Python: pandas DataFrame

R: Data Frame

In your Notebook code, reference query result sets in the datasets list by query name, position, or token. For example:

To return results for: Python R
First query added to report datasets[0] datasets[[1]]
Second query added to report datasets[1] datasets[[2]]
Query named ‘Active Users’ datasets["Active Users"] datasets[["Active Users"]]
Query with token ‘6763b688fb54’ datasets["6763b688fb54"] datasets[["6763b688fb54"]]


  • The datasets object won’t update in the Notebook until after all queries in the report have run successfully.
  • R is 1-indexed and Python is 0-indexed.
  • If you refer to query results by the query name, remember to update your code if you rename the query in your report.
  • The order of the results in the datasets object is based on when the query was added to the report. Renaming a query may change the order it’s displayed in the report editor but will not affect its position in the datasets object.
How to find a query’s token

To find the query token starting from the Notebook or editor, click View in the header, then View details, and then click SQL for the query you wish to use. The URL for SQL contains the query token at the end:


Query token


Adding cell output to your report

Notebook Toolbar

Add contents of the OUT section of any Notebook cell to the Report Builder by clicking on the cell and then clicking Add to Report in the toolbar. You can adjust the dimensions and placement of this cell in the Report Builder.

NOTE: Scheduled runs will only include Mode’s Notebook as long as the Report view page contains at least one output generated from the Notebook. At that point, the Notebook will refresh when the report itself is re-run.

Add CSV export to a cell

You can add an export button to a Notebook output cell so viewers can export the calculated results contained in any pandas DataFrame (Python) or R Data Frame to a CSV. The following examples add an export button to an output cell that will generate a downloadable CSV of the query results of a query named “New Users”:

import notebooksalamode as mode # Required library in Python

df = datasets["New Users"]      # export_csv() accepts any valid pandas DataFrame. This
mode.export_csv(df)             # example uses the result set from a query named "New Users".
df <- datasets[["New Users"]]  # export_data() accepts any valid Data Frame. This
export_data(df)                # example uses the result set from a query named "New Users".

Supported libraries


The Notebook supports separate environments for Python v2.7 and v3.7. Each environment comes pre-loaded with the following libraries:

IMPORTANT: Mode will end support for the legacy Python 2.7 environment at the end of 2019. Any new libraries and non-critical updates to libraries will be released for the Python 3.7 environment only.

IMPORTANT: We strongly discourage using the requests library to access APIs that require authentication using personally identifiable credentials and information, as they will be visible to viewers of your report.


The Notebook supports R version 3.6.1 and comes pre-loaded with the following R packages:

IMPORTANT: We strongly discourage using the httr library to access APIs that require authentication using personally identifiable credentials and information, as they will be visible to viewers of your report.

Install additional libraries

If you would like to use a publicly available library in the Notebook that is not listed above, you can use each environment’s package manager to install that library at run-time. The Notebook environment has up to 1 GB of memory available to load additional packages.

IMPORTANT: This offers a workaround to try to install additional libraries, beyond what we currently support, into the Notebook. Public package management systems may include R or Python packages that are not supported by Mode’s current Notebook architecture, including interactive libraries like Bokeh.

Unlike officially supported libraries, you must install packages for any additional libraries in each individual report’s Notebook environment. You must add the below package installation commands to the Notebook in each report where you want the corresponding libraries to be available. Avoiding these commands can result in the library not installing and or importing properly.

WARNING: Some libraries require authentication with credentials (e.g., Tweepy, requests, etc.). We strongly discourage using libraries that require authentication using personally identifiable credentials and information, as these credentials will be visible to viewers of your report.


First, enter the following command into a Notebook cell for each public package that you want to install into the Python Notebook, as demonstrated below with the bloom-filter package (replace bloom-filter with the name of the package you want to install):

! pip install bloom-filter -t "/tmp" > /dev/null 2>&1
              Package name

Next, in a subsequent cell, add an import statement for each library that want to include in your environment. For example:

from bloom_filter import BloomFilter
     Package name

You may now use any of the methods or functionality included in the library in subsequent Notebook cells.


First, enter the following command into a Notebook cell for each public package that you want to install into the R Notebook, as demonstrated below with the random package (replace random with the name of the package you want to install):

install.packages("random", lib="/tmp/")
                  Package name

Next, invoke the library command for each library you want to include in your environment from the installed package(s). For example:

library("random", lib.loc="/tmp/")
         Library name

You may now use any of the methods or functionality included in the library in subsequent Notebook cells.


How much memory is available to the Notebook?

Each Notebook session has the following resources available, depending on the version of your Mode organization:

Available memory Run-time limit Suspend after idle for
Mode Studio 4 GB 60 minutes 30 minutes
Mode Paid Plans 16 GB 12 hours 60 minutes

When suspended, the Notebook environment can be resumed at any time by running a cell, running the entire Notebook, or running the report.

How long will Mode continue to support Python 2.7?

Mode will continue to support Python 2.7 until the end of 2019. Starting in 2020 Mode, along with the majority of the Python community, will end support for Python 2.x and require Python 3.x.

Starting October 2018, Mode will not release any new libraries or non-critical library updates in the Python 2.7 environment. We encourage you to migrate your work to Python 3.7 so you can access the latest versions of supported libraries.

Why should I use Python 3.7 instead of Python 2.7?

Learn more about the advantages of using Python 3 over Python 2 as well as some background about why Python 3 exists.

Will my code break if I switch my Notebook to Python 3.7?

Python 2.7 code will likely not execute in a Python 3.7 environment without some modification. There are a number of syntax differences to take into consideration when migrating to Python 3.7. For more information, read about porting Python 2 code to Python 3.

In the Notebook, why can I only select Python 3.7 and not Python 2.7?

Given that Mode is ending support for Python 2.7 at the end of 2019, the Python 2.7 environment will not be enabled for Mode organizations created on or after October 2018. If you would like us to enable Python 2.7 in your organization to support your transition to Python 3.7, please contact Mode’s Customer Success team and let us know.

Last updated June 2, 2020