question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

show folium outside of ipython notebooks

See original GitHub issue

Hey guys i am trying to run one of the examples you provide. However, I am unable to render it. It’s probably because I am using it in a normal python script inside the PyCharm. Is there any functionality similar to matplotlib show()? I tried render() but it does not do anything…

import folium
from folium.plugins import HeatMap
import numpy as np
print(folium.__version__)
#0.6.0

data = (np.random.normal(size=(100, 3)) *
        np.array([[1, 1, 1]]) +
        np.array([[48, 5, 1]])).tolist()
m = folium.Map([48., 5.], tiles='stamentoner', zoom_start=6)
HeatMap(data).add_to(m)
m
# <folium.folium.Map object at 0x7f4c56e14b70>
m.render()
# nothing happens


Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:20 (14 by maintainers)

github_iconTop GitHub Comments

23reactions
Conengmocommented, Aug 30, 2018

If you want to open your map in your web browser you could do something like this:

import os
import webbrowser
filepath = 'C:/whatever/map.html'
m = folium.Map()
m.save(filepath)
webbrowser.open('file://' + filepath)

On my Windows machine this opens the map in my default webbrowser. No need to run a server or anything.

Does this work for you?

2reactions
Demetrio92commented, Aug 30, 2018

Proof of concept. Tested with python 3.5 and folium 0.6.0

import numpy as np
import folium
from folium.plugins import HeatMap

# create map
data = (np.random.normal(size=(100, 3)) *
        np.array([[1, 1, 1]]) +
        np.array([[48, 5, 1]])).tolist()
folium_map = folium.Map([48., 5.], tiles='stamentoner', zoom_start=6)
HeatMap(data).add_to(folium_map)

# this won't work:
folium_map
folium_map.render()



# ------------------------------------------------------------------------------------------------
# so let's write a custom temporary-HTML renderer
# pretty much copy-paste of this answer: https://stackoverflow.com/a/38945907/3494126
import subprocess
import webbrowser
from http.server import BaseHTTPRequestHandler, HTTPServer


PORT = 7000
HOST = '127.0.0.1'
SERVER_ADDRESS = '{host}:{port}'.format(host=HOST, port=PORT)
FULL_SERVER_ADDRESS = 'http://' + SERVER_ADDRESS


def TemproraryHttpServer(page_content_type, raw_data):
    """
    A simpe, temprorary http web server on the pure Python 3.
    It has features for processing pages with a XML or HTML content.
    """

    class HTTPServerRequestHandler(BaseHTTPRequestHandler):
        """
        An handler of request for the server, hosting XML-pages.
        """

        def do_GET(self):
            """Handle GET requests"""

            # response from page
            self.send_response(200)

            # set up headers for pages
            content_type = 'text/{0}'.format(page_content_type)
            self.send_header('Content-type', content_type)
            self.end_headers()

            # writing data on a page
            self.wfile.write(bytes(raw_data, encoding='utf'))

            return

    if page_content_type not in ['html', 'xml']:
        raise ValueError('This server can serve only HTML or XML pages.')

    page_content_type = page_content_type

    # kill a process, hosted on a localhost:PORT
    subprocess.call(['fuser', '-k', '{0}/tcp'.format(PORT)])

    # Started creating a temprorary http server.
    httpd = HTTPServer((HOST, PORT), HTTPServerRequestHandler)

    # run a temprorary http server
    httpd.serve_forever()


def run_html_server(html_data=None):

    if html_data is None:
        html_data = """
        <!DOCTYPE html>
        <html>
        <head>
        <title>Page Title</title>
        </head>
        <body>
        <h1>This is a Heading</h1>
        <p>This is a paragraph.</p>
        </body>
        </html>
        """

    # open in a browser URL and see a result
    webbrowser.open(FULL_SERVER_ADDRESS)

    # run server
    TemproraryHttpServer('html', html_data)

# ------------------------------------------------------------------------------------------------


# now let's save the visualization into the temp file and render it
from tempfile import NamedTemporaryFile
tmp = NamedTemporaryFile()
folium_map.save(tmp.name)
with open(tmp.name) as f:
    folium_map_html = f.read()

run_html_server(folium_map_html)
Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Folium map not displaying - Stack Overflow
To display in the iPython notebook, you need to generate the html with the myMap._build_map() method, and then wrap it in an iFrame...
Read more >
Map Visualizations in Python Using Folium - Pluralsight
To set up, open a new notebook and run the installation for both Folium and pandas by running the command below on a...
Read more >
Quickstart — Folium 0.12.1 documentation - GitHub Pages
To create a base map, simply pass your starting coordinates to Folium: ... To display it in a Jupyter notebook, simply ask for...
Read more >
python-visualization/folium - Gitter
I had a homegrown solution for this (e.g. http://54.242.2.146:9999/notebooks/Sample/Timeseries_Sample.ipynb, cell 37) but want to move to something more ...
Read more >
Jupyter Notebook files
We can do the same for interactive material. Below we'll display a map using folium. When your book is built, the code for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found