This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp_all_data.py
More file actions
142 lines (112 loc) · 4.82 KB
/
app_all_data.py
File metadata and controls
142 lines (112 loc) · 4.82 KB
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
from bokeh.models.widgets.tables import DataTable
import geopandas as gpd
from geopandas import GeoDataFrame
import pandas as pd
import streamlit as st
from shapely.geometry import Polygon
from bokeh.plotting import figure
from bokeh.tile_providers import CARTODBPOSITRON, get_provider
from bokeh.models import ColumnDataSource, CustomJS, tools
from bokeh.models import TableColumn, WidgetBox
from streamlit_bokeh_events import streamlit_bokeh_events
st.set_page_config(layout="wide")
# LAYING OUT THE TOP SECTION OF THE APP
row1_1, row1_2 = st.beta_columns((2,3))
with row1_1:
st.title("Dublin Energy Masterplan Modelling Outputs")
with row1_2:
st.write(
"""
##
An interactive web-map to view and download the outputs of the SEAI-funded Dublin Region Energy
Masterplan developed by Codema. Additional information available on the modelling methodology
upon request. Small Area centroids as of the 2016 Census, are marked on the map here for all of Dublin,
of which they can be selected using the lasso tool on the right of the map to circle desired data and
downloaded in csv format.
""")
col1, col2 = st.beta_columns([1, 5])
with col1:
st.image('data/codema_logo.png', width=200)
with col2:
st.image('data/seai_logo.jpg', width=200)
# READ IN TOP BUILDINGS DEMANDS
map_data = gpd.read_file("data/drem/single_polygon_sa_all_info.geojson")
map_data = map_data.set_crs(epsg="4326")
map_data = map_data.to_crs(epsg="3857")
map_data = gpd.GeoDataFrame(map_data)
df = map_data[["GEOGID_left", "T1_1AGETT", "postcodes", "area", "pop-density-ppsqkm", "sa_resi_heat_emissions_TCO2", "sa_comm_elec_emissions_TCO2", "sa_comm_heat_emissions_TCO2", "sa_data_centre_elec_emissions_TCO2", "total_sa_emissions_TCO2", "total_sa_resi_emissions_TCO2", "total_sa_comm_emissions_TCO2", "geometry"]]
df["centroid"] = df.centroid
df["x"] = df["centroid"].x
df["y"] = df["centroid"].y
df = df[["GEOGID_left", "T1_1AGETT", "postcodes", "area", "pop-density-ppsqkm", "sa_resi_heat_emissions_TCO2", "sa_comm_elec_emissions_TCO2", "sa_comm_heat_emissions_TCO2", "sa_data_centre_elec_emissions_TCO2", "total_sa_emissions_TCO2", "total_sa_resi_emissions_TCO2", "total_sa_comm_emissions_TCO2", "x", "y"]]
df = df.rename(columns={"GEOGID_left": "small_area_id", "T1_1AGETT": "population", "area": "area_m2"})
df = df.drop_duplicates('small_area_id')
col1, col2 = st.beta_columns(2)
cds = ColumnDataSource(df)
columns = list(map(lambda colname: TableColumn(field=colname, title=colname), df.columns))
cds.selected.js_on_change(
"indices",
CustomJS(
args=dict(source=cds),
code="""
document.dispatchEvent(
new CustomEvent("INDEX_SELECT", {detail: {data: source.selected.indices}})
)
"""
)
)
table = DataTable(source=cds, columns=columns)
with col1:
result = streamlit_bokeh_events(
bokeh_plot=table,
events="INDEX_SELECT",
key="foo",
refresh_on_update=False,
debounce_time=0,
override_height=500
)
if result:
if result.get("INDEX_SELECT"):
st.write(df.iloc[result.get("INDEX_SELECT")["data"]])
plot = figure(tools="pan, zoom_in, zoom_out, box_zoom, wheel_zoom, lasso_select", width=250, height=250)
cds_lasso = ColumnDataSource(df)
cds_lasso.selected.js_on_change(
"indices",
CustomJS(
args=dict(source=cds_lasso),
code="""
document.dispatchEvent(
new CustomEvent("LASSO_SELECT", {detail: {data: source.selected.indices}})
)
"""
)
)
import base64
# Assuming UTF-8 encoding, change to something else if you need to
base64.b64encode("password".encode("utf-8"))
def get_table_download_link(df):
"""Generates a link allowing the data in a given panda dataframe to be downloaded
in: dataframe
out: href string
"""
csv = df.to_csv(index=False)
b64 = base64.b64encode(csv.encode()).decode() # some strings <-> bytes conversions necessary here
href = f'<a href="data:file/csv;base64,{b64}" download="ets-demand-data.csv">Download selected data in csv file</a>'
return href
plot = figure(x_axis_type="mercator", y_axis_type="mercator", tools="pan, box_zoom, wheel_zoom, lasso_select")
plot.xaxis.axis_label = 'longitude'
plot.yaxis.axis_label = 'latitude'
tile_provider = get_provider(CARTODBPOSITRON)
plot.add_tile(tile_provider)
plot.circle("x", "y", fill_alpha=0.5, size=5, source=cds_lasso)
with col2:
result_lasso = streamlit_bokeh_events(
bokeh_plot=plot,
events="LASSO_SELECT",
key="bar",
refresh_on_update=False,
debounce_time=0)
if result_lasso:
if result_lasso.get("LASSO_SELECT"):
st.write(df.iloc[result_lasso.get("LASSO_SELECT")["data"]])
st.markdown(get_table_download_link(df.iloc[result_lasso.get("LASSO_SELECT")["data"]]), unsafe_allow_html=True)