-
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 all 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,110 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
Simple HTTP server so we are able to show Maps in browser. | ||
""" | ||
|
||
import sys | ||
if sys.version_info[0] >= 3: | ||
from http.server import BaseHTTPRequestHandler, HTTPServer | ||
else: | ||
from SimpleHTTPServer import SimpleHTTPRequestHandler as BaseHTTPRequestHandler | ||
from SocketServer import TCPServer as HTTPServer | ||
|
||
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. 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 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): | ||
|
||
def do_GET(self): | ||
self.send_response(200) | ||
self.send_header('Content-type', 'text/html') | ||
self.end_headers() | ||
if sys.version_info[0] >= 3: | ||
self.wfile.write(bytes(html_data, encoding='utf')) | ||
else: | ||
self.wfile.write(bytes(html_data)) | ||
|
||
return | ||
|
||
# run a temporary server | ||
HTTPServer.allow_reuse_address = True | ||
httpd = HTTPServer((self.host, self.port), HTTPServerRequestHandler) | ||
|
||
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.
E402 module level import not at top of file
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.
that seems to be the case for most of the other files in the repo, so I chose consistency over purity.