2

I'd like to save a list of ipyleaflet markers so I can load them into another ipyleaflet map later on. I have customized the markers using a MarkerCustom class in order to incorporate another attribute (duration), which works fine. Later on I also split them up into different clusters, so I do not want to save them as one cluster at this stage.

from traitlets import List
import ipyleaflet
from ipywidget import HTML

class MarkerCustom(Marker):
    duration=List().tag(sync=True, o=True) 

My markers are all part of a longer list named mark:

mark[0]=MarkerCustom(location=[df.loc[index]['latitude'], df.loc[0]['longitude']], icon=icon, draggable=False, title=tooltip, duration=duration_list, popup=HTML('custom text0'))
mark[1]=MarkerCustom(location=[df.loc[1]['latitude'], df.loc[1]['longitude']], icon=icon, draggable=False, title=tooltip, duration=duration_list,popup=HTML('custom text1'))

and so on.

How do I save the list mark and load it later on?

I have tried solutions using : shelve, pickle, and json, but none of these seem to work for this data type. I also looked into the geojson library (particularly here), but I am fairly new to this and not very familiar with classes yet. I have tried to adjust my custom Marker class in the following way:

from traitlets import List
import ipyleaflet
from ipywidget import HTML
import geojson


class MarkerCustom(Marker):

    duration=List().tag(sync=True, o=True)
    
    @property
    def __geo_interface__(self):
        return {self} 

and then tried to save one of the markers like this , e.g.:

geojson.dumps(mark[0])

But the error is: "TypeError: Object of type set is not JSON serializable"

If I use pickle instead, e.g.:

import pickle

with open("test", "wb") as fp:   
    pickle.dump(mark[0], fp)

The error I get is: "TypeError: cannot pickle '_hashlib.HMAC' object" (The error is the same if I use the entire list mark instead of just one marker, e.g. mark[0])

I have a lot of markers (>1000) in the list, so instead of saving each individual marker mark[0], mark[1] etc, I would prefer to save and load them as the list mark.

JoeMount
  • 21
  • 2
  • I would develop a factory function that received a dataframe parameter and return the `MarkerCustom` list. Ipywidgets takes some time to render multiple widgets so using pickle may be faster, can you add your attempt to use pickle? – itepifanio Jan 04 '23 at 13:38
  • Thanks for the reply - I've updated my question to include my attempt with pickle – JoeMount Jan 04 '23 at 22:05

0 Answers0