{% if section(page, "dr-notebooks") %}

You can create and run code, text, and chart cells within your notebook. Before proceeding, be sure to review the guidelines for [configuring and initializing a notebook environment](dr-env-nb).

{% else %}

You can create and run code, text, and chart cells within your notebook. Before proceeding, be sure to review the guidelines for [configuring and initializing a notebook environment](wb-env-nb).

{% endif %}

### Use the DataRobot API {: #use-the-datarobot-api }

Within DataRobot Notebooks you can easily use the DataRobot API. The environment images DataRobot provides come with the respective DataRobot Python and R clients preinstalled. DataRobot automatically fetches your endpoint and your API token and sets them as environment variables (`DATAROBOT_ENDPOINT` and `DATAROBOT_API_TOKEN` for Python and `DATAROBOT_API_ENDPOINT` and `DATAROBOT_API_TOKEN` for R) within your notebook container, so that when the session starts up it automatically handles client instantiation without requiring manual authentication.

=== "Python"

    Import the DataRobot package to start using the Python client:

    `import datarobot as dr`


=== "R"

    Import the DataRobot library to start using the R client:

    `library(datarobot)`

{% if section(page, "workbench") %}

#### Python client use case support

When running DataRobot code in a notebook that is part of a Use Case, by default the Use Case is configured for DataRobot's Python client. The Python client (if you are using client version 3.2, which is preinstalled in the built-in notebook environments) detects the Use Case that the notebook is in and configures that as the default Use Case. This means that when in a Use Case, any projects or datasets you create using the Python client from a notebook are automatically included among that use cases's assets. You can override this default behavior by either:

 * Using the `default_use_case` parameter during `datarobot.Client()` instantiation.
 * Explicitly specifying the `use_case` parameter when creating a project or dataset using any of their respective creation methods. 

{% endif %}

### Create code cells

The default image for the notebooks is a pre-built Python image with all the dependencies and common open-source software (OSS) libraries preinstalled. To see the list of all packages available in the default image, hover over that image in the **Environment** tab:

{% if section(page, "dr-notebooks") %}

![](images/nb-8.png)

{% else %}

![](images/wb-nb-8.png)

{% endif %}

!!! note
    DataRobot notebooks are not polyglot; run Python code or R code in a notebook, but do not intermingle both. The language supported will depend on the image you’ve selected for the notebook’s environment configuration.

{% if section(page, "dr-notebooks") %}

For more information about configuring a notebook's environment, read about [environment management](dr-env-nb).

{% else %}

For more information about configuring a notebook's environment, read about [environment management](wb-env-nb).


{% endif %}

### Add libraries {: #add-libraries }

Follow the instructions below to ad-hoc install additional libraries in Python or R.

!!! note
    Notebooks support rich outputs similar to Jupyter, so you can use plotting libraries of your choice (such as matplotlib, seaborn, etc.) and see the plotted charts inline within the cell output. You can run other shell commands for Python as well by using the `!` notation.

    Note that when the session restarts, these ad-hoc installations will not persist as the environment will revert back to the original state of the pre-built image.

=== "Python"

    To install and use a Python package that is not included in the default image, run the following within a code cell:

    `!pip install <your-package>`


=== "R"

    You can work with the R language in a notebook instead of Python by selecting the pre-built R image from the **Environment** tab. You can then write and execute R code in the code cells of the notebook with the R kernel. To install and use additional R packages, use [devtools](https://cran.r-project.org/web/packages/devtools/devtools.pdf){ target=_blank } from a code cell or the following code:


    `install.packages(<package_name>)`

### Create text cells {: #create-text-cells }

Markdown text cells support traditional Markdown syntax and [GitHub-flavored Markdown](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax){ target=_blank } syntax. Render Markdown cells by executing the cell. To edit a rendered Markdown cell, double click on the cell.

{% if section(page, "dr-notebooks") %}

![](images/nb-9.png)

{% else %}

![](images/wb-nb-9.png)

{% endif %}

### Create chart cells

DataRobot allows you to create built-in, code-free chart cells within DataRobot Notebooks, enabling you to quickly visualize your data without coding your own plotting logic.

!!! note
    Note that chart cells are only supported for Python notebooks. They are unavailable for R notebooks.

To add a code cell, hover your cursor outside of a cell in the notebook and select the **Chart** option.

{% if section(page, "dr-notebooks") %}

![](images/nb-32.png)

{% else %}

![](images/wb-nb-32.png)

{% endif %}

When you add a chart cell to a notebook, you first need to specify the data you want to visualize by selecting a DataFrame. Select the variable name corresponding to the DataFrame from the dropdown list in the chart cell. The cell lists all  of the DataFrame objects that are currently in-memory.

!!! note
    Note that DataRobot only plots the first 5,000 rows of the DataFrame.

{% if section(page, "dr-notebooks") %}

![](images/nb-33.png)

{% else %}

![](images/wb-nb-33.png)

{% endif %}

After selecting a DataFrame, pick the type of chart you want to create. DataRobot offers configurations for bar charts, line charts, scatter plots, and area charts. They all require the same configuration fields, detailed below.

{% if section(page, "dr-notebooks") %}

![](images/nb-34.png)

{% else %}

![](images/wb-nb-34.png)

{% endif %}

Field | Description
---------- | -----------
Show title | Displays a title for the chart. Once enabled, provide a name for the title.
Display tooltips | Shows tooltips with more details on the data when hovering over the chart.
Show legend | Displays legend mapping.
X-axis (Dimension) | Select a column from the DataFrame (provided as a dropdown list) to encode as the X-axis.
Y-axis (Measure) | Select a column from the DataFrame (provided as a dropdown list) to encode as the Y-axis.
Color | Choose the color for the chart contents.

In addition to the configuration above, you can modify aspects of the X- and Y-axes by selecting the settings wheel next to each field.

{% if section(page, "dr-notebooks") %}

![](images/nb-35.png)

{% else %}

![](images/wb-nb-35.png)

{% endif %}

Toggle | Description
---------- | -----------
Hide axis label | Hides the label for the axis from the chart display.
Hide grid | Removes the grid pattern in the background of the chart.
Hide in tooltip | Hides the value for the axis in the tooltip shown when hovering on points of the chart.
Show point markers | Displays point markers along the points for the axis in the chart.
Aggregation | Sets whether to aggregate values for the axis. You can aggregate by a variety of values, including the median, mean, sum, standard deviation, and more.

After configuring the chart, you can edit how the cell displays using the icons in the top-right corner.

{% if section(page, "dr-notebooks") %}

![](images/nb-36.png)

{% else %}

![](images/wb-nb-36.png)

{% endif %}


{% if section(page, "dr-notebooks") %}

* Select the pencil icon to hide the editor menu for the chart cell.
* Click the download icon to save a local copy of the chart as an SVG file.
* Select the trash can icon to delete the chart cell.
* Click the menu icon to access the [actions](dr-action-nb) available for the cell.

{% else %}

* Select the pencil icon to hide the editor menu for the chart cell.
* Click the download icon to save a local copy of the chart as an SVG file.
* Select the trash can icon to delete the chart cell.
* Click the menu icon to access the [actions](wb-action-nb) available for the cell.

{% endif %}


### Table of contents {: #table-of-contents }

As you create and execute cells to develop your notebook, DataRobot automatically generates a table of contents for easier navigation. To access it, click the table of contents icon (![](images/icon-toc.png)) in the sidebar.

The **Markdown** tab provides an autogenerated table of contents. Each entry maps to headings in the notebook's Markdown cells, corresponding to the Markdown heading level (#, ##, ###, etc.). Entries are hyperlinked, so clicking on an entry navigates you to the corresponding cell in the notebook.

{% if section(page, "dr-notebooks") %}

![](images/nb-23.png)

The **Cells** tab provides an overview of all cells within the notebook (both code and Markdown cells). You can perform [cell actions](dr-action-nb) (and move cells around via drag-and-drop) from the cell list in the table of contents.

{% else %}

![](images/wb-nb-23.png)

The **Cells** tab provides an overview of all cells within the notebook (both code and Markdown cells). You can perform [cell actions](wb-action-nb) (and move cells around via drag-and-drop) from the cell list in the table of contents.

{% endif %}

{% if section(page, "dr-notebooks") %}

![](images/nb-24.png)

{% else %}

![](images/wb-nb-24.png)

{% endif %}

### Cell settings {: #cell-settings }

{% if section(page, "dr-notebooks") %}

You can configure [notebook-wide cell settings](dr-settings-nb#notebook-settings) by accessing the gear icon menu at the top right of the Notebook header.

![](images/nb-4.png)

{% else %}

You can configure [notebook-wide cell settings](wb-settings-nb#notebook-settings) by accessing the gear icon menu at the top right of the Notebook header.

![](images/wb-nb-4.png)

{% endif %}
