Repository navigation
Expand file tree
/
Copy pathmap.py
More file actions
150 lines (113 loc) · 4.18 KB
/
Copy pathmap.py
File metadata and controls
150 lines (113 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import folium
import os
import json
# Create map object
m = folium.Map(location=[42.3601, -71.0589], zoom_start=12)
# Global tooltip
tooltip = 'Click For More Info'
# Create custom marker icon
logoIcon = folium.features.CustomIcon('logo.png', icon_size=(50, 50))
# Vega data
vis = os.path.join('data', 'vis.json')
# Geojson Data
overlay = os.path.join('data', 'overlay.json')
# Create markers
folium.Marker([42.363600, -71.099500],
popup='<strong>Location One</strong>',
tooltip=tooltip).add_to(m),
folium.Marker([42.333600, -71.109500],
popup='<strong>Location Two</strong>',
tooltip=tooltip,
icon=folium.Icon(icon='cloud')).add_to(m),
folium.Marker([42.377120, -71.062400],
popup='<strong>Location Three</strong>',
tooltip=tooltip,
icon=folium.Icon(color='purple')).add_to(m),
folium.Marker([42.374150, -71.122410],
popup='<strong>Location Four</strong>',
tooltip=tooltip,
icon=folium.Icon(color='green', icon='leaf')).add_to(m),
folium.Marker([42.375140, -71.032450],
popup='<strong>Location Five</strong>',
tooltip=tooltip,
icon=logoIcon).add_to(m),
folium.Marker([42.315140, -71.072450],
popup=folium.Popup(max_width=450).add_child(folium.Vega(json.load(open(vis)), width=450, height=250))).add_to(m)
# Circle marker
folium.CircleMarker(
location=[42.466470, -70.942110],
radius=50,
popup='My Birthplace',
color='#428bca',
fill=True,
fill_color='#428bca'
).add_to(m)
# Geojson overlay
folium.GeoJson(overlay, name='cambridge').add_to(m)
# Generate map
m.save('map.html')
# ------------------------------------------------------------------------------------------------
# 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)