mealesbia
mealesbia

Reputation: 935

Python: How to open map created with folium in browser when running application

I have a very small application written in PyCharm using python3:

import folium

map = folium.Map(location=[58.1, 23.3], zoom_start=10)
map.save('map2.html')

This will create a map2.html which I can access in my browser by using pycharm and the url looks like: http://localhost:63342/iss-country/map2.html?_ijt=dcsefdg8om4ddfovlt5ooq6ro5

How can I automatically open this in my browser? So when I run the application it does not only generate the html page but also visits it immediatley. I found the webbrowser module which can be useful but how do I know the correct localhost url?

Upvotes: 2

Views: 7682

Answers (3)

Louie Lee
Louie Lee

Reputation: 283

You can probably try this:

my_map.show_in_browser()

Upvotes: 3

uranium13
uranium13

Reputation: 1

Add:

webbrowser.open("http://localhost:80/"+output_file, new=2)

Upvotes: 0

Bill--
Bill--

Reputation: 170

I don't see the issue with using the webbrowser module. Just make the file name and path a variable and call the webbrowser open method.

output_file = "map2.html"
map = folium.Map(location=[58.1, 23.3], zoom_start=10)
map.save(output_file)
webbrowser.open(output_file, new=2)  # open in new tab

Upvotes: 4

Related Questions