Python Panel¶
A Python panel is a fully custom panel that allows you to use your own methods to explore, visualize, and analyze your data. When writing a Python Panel, you can use all of the standard data science Python modules, including: comet_ml
, matplotlib
, numpy
, pandas
, plotly
, scikit-learn
, seaborn
, and scipy
. For a full list, see below.
Use the Python panel in Comet to create custom visualizations when they are not already offered in the built-in panels accessible from the Panels Gallery.
Note
We are actively developing the Python Panels functionality, if you notice anything missing, do reach out to us on product@comet.com.
About the Python Panel Editor¶
The Panel Editor for the Python panel type has a unique layout, different from the default editor for all other panels.
The Python Panel Editor consists of two panes:
- The Editor pane to the left is used to provide and edit your custom code, set visualization options, add a description, and apply any relevant filters.
- The Preview pane displays a preview of the panel as currently defined in the editor, and running in the current project.You can also update the default panel name from here.
Get started with the Python Panel¶
Python Panels are powered by Comet Compute Beta which allow you to run you code on a personal secure server. Compute Compute uses CPython 3.9, so all of your regular Python libraries should work. In addition, you can use the comet_ml.ui
(described below) or any streamlit
code.
Note
There is a Compute Engine available for each user. If you are not viewing any Python Panels for 15 minutes, your Compute Engine may fall asleep. Refreshing the page will wake up your Compute Engine.
A simple Python Panel¶
Get a feel for Python Panels with this simple example.
Enter the following code in the Code tab:
from comet_ml import ui
ui.display("My first Python Panel!")
Click Run 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 comet_ml.ui.display()
.
Note
Python Panels do not automatically update 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.
See more examples on Python Panels.
Python SDK methods for Python Panels¶
API, ui, and APIExperiment classes in the Python SDK provide methods that you can call in your Python Panels code.
Note
Note that when using the comet_ml
SDK in Python Panels, you are only able to use the read-only functions. That is, you cannot create an Experiment()
, nor change any existing experiment data, etc.
API¶
The Comet API class contains methods that are useful for retrieving a group of experiments, or data from a group of experiments. The API reference document provides an overview of all available API methods.
Included in the API class are a number of methods that are only available within Python Panels:
API.get_panel_experiments()
: This will return a list containing all of the currentAPIExperiment
s. Specifically:- If on a Project View, the
APIExperiment
s from the visible experiment table page. - If a filter is set, all of the matching
APIExperiment
s. - If a filter is set for this Panel, the matching
APIExperiment
s.
- If on a Project View, the
API.get_panel_experiment_keys()
: Like theAPI.get_panel_experiments()
method, but returns a list of Experiment keys rather thanAPIExperiment
s.API.get_panel_metrics_names()
: Get the names of all metrics logged for all experiments in this Project.API.get_panel_project_id()
: Get Project ID. That information can be useful to retrieve other Project-level information.API.get_panel_project_name()
: Get Project name. That can be useful in creating report-like Panels.API.get_panel_workspace()
: Get Workspace name. That can be useful in creating report-like Panels.API.get_panel_size()
: Get the size of a current panel. Returns a tuple (width, height) in pixels.API.get_panel_width()
: Get the width of a current panel in pixels.API.get_panel_height()
: Get the height of a current panel in pixels.API.get_panel_experiment_colors()
: Get the colors of experiments shown in a panel. Returns a dictionary where the colors are in 6 hexadecimal character format "RRGGBB" (R is red, G is green, and B is blue, each between 0 and F)
{
[experiment_key_1]: {
"experimentKey": experiment_key_1,
"primary": string, // the default color of an experiment, should be used in majority of cases
"light": string // might be used for playing with highlighting
},
[experiment_key_2]: {
"experimentKey": experiment_key_2,
"primary": string,
"light": string
},
...
}
API.get_panel_metric_colors()
: Get the colors of metrics shown in a panel. Available on a single experiment page only. Returns a dictionary where the colors are in 6 hexadecimal character format "RRGGBB" (R is red, G is green, and B is blue, each between 0 and F)
{
[metric_1]: {
"primary": string, // the default color of a metric, should be used in majority of cases
"light": string // might be used for playing with highlighting
},
[metric_2]: {
"primary": string,
"light": string
},
...
}
In addition, below are general API methods commonly used in Python Panels:
API.get_metrics_for_chart()
: Get multiple metrics and parameters from a set of experiments. This method is designed to make custom charting of metrics easier.API.get_metrics_df()
: Get a DataFrame of multiple metrics from a set of experiments across a specified x-axis. This method is designed to make custom charting of metrics easier.
APIExperiment¶
When you call methods such as api.get_panel_experiments()
or api.query()
then you get back a list of APIExperiment
objects.
The Comet APIExperiment methods can be used within Python Panels to retrieve data for a specific experiment. All get_*
methods in this class are available in Python Panels. Below are some of the most commonly used methods:
APIExperiment.get_asset_list()
: Get a list of assets associated with the Experiment.APIExperiment.get_asset()
: Get an asset.APIExperiment.get_asset_by_name()
: Get an asset by name.APIExperiment.get_metrics()
: Get all of the logged metrics.APIExperiment.get_metrics_summary()
: Return the Experiment metrics summary.APIExperiment.get_parameters_summary()
: Return the Experiment parameters summary.APIExperiment.get_metadata()
: Get the metadata associated with the Experiment.APIExperiment.get_others_summary()
: Return the Experiment others summary.APIExperiment.get_model_asset_list()
: Get an Experiment model's asset list by model name.
ui¶
The Comet ui methods can be used within Python Panels to control the rendering of Panels. ui
contains three sets of methods:
- Display methods: To visualize different kinds of Python objects on the Panel canvas area
- Widgets methods: To add interactivity to Panels through elements that have a GUI representation
- Utility methods: To update the styling of Panels
Examples:
To use comet_ml.ui
, you need only import it:
from comet_ml import ui
choice = ui.dropdown("Choose one:", ["A", "B", "C"])
ui.display("You picked", choice)
streamlit¶
Python Panels running on Comet Compute can also use streamlit functions. The only limitation is that the streamlit sidebar is hidden.
Examples:
To use streamlit
, you need only import it:
import streamlit as st
choice = st.selectbox("Choose one:", ["A", "B", "C"])
st.write("You picked", choice)
See docs.streamlit.io for more information.
Debugging¶
Note that using print()
will not display items in the panel display. However, you can simply put the value that you wish to display on a line by itself. For example, change this:
# Don't do this:
print(value)
into the following:
# Do this
value
value
can be any valid Python value (string, number, bool, etc). You can even get information on Python modules, functions, and objects with this. This can be useful for debugging. For more information, see Streamlit magic commands.
Use standard Python modules¶
Python Panels allows you to use many other Python support modules, including:
altair
biopython
bokeh
boto3
comet_ml
fastparquet
geopandas
jsonschema
matplotlib
msgpack
nltk
numpy
opencv-python
pandas
Pillow
(PIL - Python Image Library)plotly
scikit-learn
scipy
simplejson
streamlit
To use a Python package that is not listed here, contact us through our Slack channel.
Know the limitations¶
- Python Panels do not automatically update 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.
- These are the supported widget types, but you can also use any streamlit widget.
- All of your Python Panels share the same Python environment. There are a few widely-used ideoms that you will want to avoid because of this. For example:
Instead of using:
for experiment_key in metrics:
for metric in metrics[experiment_key]["metrics"]:
# DO NOT DO THIS:
plt.plot(metric['steps'], metric['values'])
ui.display(plt)
you should use:
figure, ax = plt.subplots()
for experiment_key in metrics:
for metric in metrics[experiment_key]["metrics"]:
ax.plot(metric['steps'], metric['values'])
ui.display(figure)
That is, insead of using matplotlib's pyplot
global API, you should use local figure and ax.
Backward Incompatibilities¶
There are a few differences between the previous version of Python Panels and those supported by Comet Compute. In the following list, st
stands for streamlit
, ui
stands for comet_ml.ui
, and api
stands for comet_ml.API()
.
ui.sidebar()
has been removed, and the sidebar created withst.sidebar()
is not visible. Users can usest.columns()
orst.tabs()
insteadapi.project_id
has been removed; useapi.get_panel_project_id()
instead- All of the
api.get_experiment_*()
methods have been moved toAPIExperiment().get_*()
methods - The module
js
has been removed ui.get_theme_names()
has been removed- The
theme
option inui.display()
is no longer supported Experiment.get_asset(..., "raw")
should now beExperiment.get_asset(..., "binary")
- It used to be the case if a user defined a
main()
method that it would be called automatically and was used to separate long-running code from other code. Support formain()
has been removed. Now, users can use streamlit cache and state functions.
If you find any additional limitations or have a request, let us know.