Spaces:
Build error
Build error
Fixed bug with database reading
Browse files
app.py
CHANGED
@@ -239,10 +239,10 @@ def create_dataframe(years, project_name):
|
|
239 |
dfs.append(df)
|
240 |
return pd.concat(dfs)
|
241 |
|
242 |
-
def filter_map():
|
243 |
prepared_statement = \
|
244 |
con.execute("SELECT geometry FROM project WHERE name = ? LIMIT 1",
|
245 |
-
[
|
246 |
features = \
|
247 |
json.loads(prepared_statement[0][0].replace("\'", "\""))['features']
|
248 |
geometry = features[0]['geometry']
|
@@ -294,9 +294,17 @@ def calculate_biodiversity_score(start_year, end_year, project_name):
|
|
294 |
USE climatebase;
|
295 |
CREATE TABLE IF NOT EXISTS bioindicator (year BIGINT, project_name VARCHAR(255), value DOUBLE, area DOUBLE, score DOUBLE, CONSTRAINT unique_year_project_name UNIQUE (year, project_name));
|
296 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
297 |
scores = \
|
298 |
-
con.execute("SELECT * FROM bioindicator WHERE (year
|
299 |
-
[start_year, end_year, project_name]).
|
300 |
return scores
|
301 |
|
302 |
def view_all():
|
@@ -304,17 +312,6 @@ def view_all():
|
|
304 |
return con.sql("SELECT * FROM bioindicator").df()
|
305 |
|
306 |
|
307 |
-
def push_to_md():
|
308 |
-
# UPSERT project record
|
309 |
-
con.sql(
|
310 |
-
"""
|
311 |
-
INSERT INTO bioindicator FROM _temptable
|
312 |
-
ON CONFLICT (year, project_name) DO UPDATE SET value = excluded.value;
|
313 |
-
"""
|
314 |
-
)
|
315 |
-
logging.info("upsert records into motherduck")
|
316 |
-
|
317 |
-
|
318 |
with gr.Blocks() as demo:
|
319 |
# Environment setup
|
320 |
authenticate_ee(GEE_SERVICE_ACCOUNT)
|
@@ -335,8 +332,8 @@ with gr.Blocks() as demo:
|
|
335 |
datatype=["number", "str", "number"],
|
336 |
label="Biodiversity scores by year",
|
337 |
)
|
338 |
-
demo.load(filter_map, outputs=[m1])
|
339 |
calc_btn.click(calculate_biodiversity_score, inputs=[start_year, end_year, project_name], outputs=[results_df])
|
340 |
view_btn.click(view_all, outputs=[results_df])
|
341 |
-
save_btn.click(push_to_md)
|
342 |
demo.launch()
|
|
|
239 |
dfs.append(df)
|
240 |
return pd.concat(dfs)
|
241 |
|
242 |
+
def filter_map(project_name):
|
243 |
prepared_statement = \
|
244 |
con.execute("SELECT geometry FROM project WHERE name = ? LIMIT 1",
|
245 |
+
[project_name]).fetchall()
|
246 |
features = \
|
247 |
json.loads(prepared_statement[0][0].replace("\'", "\""))['features']
|
248 |
geometry = features[0]['geometry']
|
|
|
294 |
USE climatebase;
|
295 |
CREATE TABLE IF NOT EXISTS bioindicator (year BIGINT, project_name VARCHAR(255), value DOUBLE, area DOUBLE, score DOUBLE, CONSTRAINT unique_year_project_name UNIQUE (year, project_name));
|
296 |
""")
|
297 |
+
# UPSERT project record
|
298 |
+
con.sql(
|
299 |
+
"""
|
300 |
+
INSERT INTO bioindicator FROM _temptable
|
301 |
+
ON CONFLICT (year, project_name) DO UPDATE SET value = excluded.value;
|
302 |
+
"""
|
303 |
+
)
|
304 |
+
logging.info("upsert records into motherduck")
|
305 |
scores = \
|
306 |
+
con.execute("SELECT * FROM bioindicator WHERE (year >= ? AND year <= ? AND project_name = ?)",
|
307 |
+
[start_year, end_year, project_name]).df()
|
308 |
return scores
|
309 |
|
310 |
def view_all():
|
|
|
312 |
return con.sql("SELECT * FROM bioindicator").df()
|
313 |
|
314 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
315 |
with gr.Blocks() as demo:
|
316 |
# Environment setup
|
317 |
authenticate_ee(GEE_SERVICE_ACCOUNT)
|
|
|
332 |
datatype=["number", "str", "number"],
|
333 |
label="Biodiversity scores by year",
|
334 |
)
|
335 |
+
demo.load(filter_map, inputs=[project_name], outputs=[m1])
|
336 |
calc_btn.click(calculate_biodiversity_score, inputs=[start_year, end_year, project_name], outputs=[results_df])
|
337 |
view_btn.click(view_all, outputs=[results_df])
|
338 |
+
# save_btn.click(push_to_md)
|
339 |
demo.launch()
|