Anna M
Anna M

Reputation: 77

How to make web browser.open() work on MacOS

I'm making a folium map tool in Python and need to make and executable for Windows, but I need a version for macOS also (Probably will convert to .app).

My code works fine on windows but webbrowser.open() does not work on macOS. Any ideas on how I can work around this?

Here's an example of code for a folium map:

import folium
import webbrowser

map = folium.Map(
    location=[39.82, -100],
    zoom_start=4)

folium.Marker(location=[39.82, -100]).add_to(map)

# Save map as html file and open in default web browser
map.save("Map.html")
webbrowser.open("Map.html")

Upvotes: 0

Views: 1282

Answers (1)

Anna M
Anna M

Reputation: 77

This took me a bit to figure out so here is the answer for anyone in the future who needs it:

import folium
import webbrowser
import os


map = folium.Map(
    location=[39.82, -100],
    zoom_start=4)

folium.Marker(location=[39.82, -100]).add_to(map)

filename = 'Map.html'
map.save(filename)

filepath = os.getcwd()
file_uri = 'file:///' + filepath + '/' filename
webbrowser.open_new_tab(file_uri)

Upvotes: 3

Related Questions