4

I have the jinja macro template provided to my code, which executes the Leaflet circle creation.

I would like to include the .csv data in this template when possible

df = pd.read_csv("survey.csv")

class Circle(folium.ClickForMarker):
_template = Template(u"""
        {% macro script(this, kwargs) %}
            var circle_job = L.circle();
            function newMarker(e){
                circle_job.setLatLng(e.latlng).addTo({{this._parent.get_name()}});
                circle_job.setRadius({{rad}});
                circle_job.getPopup({{role}})
                      if {{role}} = "Contractor" {
                      color="red"
                      }else{
                      color="black"
                      }
                circle_job.bringToFront()
                parent.document.getElementById("latitude").value = lat;
                parent.document.getElementById("longitude").value =lng;
                };
            {{this._parent.get_name()}}.on('click', newMarker);      
         {% endmacro %}
          """)  # noqa

  def __init__(self, popup=None):
    super(Circle, self).__init__(popup)
    self._name = 'Circle'

job_range = Circle()

for i,row in df.iterrows():
    lat =df.at[i, 'lat']
    lng = df.at[i, 'lng']
    sp = df.at[i, 'sp']
    phone = df.at[i, 'phone']
    role = df.at[i, 'role']
    rad = int(df.at[i, 'radius'])

is it possible something like this?

A similar approach was here:

How add circle markers to a rendered folium map embedded in a QWebEngineView?

UPDATE I:

I tried recently something like this:

class Circle(MacroElement):
 _template = Template(u"""
        {% macro script(this, kwargs) %}
            var {{this.get_name()}} = L.circle();
            function newCircle(e){ 
{{this.get_name()}}.setLatLng(e.latlng)
.addTo({{this._parent.get_name()}});
                {{this.get_name()}}.setRadius({{rad}});
                {{this.get_name()}}.setStyle({
                color: 'black',
                fillcolor: 'black'
                });
                };
            {{this._parent.get_name()}}.on('click', newCircle);      
        {% endmacro %}
        """)  # noqa

def __init__(self,
             popup=None
             ):
    super(Circle, self).__init__()
    self._name = 'Circle'

for i,row in df.iterrows():
  lat =df.at[i, 'lat']
  lng = df.at[i, 'lng']
  sp = df.at[i, 'sp']
  phone = df.at[i, 'phone']
  role = df.at[i, 'role']
  rad = int(df.at[i, 'radius'])


popup = '<b>Phone: </b>' + str(df.at[i,'phone'])
work_range = os.path.join('survey_range.geojson')
job_range = Circle()

Now I lost some features, whereas the Js console says nothing. Is it possible to fetch data from df.iterrows directly to the Macroelement?

UPDATE II

I tried to fiddle with def__init__ section and now my code looks as follows:

class Circle(MacroElement):

  def __init__(self,
             popup=None,
             draggable=False,
             edit_options=None,
             radius=rad
             #lat,
             #lng
             ):
    super(Circle, self).__init__()
    self._name = 'Circle',
    self.radius = radius,
    
    self._template = Template(u"""
        {% macro script(this, kwargs) %}
            var circle_job = L.circle();
            function newCircle(e){
                
 circle_job.setLatLng(e.latlng).addTo({{this._parent.get_name()}});
                circle_job.setRadius(50000);
                circle_job.setStyle({
                color: 'black',
                fillcolor: 'black'
                });
                };
            {{this._parent.get_name()}}.on('click', newCircle);      
        {% endmacro %}
        """)  # noqa
    
for i,row in df.iterrows():
lat =df.at[i, 'lat']
lng = df.at[i, 'lng']
sp = df.at[i, 'sp']
phone = df.at[i, 'phone']
role = df.at[i, 'role']
rad = int(df.at[i, 'radius'])

and the error thrown is: The rad is not defined

Is there any way of including the stuff from inside of df.iterrows() in the jinja2 template via defining the def___init?

Geographos
  • 827
  • 2
  • 23
  • 57

0 Answers0