Update staged app.py with export button for filtered CSV download
Browse files- _space/app.py +52 -15
_space/app.py
CHANGED
|
@@ -235,6 +235,7 @@ map.on('click', function(e) {
|
|
| 235 |
CSS = """
|
| 236 |
#results-table { max-height: 620px; }
|
| 237 |
.modal-hint { font-size: 12px; color: #6b7280; }
|
|
|
|
| 238 |
"""
|
| 239 |
|
| 240 |
|
|
@@ -306,9 +307,9 @@ def build_map(pid):
|
|
| 306 |
return iframe, title
|
| 307 |
|
| 308 |
|
| 309 |
-
def
|
| 310 |
-
|
| 311 |
-
|
| 312 |
df = DATA
|
| 313 |
if garden_lo is None:
|
| 314 |
garden_lo = 0.0
|
|
@@ -336,7 +337,6 @@ def apply_filters(garden_lo, garden_hi, parcel_lo, parcel_hi, cov_lo, cov_hi, nb
|
|
| 336 |
m = m & df['sectie'].isin(secties)
|
| 337 |
if doelen:
|
| 338 |
m = m & df['gebruiksdoel'].isin(doelen)
|
| 339 |
-
# Perceelnummer uit een tekstveld halen (lege tekst = geen filter)
|
| 340 |
perc_lo = _parse_int(perc_lo)
|
| 341 |
perc_hi = _parse_int(perc_hi)
|
| 342 |
if perc_lo is not None or perc_hi is not None:
|
|
@@ -362,9 +362,40 @@ def apply_filters(garden_lo, garden_hi, parcel_lo, parcel_hi, cov_lo, cov_hi, nb
|
|
| 362 |
| df['straat_en_huisnummer'].astype(str).str.contains(q, case=False, na=False)
|
| 363 |
| df['postcode'].astype(str).str.contains(q, case=False, na=False)
|
| 364 |
)
|
| 365 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
if res.empty:
|
| 367 |
-
|
|
|
|
|
|
|
| 368 |
res = res.sort_values('calculated_garden_area', ascending=False)
|
| 369 |
shown = res[TABLE_COLS].head(int(limit)).copy()
|
| 370 |
shown['Bekijken'] = [VIEW_CELL.replace('{pid}', str(v)) for v in shown[ID_COL]]
|
|
@@ -372,16 +403,16 @@ def apply_filters(garden_lo, garden_hi, parcel_lo, parcel_hi, cov_lo, cov_hi, nb
|
|
| 372 |
export = res[TABLE_COLS].rename(columns=RENAME)
|
| 373 |
for frame in (shown, export):
|
| 374 |
for c in NUM_ROUND:
|
| 375 |
-
|
| 376 |
-
|
| 377 |
export.to_csv(csv_path, index=False)
|
| 378 |
# NaN (percelen zonder gebouwen hebben geen bouwjaar) is niet JSON-compatibel:
|
| 379 |
# naar None converteren zodat de Dataframe-payload serialiseert.
|
| 380 |
shown = shown.astype(object).where(pd.notna(shown), None)
|
| 381 |
-
status = ('**' + format(len(res), ',d') + '** percelen komen overeen (van ' + format(len(
|
| 382 |
+ ' in de momentopname). Tuin ' + _fmt(garden_lo) + '\u2013' + _fmt(garden_hi)
|
| 383 |
+ ' m\u00b2, gesorteerd op tuinoppervlak; top ' + str(len(shown)) + ' weergegeven.')
|
| 384 |
-
return status, shown, csv_path
|
| 385 |
|
| 386 |
|
| 387 |
def build_map_page(pid):
|
|
@@ -444,21 +475,27 @@ with ui.Blocks(title='Maastricht Tuinzoeker') as demo:
|
|
| 444 |
label='Woonfunctie')
|
| 445 |
idq = ui.Textbox(label='Zoeken (Adres, Postcode, Kadaster-ID)', placeholder='bijv. Boschstraat, 6211, 3430000927')
|
| 446 |
limit = ui.Slider(minimum=5, maximum=500, value=25, step=5, label='Weergegeven rijen')
|
| 447 |
-
|
|
|
|
|
|
|
| 448 |
ui.Markdown('<div class="modal-hint">Klik op <b>Bekijken</b> in een rij om de '
|
| 449 |
'perceelkaart in een nieuw tabblad te openen.</div>')
|
| 450 |
|
| 451 |
with ui.Column(scale=3):
|
| 452 |
-
|
|
|
|
|
|
|
| 453 |
table = ui.Dataframe(datatype=DATATYPES, interactive=True, wrap=False,
|
| 454 |
elem_id='results-table', label='Overeenkomende percelen')
|
| 455 |
-
download = ui.File(label='
|
| 456 |
|
| 457 |
inputs = [garden_lo, garden_hi, parcel_lo, parcel_hi, cov_lo, cov_hi, nb_lo, nb_hi,
|
| 458 |
gemeenten, secties, perc_lo, perc_hi, year_lo, year_hi, excl_year,
|
| 459 |
doelen, residential, idq, limit]
|
| 460 |
-
btn.click(fn=apply_filters, inputs=inputs, outputs=[status, table, download])
|
| 461 |
-
demo.load(fn=apply_filters, inputs=inputs, outputs=[status, table, download])
|
|
|
|
|
|
|
| 462 |
|
| 463 |
@app.get('/map/{pid}')
|
| 464 |
def map_page(pid: str):
|
|
|
|
| 235 |
CSS = """
|
| 236 |
#results-table { max-height: 620px; }
|
| 237 |
.modal-hint { font-size: 12px; color: #6b7280; }
|
| 238 |
+
#table-top-bar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
|
| 239 |
"""
|
| 240 |
|
| 241 |
|
|
|
|
| 307 |
return iframe, title
|
| 308 |
|
| 309 |
|
| 310 |
+
def get_filtered_df(garden_lo, garden_hi, parcel_lo, parcel_hi, cov_lo, cov_hi, nb_lo, nb_hi,
|
| 311 |
+
gemeenten, secties, perc_lo, perc_hi, year_lo, year_hi, excl_year,
|
| 312 |
+
doelen, residential, idq):
|
| 313 |
df = DATA
|
| 314 |
if garden_lo is None:
|
| 315 |
garden_lo = 0.0
|
|
|
|
| 337 |
m = m & df['sectie'].isin(secties)
|
| 338 |
if doelen:
|
| 339 |
m = m & df['gebruiksdoel'].isin(doelen)
|
|
|
|
| 340 |
perc_lo = _parse_int(perc_lo)
|
| 341 |
perc_hi = _parse_int(perc_hi)
|
| 342 |
if perc_lo is not None or perc_hi is not None:
|
|
|
|
| 362 |
| df['straat_en_huisnummer'].astype(str).str.contains(q, case=False, na=False)
|
| 363 |
| df['postcode'].astype(str).str.contains(q, case=False, na=False)
|
| 364 |
)
|
| 365 |
+
return df[m]
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
def export_filtered_csv(garden_lo, garden_hi, parcel_lo, parcel_hi, cov_lo, cov_hi, nb_lo, nb_hi,
|
| 369 |
+
gemeenten, secties, perc_lo, perc_hi, year_lo, year_hi, excl_year,
|
| 370 |
+
doelen, residential, idq, limit):
|
| 371 |
+
res = get_filtered_df(garden_lo, garden_hi, parcel_lo, parcel_hi, cov_lo, cov_hi, nb_lo, nb_hi,
|
| 372 |
+
gemeenten, secties, perc_lo, perc_hi, year_lo, year_hi, excl_year,
|
| 373 |
+
doelen, residential, idq)
|
| 374 |
+
csv_path = os.path.join(tempfile.gettempdir(), 'maastricht_percelen_gefilterd.csv')
|
| 375 |
+
if res.empty:
|
| 376 |
+
empty_df = pd.DataFrame(columns=[RENAME.get(c, c) for c in TABLE_COLS])
|
| 377 |
+
empty_df.to_csv(csv_path, index=False)
|
| 378 |
+
return csv_path
|
| 379 |
+
res = res.sort_values('calculated_garden_area', ascending=False)
|
| 380 |
+
export = res[TABLE_COLS].rename(columns=RENAME)
|
| 381 |
+
for c in NUM_ROUND:
|
| 382 |
+
if c in export.columns:
|
| 383 |
+
export[c] = export[c].round(1)
|
| 384 |
+
export.to_csv(csv_path, index=False)
|
| 385 |
+
return csv_path
|
| 386 |
+
|
| 387 |
+
|
| 388 |
+
def apply_filters(garden_lo, garden_hi, parcel_lo, parcel_hi, cov_lo, cov_hi, nb_lo, nb_hi,
|
| 389 |
+
gemeenten, secties, perc_lo, perc_hi, year_lo, year_hi, excl_year,
|
| 390 |
+
doelen, residential, idq, limit):
|
| 391 |
+
res = get_filtered_df(garden_lo, garden_hi, parcel_lo, parcel_hi, cov_lo, cov_hi, nb_lo, nb_hi,
|
| 392 |
+
gemeenten, secties, perc_lo, perc_hi, year_lo, year_hi, excl_year,
|
| 393 |
+
doelen, residential, idq)
|
| 394 |
+
csv_path = os.path.join(tempfile.gettempdir(), 'maastricht_percelen_gefilterd.csv')
|
| 395 |
if res.empty:
|
| 396 |
+
empty_df = pd.DataFrame(columns=[RENAME.get(c, c) for c in TABLE_COLS])
|
| 397 |
+
empty_df.to_csv(csv_path, index=False)
|
| 398 |
+
return 'Geen percelen gevonden met deze filters. Probeer de bereiken te verruimen.', None, csv_path, csv_path, csv_path
|
| 399 |
res = res.sort_values('calculated_garden_area', ascending=False)
|
| 400 |
shown = res[TABLE_COLS].head(int(limit)).copy()
|
| 401 |
shown['Bekijken'] = [VIEW_CELL.replace('{pid}', str(v)) for v in shown[ID_COL]]
|
|
|
|
| 403 |
export = res[TABLE_COLS].rename(columns=RENAME)
|
| 404 |
for frame in (shown, export):
|
| 405 |
for c in NUM_ROUND:
|
| 406 |
+
if c in frame.columns:
|
| 407 |
+
frame[c] = frame[c].round(1)
|
| 408 |
export.to_csv(csv_path, index=False)
|
| 409 |
# NaN (percelen zonder gebouwen hebben geen bouwjaar) is niet JSON-compatibel:
|
| 410 |
# naar None converteren zodat de Dataframe-payload serialiseert.
|
| 411 |
shown = shown.astype(object).where(pd.notna(shown), None)
|
| 412 |
+
status = ('**' + format(len(res), ',d') + '** percelen komen overeen (van ' + format(len(DATA), ',d')
|
| 413 |
+ ' in de momentopname). Tuin ' + _fmt(garden_lo) + '\u2013' + _fmt(garden_hi)
|
| 414 |
+ ' m\u00b2, gesorteerd op tuinoppervlak; top ' + str(len(shown)) + ' weergegeven.')
|
| 415 |
+
return status, shown, csv_path, csv_path, csv_path
|
| 416 |
|
| 417 |
|
| 418 |
def build_map_page(pid):
|
|
|
|
| 475 |
label='Woonfunctie')
|
| 476 |
idq = ui.Textbox(label='Zoeken (Adres, Postcode, Kadaster-ID)', placeholder='bijv. Boschstraat, 6211, 3430000927')
|
| 477 |
limit = ui.Slider(minimum=5, maximum=500, value=25, step=5, label='Weergegeven rijen')
|
| 478 |
+
with ui.Row():
|
| 479 |
+
btn = ui.Button('Filters toepassen', variant='primary', scale=2)
|
| 480 |
+
export_btn = ui.DownloadButton('📥 Exporteer (.csv)', variant='secondary', scale=2)
|
| 481 |
ui.Markdown('<div class="modal-hint">Klik op <b>Bekijken</b> in een rij om de '
|
| 482 |
'perceelkaart in een nieuw tabblad te openen.</div>')
|
| 483 |
|
| 484 |
with ui.Column(scale=3):
|
| 485 |
+
with ui.Row(elem_id='table-top-bar'):
|
| 486 |
+
status = ui.Markdown()
|
| 487 |
+
table_export_btn = ui.DownloadButton('📥 Exporteer naar .csv', variant='secondary', size='sm', scale=0)
|
| 488 |
table = ui.Dataframe(datatype=DATATYPES, interactive=True, wrap=False,
|
| 489 |
elem_id='results-table', label='Overeenkomende percelen')
|
| 490 |
+
download = ui.File(label='📥 Gefilterde CSV downloaden')
|
| 491 |
|
| 492 |
inputs = [garden_lo, garden_hi, parcel_lo, parcel_hi, cov_lo, cov_hi, nb_lo, nb_hi,
|
| 493 |
gemeenten, secties, perc_lo, perc_hi, year_lo, year_hi, excl_year,
|
| 494 |
doelen, residential, idq, limit]
|
| 495 |
+
btn.click(fn=apply_filters, inputs=inputs, outputs=[status, table, export_btn, table_export_btn, download])
|
| 496 |
+
demo.load(fn=apply_filters, inputs=inputs, outputs=[status, table, export_btn, table_export_btn, download])
|
| 497 |
+
export_btn.click(fn=export_filtered_csv, inputs=inputs, outputs=export_btn)
|
| 498 |
+
table_export_btn.click(fn=export_filtered_csv, inputs=inputs, outputs=table_export_btn)
|
| 499 |
|
| 500 |
@app.get('/map/{pid}')
|
| 501 |
def map_page(pid: str):
|