-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_curd.py
279 lines (241 loc) · 9.67 KB
/
streamlit_curd.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import json
import webbrowser
import geopandas as gpd
import humanize
import matplotlib.pyplot as plt
import requests
import streamlit as st
from utils import fetch_last_run_info, get_available_features
# Helper function to send HTTP requests
@st.cache_data
def send_request(method, url, data=None, headers=None):
if method == "GET":
response = requests.get(url, headers=headers)
elif method == "POST":
response = requests.post(url, json=data, headers=headers)
elif method == "PUT":
response = requests.put(url, json=data, headers=headers)
elif method == "PATCH":
response = requests.patch(url, json=data, headers=headers)
elif method == "DELETE":
response = requests.delete(url, headers=headers)
return response.json()
# CRUD operation functions
@st.cache_data
def create_hdx(data, base_url, access_token):
url = f"{base_url}/cron/"
headers = {"Access-Token": access_token}
return send_request("POST", url, data=data, headers=headers)
@st.cache_data
def read_hdx(hdx_id=None, base_url=None, access_token=None):
if hdx_id:
url = f"{base_url}/cron/{hdx_id}"
else:
url = f"{base_url}/cron/"
headers = {"Access-Token": access_token}
return send_request("GET", url, headers=headers)
@st.cache_data
def update_hdx(hdx_id, data, base_url, access_token):
url = f"{base_url}/cron/{hdx_id}"
headers = {"Access-Token": access_token}
return send_request("PATCH", url, data=data, headers=headers)
@st.cache_data
def get_country_geojson(base_url, access_token, cid):
url = f"{base_url}/countries/{cid}/"
headers = {"Access-Token": access_token}
return send_request("GET", url, headers=headers)
# Function to generate auth token
def generate_auth_token(raw_data_api_base_url):
auth_login_url = f"{raw_data_api_base_url}/auth/login"
response = requests.get(auth_login_url)
response.raise_for_status()
login_url = response.json().get("login_url")
if login_url:
st.success(
f"Login link generated [here]({login_url}). Please Log in & Copy Your Token"
)
webbrowser.open_new_tab(login_url)
else:
st.error(
"Failed to generate login link. Please check the raw data API base URL."
)
# Input API URL and authentication
raw_data_api_base_url = st.text_input(
"Enter RAW_DATA_API_BASE_URL (default is https://api-prod.raw-data.hotosm.org/v1):",
"https://api-prod.raw-data.hotosm.org/v1",
)
if "rawdata_api_auth_token" not in st.session_state:
st.session_state.rawdata_api_auth_token = st.text_input(
"Enter RAWDATA_API_AUTH_TOKEN:", type="password"
)
else:
st.session_state.rawdata_api_auth_token = st.text_input(
"Enter RAWDATA_API_AUTH_TOKEN:",
type="password",
value=st.session_state.rawdata_api_auth_token,
)
rawdata_auth_link = st.button("Generate Raw Data API Auth Token")
if rawdata_auth_link:
generate_auth_token(raw_data_api_base_url)
# Get available HDX layers
with st.spinner("Loading available HDX layers..."):
hdx_layers = get_available_features(raw_data_api_base_url=raw_data_api_base_url)
hdx_layer_titles = [
f"{layer['properties']['id']} - {layer['properties']['dataset']['dataset_title']}"
for layer in hdx_layers
]
selected_layer_title = st.sidebar.selectbox(
"Select HDX Layer", hdx_layer_titles, key="select_layer"
)
if selected_layer_title:
selected_layer_id = int(selected_layer_title.split(" - ")[0])
# Read selected layer details
with st.spinner("Loading layer details..."):
selected_layer = read_hdx(
selected_layer_id,
base_url=raw_data_api_base_url,
access_token=st.session_state.rawdata_api_auth_token,
)
# Display layer details
st.write(f"Layer ID: {selected_layer['properties']['id']}")
st.write(f"Layer Title: {selected_layer['properties']['dataset']['dataset_title']}")
folder_path = (
f"{selected_layer['properties']['dataset'].get('dataset_folder')}/{selected_layer['properties'].get('iso3')}/"
if selected_layer["properties"].get("iso3")
else f"{selected_layer['properties']['dataset'].get('dataset_folder')}/{selected_layer['properties']['dataset'].get('dataset_prefix')}/"
)
last_run_info = fetch_last_run_info(
raw_data_api_base_url, f"{folder_path}meta.json"
)
st.write(
"Last Run Date: ",
(
humanize.naturaldate(last_run_info.get("started_at", "N/A"))
if last_run_info
else "N/A"
),
)
st.write(
"Elapsed time : ",
last_run_info.get("elapsed_time", "N/A") if last_run_info else "N/A",
)
# Visualize geometry on a map
if selected_layer["geometry"]["type"] is None and selected_layer["properties"].get(
"cid"
):
with st.spinner("Loading country geometry..."):
country_geojson = get_country_geojson(
raw_data_api_base_url,
st.session_state.rawdata_api_auth_token,
selected_layer["properties"]["cid"],
)
gdf = gpd.GeoDataFrame.from_features(
{"type": "FeatureCollection", "features": [country_geojson]}, crs="4326"
)
fig, ax = plt.subplots(figsize=(8, 8))
gdf.plot(ax=ax, edgecolor="red", facecolor="none")
st.pyplot(fig)
elif selected_layer["geometry"]["type"] is not None:
geometry = json.loads(selected_layer["geometry"])
gdf = gpd.GeoDataFrame.from_features(geometry, crs="4326")
fig, ax = plt.subplots(figsize=(8, 8))
gdf.plot(ax=ax, edgecolor="red", facecolor="none")
st.pyplot(fig)
# Update layer fields interactively
st.write("Update Layer Fields:")
updated_fields = {}
# Update iso3, cid, and queue
for key in ["iso3", "cid", "queue"]:
value = selected_layer["properties"].get(key)
updated_value = st.text_input(f"{key}", value=str(value))
if updated_value != str(value):
updated_fields[key] = updated_value
# Update hdx_upload and meta
for key in ["hdx_upload", "meta"]:
value = selected_layer["properties"].get(key)
updated_value = st.checkbox(f"{key}", value=value)
if updated_value != value:
updated_fields[key] = updated_value
# Update dataset
current_dataset = selected_layer["properties"].get("dataset", {})
dataset_title = st.text_input(
"Dataset Title", value=current_dataset.get("dataset_title", "")
)
dataset_folder = st.text_input(
"Dataset Folder", value=current_dataset.get("dataset_folder", "")
)
dataset_prefix = st.text_input(
"Dataset Prefix", value=current_dataset.get("dataset_prefix", "")
)
update_frequency = st.selectbox(
"Update Frequency",
["daily", "weekly", "monthly", "disabled"],
index=["daily", "weekly", "monthly", "disabled"].index(
current_dataset.get("update_frequency", "monthly")
),
)
dataset_locations = st.text_input(
"Dataset Locations", current_dataset.get("dataset_locations", [])
)
updated_dataset = {
"dataset_title": dataset_title,
"dataset_folder": dataset_folder,
"dataset_prefix": dataset_prefix,
"update_frequency": update_frequency,
"dataset_locations": dataset_locations,
}
if updated_dataset != current_dataset:
updated_fields["dataset"] = updated_dataset
# Update categories
current_categories = selected_layer["properties"].get("categories", {})
updated_categories = st.text_area(
"Categories", value=json.dumps(current_categories, indent=4)
)
if updated_categories != json.dumps(current_categories, indent=4):
updated_fields["categories"] = json.loads(updated_categories)
current_geometry = selected_layer["properties"].get("geometry")
updated_geometry = st.text_area(
"Geometry", value=json.dumps(current_geometry, indent=4)
)
if updated_geometry != json.dumps(current_geometry, indent=4):
updated_fields["geometry"] = json.loads(updated_geometry)
if st.button("Save Updates"):
with st.spinner("Updating layer..."):
response = update_hdx(
selected_layer_id,
updated_fields,
base_url=raw_data_api_base_url,
access_token=st.session_state.rawdata_api_auth_token,
)
st.write(response)
# Create a new HDX layer
st.sidebar.title("Create New HDX Layer")
new_layer_data = {}
new_layer_data["iso3"] = st.sidebar.text_input("ISO3")
new_layer_data["hdx_upload"] = st.sidebar.checkbox("HDX Upload", value=True)
dataset_title = st.sidebar.text_input("Dataset Title")
dataset_folder = st.sidebar.text_input("Dataset Folder")
dataset_prefix = st.sidebar.text_input("Dataset Prefix")
update_frequency = st.sidebar.selectbox(
"Update Frequency", ["daily", "weekly", "monthly"], index=1
)
dataset_locations = st.sidebar.text_input("Dataset Locations", [])
new_layer_data["dataset"] = {
"dataset_title": dataset_title,
"dataset_folder": dataset_folder,
"dataset_prefix": dataset_prefix,
"update_frequency": update_frequency,
"dataset_locations": dataset_locations,
}
new_layer_data["queue"] = st.sidebar.text_input("Queue", value="raw_ondemand")
new_layer_data["meta"] = st.sidebar.checkbox("Meta", value=False)
new_layer_data["categories"] = st.sidebar.text_area("Categories", value="{}")
new_layer_data["geometry"] = st.sidebar.text_area("Geometry")
if st.sidebar.button("Create Layer"):
with st.spinner("Creating new layer..."):
response = create_hdx(
new_layer_data,
base_url=raw_data_api_base_url,
access_token=st.session_state.rawdata_api_auth_token,
)
st.sidebar.write(response)