Text label alongside JSON

Reading javascript and converting to python is tricky.

Taking the example from the original blog post and combining it with this example from the map box docs.

    def form_show(self, **event_arg):
        self.mapbox = mapboxgl.Map(
            {
                "container": self.dom,
                #'style': 'mapbox://styles/mapbox/streets-v11',
                "style": "mapbox://styles/brookemyers/cklk04z7x1f5d17pedafupa3e",
                "center": [0.1218, 52.2053],  # center on Cambridge
                "zoom": 12,
            }
        )
        self.mapbox.on("load", self.on_load)
        ...
        
    def on_load(self, *args):
        url = "https://docs.mapbox.com/mapbox-gl-js/assets/cat.png"
        self.mapbox.loadImage(url, self.on_load_image)

    def on_load_image(self, error, image):
        if error:
            raise error
        self.mapbox.addImage("cat", image)

        self.mapbox.addSource(
            "point",
            {
                "type": "geojson",
                "data": {
                    "type": "FeatureCollection",
                    "features": [{"type": "Feature", "geometry": {"type": "Point", "coordinates": [0.1218, 52.2053]}}],
                },
            },
        )
        self.mapbox.addLayer(
            {
                "id": "points",
                "type": "symbol",
                "source": "point",  # // reference the data source
                "layout": {"icon-image": "cat", "icon-size": 0.25},  # // reference the image
            }
        )

This is a case of a library using callback as part of their api.

Whenever the library requires a callback I provide it with a method on the form.

Side note: If using a python function as a javsacript callback it’s often necessary to use *args since the javascript library may provide you with additional args you don’t need (and that are not documented :disappointed: )

2 Likes