Python Panels - BETA¶
Welcome to the Comet Python Panel Beta!
Comet Python Code Panels allow you to write custom visualizations the same way that you write all of your other scripts and using the Python modules that you know and love.
When writing a Python Panel, you can use all of the standard data
science Python modules, including:
If you would like to jump to the examples, they are here:
Steps to creating a Custom Python Panel¶
Go to a Project or Experiment View.
Click on "Add Panel" (either in the panel area on the Project View, or on the Panel Tab of the Experiment View).
+ Create New
That should display the following screen:
The left-hand portion of the Panel editor contains tabs for:
Here you enter on the Python code for this panel. Every saved change creates a new version that you can always revert to at any time.
Keyboard short cuts:
|PC short cut||Mac short cut||Functionality|
These are options that you can set for each instance of a Panel. They are useful for having items that can be varied without having to edit the code. For example, you could have an option for a title of a chart.
A description of the Panel and thumbnail of the Panel. Click the image to create or edit the thumbnail. We suggest putting author, and details about how the Panel works in the description. This is searchable text on the Panel Gallery.
You can provide a default query for your Panel. For example, perhaps your Panel only works for experiments with a particular hyperparameter value. You can define the query here. Of course, many Panels will work without setting a specific filter.
You can override this when you add an instance of the Panel to a Project View.
A Simple Python Panel¶
Enter the following code in the Code tab:
from comet_ml import ui ui.display("My first Python Panel!")
Click on the
Run button to display the output in the Panel Preview
area on the right-hand side.
To display items on the Panel canvas, you need to import the
comet_ml.ui library, and call
ui.display(). See ui for
addition details on
Python Panels do not automatically update with new experiments nor upon receiving new logged metrics. This is by design, as your Python Panel may be expensive to compute, and you may not wish to have it automatically refresh whenever new data is received.
The following sections provides details on accessing Comet-logged data
API, details on the User Interface
ui and end-to-end
comet_ml.API, first import it and make an instance of it:
from comet_ml import API api = API()
Then you will have access to the following methods. You can find additional documentation on the full Comet Python Panels API SDK.
API Panel Methods¶
These methods provide functionality related to the current panel.
Get the JSON Options data as a Python dictionary.
from comet_ml import API api = API() options = api.get_panel_options()
The Panel Options can be edited when in the Options tab of the Panel Code editor, and set when you are adding a Panel instance to a View.
Get the experiment keys selected for this Panel.
from comet_ml import API api = API() experiment_keys = api.get_panel_experiment_keys()
This will return a list containing all of the current experiment keys. Specifically:
- If on a Project View, the experiment keys from the visible experiment table page
- If a filter is set, all of the matching experiments
- If a Query is set for this Panel, the matching experiments
APIExperiments selected for this Panel.
from comet_ml import API api = API() experiment = api.get_panel_experiments()
Like the API.get_experiment_keys()
method, but returns a list of
APIExperiments rather than just their
You can find additional documentation at Python Panels APIExperiment.
project_id for this Panel.
from comet_ml import API api = API() project_id = api.get_panel_project_id()
project_id can be useful to retrieve other Project-level information.
project_name for this Panel.
from comet_ml import API api = API() project_name = api.get_panel_project_name()
The Project name could be useful in creating report-like Panels.
workspace (name) for this Panel.
from comet_ml import API api = API() workspace = api.get_panel_workspace()
The Workspace name could be useful in creating report-like Panels.
Get the names of all metrics logged for all experiments in this Project.
from comet_ml import API, ui api = API() metrics_names = api.get_panel_metrics_names() metric_name = ui.dropdown("Select a metric name:", metrics_names)
As shown, the metrics_names could be useful in creating a dropdown selection for plotting.
Note that this list includes all metrics, and system metrics. If you wish, you could use only those names starting (or not starting) with "sys".
comet_ml.ui, you need only import it:
from comet_ml import ui ui.display("Hello")
ui contains the following methods:
ui Display methods¶
These methods are described below.
ui.display() method is used to visualize different kinds of
Python objects on the Panel canvas area.
ui.display(*items, format=None, **kwargs)
You can use
ui.display() on one (or more) of any of the following
types of Python
PillowPython Images Library (PIL) images
- HTML strings (including SVG images)
- Matplotlib figures
- Plotly plots
- Pandas' Dataframes
- Any object that has a
In addition, there are specialized display methods for text, markdown, and images represented as raw strings (as logged as image assets, for example). We'll describe each of those methods below.
If you wish to send multiple items to the display area, pass them to
ui.display() or call
from comet_ml import ui ui.display("hello") ui.display("world") # or ui.display("hello", "world")
format argument can be either "text", "markdown", "html" or
None. The default is "html"/None.
kwargs are optional, and varying depending on the type of item being
displayed. For example, if the
item is a pandas Dataframe, then you
may also pass in these keyword arguments:
- theme: the name of a color theme (see below)
- font_size: name of the size, eg 'normal'
- font_family: name of the font family
- text_align: which side to align to
- width: the width of table, eg, 'auto'
- index: the index to highlight
- even_color: color of even rows
- even_bg_color: background color of even rows
Theme names: 'yellow light', 'grey light', 'blue light', 'orange light', 'green light', 'red light', 'yellow dark', 'grey dark', 'blue dark', 'orange dark', 'green dark', 'red dark', or 'comet'.
For more details on
ui.display(), see the Python Panel Examples.
This method is used to display a matplotlib figure.
Displays a matplotlib figure.
from comet_ml import ui import matplotlib.pyplot as plt # plt commands here ui.display(plt)
Or calling with the figure:
from comet_ml import ui import matplotlib.pyplot as plt fig, ax = plt.subplots() # Figure commands here ui.display(fig)
This method is used to display an image, either from a logged asset, or from a PIL Image.
Example from logged asset:
from comet_ml import API, ui api = API() for experiment in api.get_panel_experiments(): for json in experiment.get_asset_list("image"): if json["fileName"].endswith("jpg"): data = experiment.get_asset(json["assetId"]) ui.display_image(data, format="jpg")
Displays image strings in the given format ("jpg", "gif", or "png").
Example from PIL Image:
from comet_ml import ui from PIL import Image import random # Create a PIL Image image = Image.new("RGB", (500, 500)) # process image here ui.display(image)
For more details, see the Python Panel Examples.
This method is useful for displaying plain text.
Displays text that otherwise would have characters interpreted as HTML.
This method is useful for displaying text formatted as markdown.
Displays text as markdown, ignoring any indentation.
ui Widget methods¶
This section describes so-called
widgets. These are elements that
have a GUI representation, and trigger a change to re-run your
code. This is best shown through an example.
ui.dropdown() element does two things at once:
- creates a dropdown (selection) list of options on the Panel canvas
- returns the selected item
choice = ui.dropdown(label, options, index=0, format_func=None, key=None)
Let's take a look at a specific example.
from comet_ml import ui choice = ui.dropdown("Choose one:", ["A", "B", "C"]) ui.display("You picked", choice)
When you first run this code, you will see:
Note that this is very different from the usual manner of creating GUI
elements. In this manner, there are no "callbacks" but merely the
above code. By default, the dropdown has been shown on the screen and
the default options (index=0) has been selected. The code continues,
and so you see choice "A" already set as the
If you then select a different item, your code runs again, updating the GUI and the selected item:
If you would like to separate code that should only run once (say, because it is expensive to compute) you can separate the code to run when the GUI is updated by placing it in a main function, like this:
from comet_ml import ui # Code that is expensive to run: choices = ... async def main(): # The fast GUI-based code: choice = ui.dropdown("Choose one:", choices) ui.display("You picked", choice)
index is a numeric value representing the row to show
as the initial choice.
format_func is a function that takes a row from options, and
returns a string to be used in the dropdown selection list. This is
useful if you would like to provide to options something other than
what you would like displayed. For example, consider:
from comet_ml import API, ui api = API() api_experiments = api.get_panel_experiments() api_experiment = ui.dropdown( "Choose an experiment by key:", api_experiments, format_func= lambda experiment: experiment.key )
In this example, the experiment's key is used in the dropdown list,
but options is a list of
APIExperiments. In this manner, you can
pass in options in any format, but you should then provide a
format_func for easy selection.
ui Utility methods¶
These are the ui utility functions:
These are described below.
Get the names of color themes for displaying pandas' Dataframes.
from comet_ml import ui color_theme_names = ui.get_theme_names() color_theme_name = ui.dropdown.dropdown( "Select a theme:", color_theme_name ) # create a draframe here ui.display(df, theme=color_theme_name)
As shown, the color_theme_names could be useful in creating a dropdown selection showing a Dataframe.
This method allows you to set additional CSS for items for display.
This is experimental and may be removed in a future version.
This method allows you to add CSS for items for display.
This is experimental and may be removed in a future version.
If a panel is reporting an error, then it could be caused by the following:
Using an unsupported browser. Currently Python Panels is known to work under the following browsers:
- Chrome, version 71.0 and greater
- Firefox, version 70.0 and greater
- Edge, version 80 and greater
The panel has an error. If this is your panel, you can edit it to fix what is wrong. If it is another user's panel, you can contact them to let them know there is an issue.
Note that using
print() will display items in the Console area
(bottom, right-hand corner). This is very useful for debugging.
Python Panels also allows you to use many other Python support
Python Panels run in the browser rather than from a server. That has a number of implications. First and foremost, there is a bit of an overhead to load the libraries needed in order to execute Python code in the browser. Typically, this delay only happens when required, typcially once per project or experiment view. Note also, that Python Panels can use more memory than usual.
To create responsive Python Panels, it is suggested to:
- limit the number of modules loaded
- limit the number of requests (e.g., api.get_...() calls)
- limit the amount of information displayed
The following Python modules are known to require noticeable load times:
- matplotlib (medium load time)
- pandas (large load time)
- Plotly Express (large load times, as it requires pandas)
- Python Panels do not automatically update with new experiments nor upon receiving new logged metrics. This is by design, as your Python Panel may be expensive to compute, and you may not wish to have it automatically refresh whenever new data is received.
- Limited types of widgets: currently, there is only
ui.dropdown(); what else would you like?
- Line numbers in Python's tracebacks are off; see your webbrowser's Console for current line numbers
DataFrame.styleis not available
References for Panel Resources¶
For more information on Panels, see also: