Skip to content

Python Panel Examples

This page provides a series of end-to-end (complete) examples using Comet Python Panels.

Hello, World!

For any item that you would like to appear in the Panel area, simply ui.display() it:

from comet_ml import ui

ui.display("<b>Hello</b> <i>world</i>!")

Using Matplotlib

This example show how to create and display a Matplotlib example. Note that you can pass the plt module or figure to ui.display.

from comet_ml import ui
import matplotlib.pyplot as plt

names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]
fig, [ax1, ax2, ax3] = plt.subplots(1, 3, figsize=(9, 3))
ax1.bar(names, values)
ax2.scatter(names, values)
ax3.plot(names, values)
fig.suptitle('Categorical Plotting')

ui.display(fig)
from comet_ml import ui
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])

ui.display(fig)

Using Plotly

You can use Plotly with data logged by your Python Experiment. In this example, we define it directly, but it can also come from an asset (see example below).

This example also uses the Options data. This is a tab in the Python Panel that allows you to create difference instances of your Panel without having to change the code. In this example, you can control the entire Plotly layout by putting JSON in the options tab.

The options are accessed through the comet_ml.API instance.

from comet_ml import API, ui
import plotly.graph_objects as go

api = API()
options = api.get_panel_options()
data = [
    {
        "type": "bar",
        "x": ["a", "b", "c"],
        "y": [50, 100, 25],
    }
]
figure = {
    "data": data,
    "layout": options.get("layout", {}),
}
ui.display(go.Figure(figure))

This example uses the Options defined for the Panel instance.

Using API, options, and metrics

This is actually the default Python Panel, with comments:

from comet_ml import API, ui
import matplotlib.pyplot as plt

# Get available metrics
api = API()
metrics = api.get_panel_metrics_names()

# Make chart interactive by adding a dropdown menu
selected_metric = ui.dropdown('Select a metric: ', metrics)

# Use API to fetch the metric data for all experiments in the panel scope
experiment_keys = api.get_panel_experiment_keys()
metrics = api.get_metrics_for_chart(experiment_keys, [selected_metric])

# Visualize the data
fig, ax = plt.subplots()
for experiment_key in metrics:
    for metric in metrics[experiment_key]["metrics"]:
        fig.plot(metric['steps'], metric['values'])

ui.display(fig)

Using API and options

In this example, we use the API instance to get the options, get the Panel's experiments, and get the metrics.

from comet_ml import API, ui
import matplotlib.pyplot as plt

api = API()

experiment_keys = api.get_panel_experiment_keys()
options = api.get_panel_options()

option_metrics = options.get("metrics", ["loss"])

metrics = api.get_metrics_for_chart(
    experiment_keys, option_metrics)

fig, ax = plt.subplots()
ax.set_title("Metrics: " + (", ".join(option_metrics)))
ax.set_xlabel("steps")
ax.set_ylabel("value")

for experiment_key in metrics:
    for metric in metrics[experiment_key]["metrics"]:
        values = metric["values"]
        steps = metric["steps"]
        fig.plot(
            steps, values,
            label=experiment_key[:9] + " " + metric["metricName"]
        )
fig.legend()
ui.display(fig)

Using PIL image library

This example uses the standard Python Image Library to create an image.

from comet_ml import ui
from PIL import Image
import random

image = Image.new("RGB", (100, 100))
pixels = image.load()
for x in range(100):
    for y in range(100):
        pixels[x, y] = (int(random.random() * 256),
                        int(random.random() * 256),
                        int(random.random() * 256))
ui.display(image)

Getting assets

This example demonstrates getting an image asset from all expreiments, and displaying it.

from comet_ml import API, ui

api = API()

experiments = api.get_panel_experiments()
for experiment in experiments:
    asset_data = experiment.get_asset_list("image")
    for asset in asset_data:
        image = experiment.get_asset(asset["assetId"], "binary")
        # Do something with raw asset data. In this case, we can
        # just pass it to display_image()
        if "." in asset["fileName"]:
            _, format= asset["fileName"].rsplit(".", 1)
            ui.display_image(image, format)

If you are going to access many items related to an experiment, it may be easier to use api.get_panel_experiments that returns a list of APIExperiment and use its methods directly, like:

from comet_ml import API, ui

api = API()

experiments = api.get_panel_experiments()
for experiment in experiments:
    asset_data = experiment.get_asset_list("image")
    for asset in asset_data:
        image = experiment.get_asset(asset["assetId"], "binary")
        if "." in asset["fileName"]:
            _, format= asset["fileName"].rsplit(".", 1)
            ui.display_image(image, format)

Using Plotly Express

Note that pandas lakes a long time to load. However, once loaded, it works as you would normally use it.

from comet_ml import ui
import pandas
import plotly.express as px

df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length",
    color="species")
ui.display(fig)

Using a dropdown

from comet_ml import ui

selection = ui.dropdown("Pick one:", ["One", "Two", "Three"])
ui.display("You selected", selection)
Apr. 29, 2024