Skip to content

Commit c6c1d20

Browse files
committed
add distance calculator
1 parent f72980e commit c6c1d20

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

calculate_distance.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import streamlit as st
2+
from geopy import Nominatim, distance
3+
4+
5+
def get_coordinates(place_name):
6+
geolocator = Nominatim(user_agent="geo_locator")
7+
location = geolocator.geocode(place_name)
8+
if location:
9+
return location.latitude, location.longitude
10+
else:
11+
return None, None
12+
13+
st.set_page_config(page_title="Calculate distance")
14+
15+
st.write("Please note that the distance calculator is primarily for calculating the distance"
16+
" for flight expenses")
17+
18+
with st.form("distance_calculator"):
19+
start_city = st.text_input("Starting city")
20+
end_city = st.text_input("Destination city")
21+
22+
return_flight = st.checkbox("Return flight?")
23+
24+
submitted = st.form_submit_button("Calculate")
25+
st.caption("Please note that distance will be calculated to 2 decimal places.")
26+
27+
if submitted:
28+
distance_miles = distance.distance(get_coordinates(start_city), get_coordinates(end_city)).miles
29+
st.write("Distance between ", start_city, " and ", end_city, " is: ", round(distance_miles, 2), " miles.")
30+
31+
if return_flight:
32+
st.write("With a return flight, the total distance is ", round((distance_miles * 2), 2), " miles.")

pages/app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
help_page = st.Page("get_started.py", title="Get started")
88
data_page = st.Page("calculate_data.py", title="Calculate data")
9+
distance_page = st.Page("calculate_distance.py", title="Calculate distance")
910

1011

11-
pg = st.navigation([help_page, data_page])
12+
pg = st.navigation([help_page, data_page, distance_page])
1213
pg.run()

0 commit comments

Comments
 (0)