cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
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
New Contributor III

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

9 REPLIES 9

Kaniz
Community Manager
Community Manager

Hi @ Hunter ! My name is Kaniz, and I'm the technical moderator here. Great to meet you, and thanks for your question! Let's see if your peers on the community have an answer to your question first. Or else I will follow up with my team and get back to you soon.Thanks.

Hunter
New Contributor III

Thank you!

jose_gonzalez
Moderator
Moderator

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
New Contributor III

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
Honored Contributor III
Honored Contributor III

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.

Kaniz
Community Manager
Community Manager

Wow !

That's awesome😀 @Hunter Clark​ .

I'm happy to see that your query is resolved.

Please keep posting your queries and enjoy!

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

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.