-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
show_in_browser()
method for folium.folium.Map
#953
Changes from 4 commits
f7fc8af
a05062d
7755220
dc35d5c
81e087c
395e0e1
6e87103
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
""" | ||
Uses plain python and opens the folium map in a browser window. No IPython, Jupyter or else is required. | ||
|
||
""" | ||
|
||
import numpy as np | ||
import folium | ||
from folium.plugins import HeatMap | ||
|
||
# create a dummy 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) | ||
|
||
# open in in browser | ||
folium_map.show_in_browser() | ||
|
||
Demetrio92 marked this conversation as resolved.
Show resolved
Hide resolved
Demetrio92 marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
Simple HTTP server so we are able to show Maps in browser. Only works with python 3!. | ||
|
||
""" | ||
|
||
import sys | ||
if sys.version_info[0] < 3: | ||
raise NotImplementedError('Only works with python 3!') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a more verbose message like:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually realised i might have misunderstood something. Because there is an http server in python 2, so I'll check it again tomorrow. |
||
|
||
import webbrowser | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E402 module level import not at top of file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that seems to be the case for most of the other files in the repo, so I chose consistency over purity. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E402 module level import not at top of file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as before this seems to be a convention in the repo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E402 module level import not at top of file |
||
from http.server import BaseHTTPRequestHandler, HTTPServer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E402 module level import not at top of file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that seems to be the case for most of the other files in the repo, so I chose consistency over purity. |
||
from textwrap import dedent | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E402 module level import not at top of file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that seems to be the case for most of the other files in the repo, so I chose consistency over purity. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please follow the liner cues here. We are cleaning up the code and new code should be compliant from the start. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E402 module level import not at top of file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as before this seems to be a convention in the repo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E402 module level import not at top of file |
||
|
||
|
||
# Based on https://stackoverflow.com/a/38945907/3494126 | ||
class TemporaryHTTPServer: | ||
""" | ||
A simple, temporary http web server on the pure Python 3. | ||
|
||
Parameters | ||
---------- | ||
host: str | ||
Local address on which the page will be hosted, default is '127.0.0.1' | ||
port: int | ||
Corresponding port, default 7000 | ||
""" | ||
def __init__(self, host=None, port=None): | ||
self.host = host or '127.0.0.1' | ||
self.port = port or 7000 | ||
|
||
self.server_address = '{host}:{port}'.format(host=self.host, port=self.port) | ||
self.full_server_address = 'http://' + self.server_address | ||
|
||
def serve(self, html_data): | ||
""" | ||
Serve html content in a suitable for us manner: allow to gracefully exit using ctrl+c and re-serve some other | ||
content on the same host:port | ||
|
||
""" | ||
|
||
# we need a request handler with a method `do_GET` which somehow is not provided in the baseline | ||
class HTTPServerRequestHandler(BaseHTTPRequestHandler): | ||
""" | ||
An handler of requests for the server, hosting HTML-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', 'text/html') | ||
self.end_headers() | ||
|
||
# writing data on a page | ||
self.wfile.write(bytes(html_data, encoding='utf')) | ||
|
||
return | ||
|
||
# create a temporary server | ||
HTTPServer.allow_reuse_address = True | ||
httpd = HTTPServer((self.host, self.port), HTTPServerRequestHandler) | ||
|
||
# run the temporary http server with graceful exit option | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of these comment can be removed. I guess that they were added on SO to explain the answer. We don't need them here. |
||
try: | ||
httpd.serve_forever() | ||
except KeyboardInterrupt: | ||
print('\n Closing the server.') | ||
httpd.server_close() | ||
except Exception: | ||
raise | ||
|
||
def open_html_in_browser(self, html_data=None): | ||
""" | ||
Opens a browser window showing the html content | ||
|
||
Parameters | ||
---------- | ||
html_data: str | ||
Should be a valid html code | ||
|
||
Examples | ||
-------- | ||
|
||
Demetrio92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
html_data = ''' | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title> Test Page </title> | ||
</head> | ||
<body> | ||
<p> Seems to be working. Now give the function `open_html_in_browser` some content! </p> | ||
</body> | ||
</html> | ||
''' | ||
|
||
srvr = TemporaryHTTPServer() | ||
srvr.open_html_in_browser(html_data) | ||
|
||
""" | ||
|
||
# open the URL in a browser (if possible, in a new window) | ||
webbrowser.open(self.full_server_address, new=2) | ||
|
||
# print a user-friendly message | ||
msg = ''' | ||
Your map is available at {link}. | ||
Demetrio92 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
It should have been opened in your browser automatically. | ||
Press ctrl+c to return. | ||
'''.format(link=self.full_server_address) | ||
print(dedent(msg)) | ||
|
||
# run server (this blocks the console!) | ||
self.serve(html_data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for this example. This can be in the method docstring.