-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
152 lines (107 loc) · 4.74 KB
/
main.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
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
151
152
import streamlit as st
from google.cloud import firestore
import googlemaps
import folium
import numpy as np
from streamlit_folium import st_folium, folium_static
import json
from google.oauth2 import service_account
# Authenticate to Firestore with the JSON account key.
key_dict = json.loads(st.secrets['textkey'])
creds = service_account.Credentials.from_service_account_info(key_dict)
db = firestore.Client(credentials=creds)
# Define the API key
api_key = st.secrets["gmap_api_key"]
gmaps = googlemaps.Client(key=api_key)
# Create a reference to the Google post.
user_collection = db.collection("users")
def get_available_users(collection):
res = collection.get() # returns a list
name_list = [doc.to_dict().get("name") for doc in res]
lat_list = [doc.to_dict().get("location").latitude for doc in res]
lng_list = [doc.to_dict().get("location").longitude for doc in res]
return name_list, lat_list, lng_list
def get_geocode_from_location(location):
geocode = gmaps.geocode(location)
# Print the latitude and longitude of the address
location = geocode[0]["geometry"]["location"]
latitude = location.get("lat")
longitude = location.get("lng")
return latitude, longitude
def delete_collection(coll_ref, batch_size=4):
docs = coll_ref.list_documents(page_size=batch_size)
deleted = 0
for doc in docs:
doc.delete()
deleted = deleted + 1
if deleted >= batch_size:
return delete_collection(coll_ref, batch_size)
def delete_particular_user(coll_ref, user_number):
docs = coll_ref.list_documents()
for i, doc in enumerate(docs):
if user_number == i:
doc.delete()
st.title("Meet in the Middle")
with st.form("input_form", clear_on_submit=True):
name = st.text_input(label="Name", key="name_key")
location = st.text_input(label="Location", key="location_key")
keywords = st.text_input(label="Keywords to search (Restaurants, Colleges etc)", value="badminton")
# Every form must have a submit button.
submitted = st.form_submit_button("Submit")
if submitted:
if name == "":
st.error("Enter a valid name")
st.stop()
if location == "":
st.error("Enter your location")
st.stop()
if keywords == "":
st.error("Please enter what you want to search for")
st.stop()
lat, lng = get_geocode_from_location(location)
geolocation = firestore.GeoPoint(latitude=lat, longitude=lng)
user_collection.document().set({
"name": name,
"location_string": location,
"location": geolocation,
})
userlist, lat_list, lng_list = get_available_users(collection=user_collection)
# st.write(userlist)
# st.write(lat_list)
# st.write(lng_list)
if userlist:
with st.expander(label="Click to see the list of users"):
st.write(userlist)
usernum_inp, but_col1, but_col2 = st.columns([1, 1, 1])
user_id = usernum_inp.number_input(label="User Number", min_value=0, max_value=len(userlist)-1)
but_col1.button(label="Delete", on_click=delete_particular_user, args=[user_collection, user_id])
delete_all_users_button = but_col2.button(label='Delete all Users', on_click=delete_collection, args=[user_collection,])
midpoint_lat = np.mean(lat_list)
midpoint_lng = np.mean(lng_list)
nearest_courts = gmaps.places_nearby(
location=(midpoint_lat, midpoint_lng),
keyword=keywords,
rank_by="distance",
)
# st.write(nearest_courts)
target_location = nearest_courts.get("results")[0].get("geometry").get("location")
target_lat = target_location.get("lat")
target_lng = target_location.get("lng")
# create map object
m = folium.Map(location=[midpoint_lat, midpoint_lng], zoom_start=12)
for lat, lng, username in zip(lat_list, lng_list, userlist):
# add marker for coordinates
folium.Marker([lat, lng], popup=username).add_to(m)
# folium.Marker([midpoint_lat, midpoint_lng]).add_to(m)
folium.Marker([target_lat, target_lng], popup=keywords, icon=folium.Icon(color="green"),).add_to(m)
st_data = folium_static(m, width=725, )
st.header("Top 3 places")
for i in range(3):
top_result = nearest_courts.get("results")[i]
place_name = top_result.get("name")
place_id = top_result.get("place_id")
place_link = f"https://www.google.com/maps/search/?api=1&query=address&query_place_id={place_id}"
# st.markdown(body=<)
st.subheader(f"[{place_name}]({place_link})")
# st.write(place_link)
st.markdown("""---""")