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.
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)
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)
Copy the file from the driver node and save it to DBFS:
dbutils.fs.cp("file:/databricks/driver/plotly_images/<imageName>.jpg", "dbfs:/FileStore/<your_folder_name>/<imageName>.jpg")
Display the image using
displayHTML()
:displayHTML('''<img src="/files/<your_folder_name>/<imageName>.jpg">''')
See also Plotly in Python and R Notebooks.