Join the Kedro community

Updated 4 weeks ago

Displaying Plotly Visualizations in MLflow

At a glance

The community member is trying to incorporate Plotly visualizations in their MLflow pipeline. They have created a function to generate a Plotly figure and stored it in the catalog using the kedro_mlflow.io.artifacts.MlflowArtifactDataset dataset. However, when viewing the MLflow UI, the figure is only displayed as a JSON file, not as a Plotly visualization.

The community members discuss various approaches to resolve this issue. One suggestion is to use mlflow.log_artifact to store the figure as an artifact, which can then be viewed in the MLflow UI. Another suggestion is to store the figure as HTML instead of JSON, using the plotly.HTMLDataset dataset. This approach is confirmed to work by one of the community members.

<answer>The community member was able to resolve the issue by using the <code>plotly.HTMLDataset</code> dataset to store the Plotly figure, which allowed the visualization to be displayed correctly in the MLflow UI.</answer>
Useful resources

Hello all ! I'm trying to incorporate plotly visualizations in my mlflow. I've created a simple function:

def make_plotly():
    import plotly.express as px
    df = px.data.iris()
    fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
                    size='petal_length', hover_data=['petal_width'])
    fig.show()
    return fig 
And created a pipeline where the output of the associated node is stored in the catalog
plotly_fig:
  type: kedro_mlflow.io.artifacts.MlflowArtifactDataset
  dataset:
    type: plotly.PlotlyDataset
    filepath: data/08_reporting/plotly.json
    plotly_args:
      type: scatter

After running the pipeline, the fig properly shows up in a tab, then is stored in a json format. However, in mlflow ui, it only appears as the json file (see attached image).

  • Is there a way to show the plotly fig in mlflow ? Do I miss something ?
  • Also, what args would you recommend to save and load the plotly fig ?

@Philippe Martin, I tried using plotly.HTMLDataset instead of plotly.JSONDataset and it worked

shuttle_passenger_capacity_plot_go:

  type: kedro_mlflow.io.artifacts.MlflowArtifactDataset
  dataset:
    type: plotly.HTMLDataset
    filepath: data/08_reporting/shuttle_passenger_capacity_plot_go.html

View full solution
1
H
P
G
7 comments

hey @Philippe Martin it seems like MLflow provides mlflow.log_artifact . it is stored in the artifact repository associated with the current MLflow run. These artifacts can then be viewed and downloaded from the MLflow UI. Perhaps this can help to visualise your plotly in MLFlow?

https://mlflow.org/docs/latest/traditional-ml/hyperparameter-tuning-with-child-runs/notebooks/logging-plots-in-mlflow.html

Unfortunatly no.. the artifacts is well saved as a json file in the artificat repository, but it doesn't show as a plotly figure in the MLflow UI, it shows as a json file. Maybe the error comes from how I stored the figure in the catalog ?

plotly_fig:
  type: kedro_mlflow.io.artifacts.MlflowArtifactDataset
  dataset:
    type: plotly.PlotlyDataset
    filepath: data/08_reporting/plotly.json
    plotly_args:
      type: scatter
Thank you for your help

I also tried chaning to type plotly.JSONDataset instead of plotly.PlotlyDataset, but same issue ..

Hi @Philippe Martin
It is possible to log plotly figure with mlflow so that they show in mlflow UI. The artifact needs to be stored as HTML.

Look into mlflow.log_figure. If I recall properly, it should be called that way:

mlflow.log_figure(figure=figure, artifact_file=f"{name}.html")

where figure is your plotly figure.

I am not just sure how it would work with the plotly dataset.

The plotly dataset in kedro-datasets returns a figure object, which works well with Kedro-Viz. However, MLflow doesn't support this json format. A great idea would be to create an plotly.MLFlowDataset dataset that would return the html as you said. We are already have a plotly.HTMLDataset

@Philippe Martin, I tried using plotly.HTMLDataset instead of plotly.JSONDataset and it worked

shuttle_passenger_capacity_plot_go:

  type: kedro_mlflow.io.artifacts.MlflowArtifactDataset
  dataset:
    type: plotly.HTMLDataset
    filepath: data/08_reporting/shuttle_passenger_capacity_plot_go.html

Amazing, thank you !!

Add a reply
Sign up and join the conversation on Slack