-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdum.py
81 lines (72 loc) · 1.81 KB
/
dum.py
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
from plotly.offline import plot
import pandas as pd, re
import plotly.graph_objs as go
mapbox_access_token = '1234'
# Build scattermapbox trace and store URL's in the customdata
# property. The values of this list will be easy to get to in the
# JavaScript callback below
data = [
go.Scattermapbox(
lat=['45.5017', '45.7', '45.3'],
lon=['-73.5673', '-73.7', '-73.3'],
mode='markers',
marker=dict(
size=14
),
name='mapbox 1',
text=['Montreal'],
customdata=['https://google.com', 'https://bing.com', 'https://duckduckgo.com']
)
]
# Build layout
layout = go.Layout(
hovermode='closest',
mapbox=dict(
accesstoken=mapbox_access_token,
bearing=0,
center=dict(
lat=45,
lon=-73
),
pitch=0,
zoom=6,
)
)
# Build Figure
fig = go.Figure(
data=data,
layout=layout,
)
# Get HTML representation of plotly.js and this figure
plot_div = plot(fig, output_type='div', include_plotlyjs=True)
# Get id of html div element that looks like
# <div id="301d22ab-bfba-4621-8f5d-dc4fd855bb33" ... >
res = re.search('<div id="([^"]*)"', plot_div)
div_id = res.groups()[0]
# Build JavaScript callback for handling clicks
# and opening the URL in the trace's customdata
js_callback = """
<script>
var plot_element = document.getElementById("{div_id}");
plot_element.on('plotly_click', function(data){{
console.log(data);
var point = data.points[0];
if (point) {{
console.log(point.customdata);
window.open(point.customdata);
}}
}})
</script>
""".format(div_id=div_id)
# Build HTML string
html_str = """
<html>
<body>
{plot_div}
{js_callback}
</body>
</html>
""".format(plot_div=plot_div, js_callback=js_callback)
# Write out HTML file
with open('hyperlink_fig.html', 'w') as f:
f.write(html_str)