3

I'm working on a script that will update automatically some folium maps in google drive daily.

I got the whole thing working correctly, locally, without issues, except for one thing: since this will be running automatically on a remote server daily, I don't want to save the created maps in the server first and then upload them to google drive. If possible, I would like to create the maps as some kind of file object in the memory, upload them to google drive, and that's it... without having to create multiple physical map files on the server. Is this possible? If yes, how?

This is my code where, after placing markers and clusters, I save the maps as html files in the file system (this is inside a cycle, as I need to update maps for several cities):

    folium.LayerControl().add_to(mymap) 
    mymap.add_child(MeasureControl()) 
    mymap.render()
    mymap.save('leads_'+city+'_'+code+'.html')

Then, for each map, this is the code for saving the html map into google drive (using pydrive):

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)

file1 = drive.CreateFile({'parents': [{'id': 'xxxfolder_idxxx'}]})
file1.SetContentFile('leads_'+city+'_'+code+'.html')
file1.Upload()

As I said, this all works fine, but I don't want to saturate the server file system with map files. Is there a way to do this without having to first save the map in the local file system?

Alain
  • 339
  • 3
  • 19
  • How many times are you saving a file each run? Is overwriting the file in drive not an acceptable workaround? – NightEye May 27 '22 at 22:54
  • 1
    Oh yeah, I do overwrite the files in drive. That's not the issue. The problem is the step in between the map creation and the drive upload... it's when saving the map in the server file system. I don't want to save multiple map files in the server where the script runs... I just want to save (or overwrite) them in the drive folder. – Alain May 27 '22 at 23:17

1 Answers1

1

If you meant to directly save the html file to drive (without saving it to local), then you can try this:

file1 = drive.CreateFile({'parents': [{'id': 'xxxfolder_idxxx'}], 'mimeType': 'text/html', 'name': 'leads_'+city+'_'+code+'.html'})
# access the html string of the object and set it via SetContentString
file1.SetContentString(m.get_root().render())
file1.Upload()

Reference:

NightEye
  • 10,634
  • 2
  • 5
  • 24