cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to programmatically download png files from matplotlib plots in notebook?

Hunter
New Contributor III

I am creating plots in databricks using python and matplotlib. These look great in notebook and I can save them to the dbfs using

plt.savefig("/dbfs/FileStore/tables/[plot_name].png")

I can then download the png files to my computer individually by pasting a url in my browser like

https://[address]/files/tables/[plot_name].png

What I'm wondering is if there is a way to run this url and download the png file for a plot programmatically when I run a notebook

1 ACCEPTED SOLUTION

Accepted Solutions

pawelmitrus
Contributor

Hello Hunter,

I think that you pushed it too far - FileStore download via web browser is a cool feature, when you want to download file to your local in the quickest possible way.

Do you have an external storage mounted to you workspace? Storages like Azure Storage, S3 has their own APIs, multiple SDKs and you should be heading that direction to download it programatically.

Hope it helps!

View solution in original post

7 REPLIES 7

Hunter
New Contributor III

Thank you!

jose_gonzalez
Databricks Employee
Databricks Employee

Hi Hunter,

FileStore is a special folder within Databricks File System (DBFS) where you can save files and have them accessible to your web browser. In your case it the png files will be saved into /FileStore/plots which contains images created in notebooks when you call display() on a Python or R plot object, such as a ggplot or matplotlib plot. If you delete files in this folder, you may have to regenerate those plots in the notebooks that reference them.

If you would like to download the files, then you will need to follow the instructions from here to create the url https://docs.databricks.com/data/filestore.html#save-a-file-to-filestore

Thank you.

pawelmitrus
Contributor

Hello Hunter,

I think that you pushed it too far - FileStore download via web browser is a cool feature, when you want to download file to your local in the quickest possible way.

Do you have an external storage mounted to you workspace? Storages like Azure Storage, S3 has their own APIs, multiple SDKs and you should be heading that direction to download it programatically.

Hope it helps!

shan_chandra
Databricks Employee
Databricks Employee

Hi Hunder, Please find the below steps to download the png file

  1. Copy the png file to a file store location Eg. -  dbfs:/FileStore/tables/DeltaLakeCheatSheet.png
  2. In a separate browser tab, I entered the below URL to automatically download the file to the local.

https://<mycompany.databricks.com>/files/tables/DeltaLakeCheatSheet.png?o=######

a. Replace <mycompany.databricks.com> with the URL you have used to login to databricks

b. ?o=######" where the number after o= is the same as in your URL

Also, the below example, which displays a hyperlink to the png file which can be used to download the png file

displayHTML("""
<p> This is a link <a href="files/tables/DeltaLakeCheatSheet.png?o=######"> link</a> to the document </p>
""")

Hunter
New Contributor III

Thanks for the html trick Shan_Chandra, I'll use that in my reports.

Hunter
New Contributor III

Thanks everyone! I am already at a place where I can download a png to FileStore and use a url to download that file locally. What I was wondering was if there is some databricks function I can use to launch the url that references the png file and download it. But it sounds like that may not be possible currently.

peter_mcnally
New Contributor III

Hi Hunter,

Did you ever find a way to automatically download? I tried going the route of running a jupyter instance on my local laptop and using the requests package to hit the Databricks Filestore url, but I am getting an error. Wondering if you ended up solving this.

Cheers

Peter

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group