Skip to content

Use Comet in Jupyter Notebooks

Comet integrates seamlessly with hosted Jupyter Notebook services, including Google's Colab, Microsoft's Azure Notebooks, Amazon SageMaker Studio Lab, Amazon SageMaker notebook instance, and others.

There are a few differences between running Comet inside a Notebook and a script, as described below.

Set your API key and Project name

Usually, when configuring your project to use Comet, you would set your API Key and Project name either as environment variables or within the .comet.config file.

However, when running Comet inside a Jupyter Notebook, configure your project using comet_ml.init. Run the following snippet at the top of your Notebook before importing any other machine language framework.

import comet_ml
comet_ml.init(project_name='<Your Project Name>')

That code opens a prompt that allows you to enter your API Key and make it available throughout the Notebook environment.

End an Experiment

When running Comet through a script, an Experiment ends when the script has finished executing. This is not the case with Notebooks. To terminate an Experiment within a Notebook, you must make an explicit call, so.



On cloud Jupyter Notebook providers, remember to add: !pip install comet_ml or use the new %pip install comet_ml.

Source code from Notebooks is also only logged once an Experiment is terminated. You will notice that the execution order of the cells in your Notebook is also logged to Comet, reflecting the true history of how the Notebook was run, rather than just a snapshot of the cells.

Display the Comet UI directly in a Notebook

Another key feature of Comet in Notebooks is the ability to display the Comet UI within a cell. Simply call:

experiment.display(tab='Tab Name')

The result is that the corresponding tab opens up in the Experiment page.

The following are the available tab names:


Try it out!

Try our example for Comet in Jupyter Notebooks.

Open In Colab

Learn more

For additional examples of using Comet and Jupyter Notebooks, see our Notebook examples repository.

Sep. 19, 2023