How to Save Plotly Files and Display From DBFS

You can save a chart generated with Plotly to the driver node as a jpg or png file. Then, you can display it in a notebook by using the displayHTML() method. By default, you save Plotly charts to the /databricks/driver/ directory on the driver node in your cluster. Use the following procedure to display the charts at a later time.

  1. Generate a sample plot:

    data = {'data': [{'y': [4, 2, 3, 4]}],
                'layout': {'title': 'Test Plot',
                           'font': dict(size=16)}}
    p = plot(data,output_type='div))
    displayHTML(p)
    
    ../_images/sample-plot.png
  2. Save the generated plot to a file with plotly.io.write_image():

    plotly.io.write_image(fig=data,file="/databricks/driver/plotly_images/<imageName>.jpg", format="jpeg",scale=None, width=None, height=None)
    
  3. Copy the file from the driver node and save it to DBFS:

    dbutils.fs.cp("file:/databricks/driver/plotly_images/<imageName>.jpg", "dbfs:/files/<your_folder_name>/<imageName>.jpg")
    
  4. Display the image using displayHTML():

    displayHTML('''<img src="/files/<your_folder_name>/<imageName>.jpg">''')
    

See also Plotly in Python and R Notebooks.