Attribute error with Mapboxgl

Hello! I am new to Anvil and was recently following a webinar hosted by Brooke Myers on integrating Mapbox into a map app. I am very interested in creating web-based maps using Mapbox but staying in Python-land if possible. Anvil seems like it would be a great tool to make this happen.

But in following the webinar, I didn’t get very far before running into an error with an import statement for mapboxgl specifically. I’m getting an attribute error on Line 3 below, where I’m attempting to import mapboxgl:

from ._anvil_designer import Form1Template
from anvil import *
from anvil.js.window import mapboxgl
import anvil.js
import anvil.http


class Form1(Form1Template):

  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Any code you write here will run when the form opens.

  def form_show(self, **event_args):
    """This method is called when the HTML panel is shown on the screen"""
    mapboxgl.accessToken = 'pk.eyJ1Ijoid3dyaWdodDIxIiwiYSI6ImNsNW1qeDRpMDBjMHozY2tjdmdlb2RxcngifQ.od9AXX3w_r6td8tM96W_gA';
    self.map = mapboxgl.Map({
    'container': anvil.js.get_dom_node(self.spacer_1), 
    'style': 'mapbox://styles/mapbox/streets-v11',
    'center': [-74.5, 40],
    'zoom': 9,
    'projection':'globe'
    }); 

Here’s the error I’m getting:

AttributeError: 'Window' object has no attribute 'mapboxgl'

As far as I can tell, I’ve pretty much copy / pasted from the webinar but not getting a map to render. Any help is greatly appreciated!

Hi @williamcwrightjr and welcome to the forum!

Did you add the following lines to Native Libraries?

<script src='https://api.mapbox.com/mapbox-gl-js/v2.1.1/mapbox-gl.js'></script>
<link href='https://api.mapbox.com/mapbox-gl-js/v2.1.1/mapbox-gl.css' rel='stylesheet' />
1 Like

Thank you, Brooke! This was 100% the issue. Just wasn’t following the webinar closely enough

1 Like

I’m glad it was easy to solve :smiling_face: