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_top_200.py
More file actions
134 lines (108 loc) · 4.17 KB
/
app_top_200.py
File metadata and controls
134 lines (108 loc) · 4.17 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
from typing import List
from bokeh.models.widgets.tables import DataTable
import geopandas as gpd
import pandas as pd
import streamlit as st
from bokeh.plotting import figure, output_file, show
from bokeh.tile_providers import CARTODBPOSITRON, get_provider, Vendors
from bokeh.plotting import gmap
from bokeh.models import ColumnDataSource, CustomJS
from bokeh.models import TableColumn
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 Large Energy Consumers")
with row1_2:
st.write(
"""
##
Examining the 200 largest energy consumers across county Dublin, as of the open-access Valuation.
Office Dataset, crossed with CIBSE TM46 Benchmarks. Select the Lasso Tool from the toolbar on the
right of the map to circle desired data and download it in csv format from the Interactive Map.
""")
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
df = pd.read_csv("data/top_200_building_demands.csv")
gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.longitude, df.latitude))
map_data = gdf[["building_type", "inferred_energy_mwh_per_year", "Benchmark", "address", "latitude", "longitude", "geometry"]]
map_data = map_data.set_crs(epsg="4326")
map_data = map_data.to_crs(epsg="3857")
map_data["x"] = map_data.geometry.apply(lambda row:row.x)
map_data["y"] = map_data.geometry.apply(lambda row:row.y)
map_data = map_data[["building_type", "inferred_energy_mwh_per_year", "Benchmark", "address", "x", "y"]]
df = map_data
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}})
)
"""
)
)
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="large-demand-data.csv">Download selected data in csv file</a>'
return href
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="lasso_select,zoom_in", 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}})
)
"""
)
)
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, line_color=None, 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)