-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathrealtime-iss.py
78 lines (68 loc) · 2.04 KB
/
realtime-iss.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
import streamlit as st
from streamlit_folium import st_folium
import folium
from folium.plugins import Realtime
st.set_page_config(page_title="iss", layout="wide")
m = folium.Map()
source = folium.JsCode(
"""
function(responseHandler, errorHandler) {
var url = 'https://api.wheretheiss.at/v1/satellites/25544';
fetch(url)
.then((response) => {
return response.json().then((data) => {
var { id, timestamp, longitude, latitude } = data;
return {
'type': 'FeatureCollection',
'features': [{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [longitude, latitude]
},
'properties': {
'id': id,
'timestamp': timestamp
}
}]
};
})
})
.then(responseHandler)
.catch(errorHandler);
}
"""
)
on_each_feature = folium.JsCode(
"""
(feature, layer) => {
layer.on("click", (event) => {
Streamlit.setComponentValue({
id: feature.properties.id,
// Be careful, on_each_feature binds only once.
// You need to extract the current location from
// the event.
location: event.sourceTarget.feature.geometry
});
});
}
"""
)
realtime = Realtime(source, on_each_feature=on_each_feature, interval=1000).add_to(m)
realtime.on(
update=folium.JsCode(
"""
(e) => {
console.log('update ', e.target._map);
var map = e.target._map;
var realtime = e.target;
map.fitBounds(realtime.getBounds(), {maxZoom: 8});
}
"""
)
)
left, right = st.columns(2)
with left:
data = st_folium(m, width=1000, returned_objects=[], debug=True)
with right:
st.write(data)