Skip to content

Commit 83329fd

Browse files
Merge pull request #4 from Czaki/setup_cache
Add streamlit cache
2 parents 46230f4 + f153200 commit 83329fd

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

hackyeah2024/server.py

+26-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import folium
22
import streamlit as st
3-
from osmnx import distance, geocoder, graph_from_bbox, routing, features_from_bbox
3+
from osmnx import distance, features_from_bbox, geocoder, graph_from_bbox, routing
44
from streamlit_folium import st_folium
55

66
bounding_box = {'min_lat': 49.174522, 'max_lat': 50.526524, 'min_lon': 19.076385, 'max_lon': 21.432953}
77

88

9-
def get_route_points(start, end):
10-
G = graph_from_bbox(
9+
@st.cache_resource
10+
def get_graph():
11+
return graph_from_bbox(
1112
bbox=(bounding_box['max_lat'], bounding_box['min_lat'], bounding_box['min_lon'], bounding_box['max_lon']),
1213
network_type='bike',
1314
simplify=False,
15+
retain_all=True,
1416
)
17+
18+
19+
@st.cache_data
20+
def get_route_points(start, end):
21+
G = get_graph()
1522
orig = geocoder.geocode(start)
1623
dest = geocoder.geocode(end)
1724
o, _ = distance.nearest_nodes(G, orig[1], orig[0], return_dist=True)
@@ -28,24 +35,34 @@ def plot_route(map_, route_pts):
2835
return map
2936

3037

38+
@st.cache_data
39+
def get_localities():
40+
print('Fetching localities')
41+
x = tuple(
42+
features_from_bbox(
43+
bbox=(bounding_box['max_lat'], bounding_box['min_lat'], bounding_box['min_lon'], bounding_box['max_lon']),
44+
tags={'place': ['town', 'vilage']},
45+
)['name'].values
46+
)
47+
print('Fetched localities')
48+
return x
49+
50+
3151
def run_server():
3252
st.title('Rowerem przez Małopolskę')
3353

34-
localities = tuple(features_from_bbox(
35-
bbox=(bounding_box['max_lat'], bounding_box['min_lat'], bounding_box['min_lon'], bounding_box['max_lon']),
36-
tags={"place": ["town", "vilage"]})['name'].values
37-
)
54+
localities = get_localities()
3855

3956
cont1 = st.container()
4057

4158
with cont1:
4259
col1, col2 = st.columns(2)
4360

4461
with col1:
45-
origin = st.selectbox("Skąd", localities)
62+
origin = st.selectbox('Skąd', localities)
4663

4764
with col2:
48-
destination = st.selectbox("Dokąd", localities)
65+
destination = st.selectbox('Dokąd', localities)
4966

5067
cont2 = st.container()
5168

0 commit comments

Comments
 (0)