Git Product home page Git Product logo

Comments (7)

santoshphilip avatar santoshphilip commented on May 23, 2024 1

I have not used it since I wrote it - so my recollection of this is a little fuzzy.

  • The API key has to be in the html for the page to open.
  • gmplot makes the page without the API key in the html
  • The code above opens the html file and inserts the API key in the right place in the html
  • The new html file (the old file is overwritten) will open.

You need to get the API key from google (the google maps page may guide on this)

Having said this, all of the above is from almost a year back. I have not tested the code to see if it still works. Things may have changed since then.

Good luck

from gmplot.

frmsaul avatar frmsaul commented on May 23, 2024 1

In case anybody encounters the same problem, here is the workaround i've been using for Jupyter / IPython.

from IPython.display import IFrame

def jupyter_display(gmplot_filename, google_api_key):
    """Hack to display a gmplot map in Jupyter"""
    with open(gmplot_filename, "r+b") as f:
        f_string = f.read()
        url_pattern = "https://maps.googleapis.com/maps/api/js?libraries=visualization&sensor=true_or_false"
        f_string = f_string.replace(url_pattern, url_pattern + "&key=%s" % google_api_key)
        f.write(f_string)
    return IFrame(gmplot_filename, width=990, height=500)

from gmplot.

vgm64 avatar vgm64 commented on May 23, 2024 1

Merged api key branch. Reopen if the issue is still present. fwiw, I've been using api-less plotting since this ticket was opened without issue. I understand the notebooks may be a bit different.

from gmplot.

santoshphilip avatar santoshphilip commented on May 23, 2024

a quick workaround until it is fixed:
(You need to install BeautifulSoup - pip install beautifulsoup4)

import gmplot

gmap = gmplot.GoogleMapPlotter(37.428, -122.145, 16)
gmap.draw("mymap.html")    
# "mymap.html" will NOT open

from bs4 import BeautifulSoup

def insertapikey(fname, apikey):
    """put the google api key in a html file"""
    def putkey(htmltxt, apikey, apistring=None):
        """put the apikey in the htmltxt and return soup"""
        if not apistring:
            apistring = "https://maps.googleapis.com/maps/api/js?key=%s&callback=initMap"
        soup = BeautifulSoup(htmltxt, 'html.parser')
        body = soup.body
        src = apistring % (apikey, )
        tscript = soup.new_tag("script", src=src, async="defer")
        body.insert(-1, tscript)
        return soup
    htmltxt = open(fname, 'r').read()
    soup = putkey(htmltxt, apikey)
    newtxt = soup.prettify()
    open(fname, 'w').write(newtxt)

insertapikey("mymap.html", apikey)
# "mymap.html" will open

I wrote this because I needed it right now :-)

from gmplot.

fuzunspm avatar fuzunspm commented on May 23, 2024

@santoshphilip could you please explain more about htmltxt and apikey? i couldn't manage to do it

from gmplot.

joemcmanus avatar joemcmanus commented on May 23, 2024

Here is my fix for using this in flask. All you need to do is add &key= to the already existing visualization line.
After you create the map run this snippet of code that uses replace() to add in the API key.

   #gmap does not have a apikey function, so lets substitute it. 
    tempHolder=''
    oldLine='<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=visualization&sensor=true_or_false"></script>'
    newLine='<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=visualization&sensor=true_or_false&key=yourKeyHere"></script>'
    #Open the file created by gmplot, do a find and replace. 
    #My file is in flask, so it is in static/map.html, change path to your file
    with open('static/map.html') as fh:
            for line in fh:
                    tempHolder += line.replace(oldLine,newLine)
    fh.close
   #Now open the file again and overwrite with the edited text
    fh=open('static/map.html', 'w')
    fh.write(tempHolder)
    fh.close

Hope that helps.

from gmplot.

leocorp96 avatar leocorp96 commented on May 23, 2024

Here is an update to @santoshphilip 's code:

def insertapikey(fname, apikey):
"""put the google api key in a html file"""
print('\n###############################')
print('\n Beginning Key Insertion ...')

def putkey(htmltxt, apikey, apistring=None):
    """put the apikey in the htmltxt and return soup"""
    if not apistring:
        apistring = "https://maps.googleapis.com/maps/api/js?key=%s&callback=initialize&libraries=visualization&sensor=true_or_false"
    soup = BeautifulSoup(htmltxt, 'html.parser')
    soup.script.decompose() #remove the existing script tag
    body = soup.body
    src = apistring % (apikey,)
    tscript = soup.new_tag("script", src=src, async="defer")
    body.insert(-1, tscript)
    return soup

htmltxt = open(fname, 'r').read()
soup = putkey(htmltxt, apikey)
newtxt = soup.prettify()
open(fname, 'w').write(newtxt)
print('\nKey Insertion Completed!!')

if name == 'main':
insertapikey("map.html", api_key)

NB:

  • The initMap() function has been changed to initialize()

  • And also removes the previous 'script' inserted by the gmplot map generation

from gmplot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.