How to save Plotly files and display From DBFS

Learn how to save Plotly files and display them from DBFS.

Written by Adam Pavlacka

Last published at: May 19th, 2022

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:
    %python
    
    data = {'data': [{'y': [4, 2, 3, 4]}],
                'layout': {'title': 'Test Plot',
                           'font': dict(size=16)}}
    p = plot(data,output_type='div')
    displayHTML(p)
    Sample plot image.
  2. Save the generated plot to a file with plotly.io.write_image():
    %sh
    
    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:
    %sh
    
    dbutils.fs.cp("file:/databricks/driver/plotly_images/<imageName>.jpg", "dbfs:/FileStore/<your_folder_name>/<imageName>.jpg")
  4. Display the image using displayHTML():
    %sh
    
    displayHTML('''<img src="/files/<your_folder_name>/<imageName>.jpg">''')


See also Plotly in Python and R Notebooks.

Was this article helpful?