Python Panel Examples

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

Other Python Panel Resources:

End-to-end Python Panel Examples

This section has a number of sample Python Panel scripts.

Hello, World!

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

```python from comet_ml import ui

ui.display("Hello world!") ```

Example 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().

```python from comet_ml import ui import matplotlib.pyplot as plt

names = ['group_a', 'group_b', 'group_c'] values = [1, 10, 100] plt.figure(figsize=(9, 3)) plt.subplot(131) plt.bar(names, values) plt.subplot(132) plt.scatter(names, values) plt.subplot(133) plt.plot(names, values) plt.suptitle('Categorical Plotting')

Note here that you can pass in the plt module:

ui.display(plt) ```

```python from comet_ml import ui import matplotlib.pyplot as plt

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

Note here that you can pass in the figure:

ui.display(fig) ```

Example 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.

```python 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.

Example using API, options, and metrics

This is actually the default Python Panel, with comments:

```python

Comet Python Panels BETA, full documentation available at:

https://www.comet.ml/docs/user-interface/python-panels/overview/

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])

Visualise the data

for experiment_key in metrics: for metric in metrics[experiment_key]["metrics"]: plt.plot(metric['steps'], metric['values'])

ui.display(plt) ```

Example 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.

```python 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()

metrics = api.get_metrics_for_chart( experiment_keys, options["metrics"])

plt.title("Metrics: " + (", ".join(options["metrics"]))) plt.xlabel("steps") plt.ylabel("value") plt.subplots_adjust(bottom=0.25)

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

plt.legend() ui.display(plt) ```

Example using PIL Image Library

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

```python 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) ```

Example getting assets

This example demonstrates getting an image asset from all expreiments, and displaying it. Note the current limitation of requiring the image to be in RGB format.

```python from comet_ml import API, ui import io from PIL import Image

api = API()

experiment_keys = api.get_panel_experiment_keys() for experiment_key in experiment_keys: asset_data = api.get_experiment_asset_list(experiment_key, "image") for asset in asset_data: image = api.get_experiment_asset(experiment_key, asset["assetId"], "raw") 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 the api.get_panel_experiments() that returns a list of APIExperiment() and use its methods directly, like:

```python from comet_ml import API, ui import io from PIL import Image

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"], "raw") if "." in asset["fileName"]: _, format= asset["fileName"].rsplit(".", 1) ui.display_image(image, format) ```

Example using Plotly Express

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

```python 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) ```

Example using dropdown

```python from comet_ml import ui

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

References for Panel Resources

For more information on Panels, see also: