-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
122 lines (105 loc) · 4.66 KB
/
app.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
import streamlit as st
from datetime import datetime, timedelta
from streamlit_folium import st_folium
from utils import get_irrad_data, get_location_from_addr
from helpers import get_map, plot_monthly_energy_generated
st.set_page_config(page_title='Energy Generation Forecaster', layout='wide', page_icon='🔌')
if 'pressed_change_addr' not in st.session_state:
st.session_state.pressed_change_addr = False
if 'changed_addr' not in st.session_state:
st.session_state.changed_addr = False
if 'loc_name' not in st.session_state:
st.session_state.loc_name = 'São Paulo, SP'
if 'loc_lat' not in st.session_state:
st.session_state.loc_lat = -23.55
if 'loc_lon' not in st.session_state:
st.session_state.loc_lon = -46.64
if 'irrad_data' not in st.session_state or st.session_state.changed_addr:
st.session_state.changed_addr = False
st.session_state.irrad_data = get_irrad_data(st.session_state.loc_lat, st.session_state.loc_lon)
with open('style.css') as f:
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
#### SIDEBAR ###########
with st.sidebar:
# user inputs
panels_area = st.number_input(
"📐 Área total em m² dos painéis solares",
step=5,
min_value=2,
value=18,
help='Soma da área de todos os painéis solares instalados'
)
panels_rend = st.slider(
"💱 Eficiência média dos painéis solares (%)",
min_value=0,
max_value=100,
step=1,
format="%d%%",
value=15,
help="Porcentagem de energia solar (irradiação) convertida em eletricidade pelos painéis solares"
)
energy_price = st.number_input(
"💵 Tarifa média por kWh (R$)",
min_value=0.05,
max_value=2.0,
step=0.001,
value=0.656,
format="%.3f",
help='Preço cobrado por quilowatt-hora de energia elétrica'
)
col_desc_loc, col_alt_loc = st.columns(2)
with col_desc_loc:
st.markdown(f'📌 {st.session_state.loc_name}')
with col_alt_loc:
change_addr = st.button(label='Alterar', key='alt_loc')
if change_addr or st.session_state.pressed_change_addr:
st.session_state.pressed_change_addr = True
with st.form("change_loc_form"):
loc_input = st.text_input('Digite a nova localização')
submit_new_loc = st.form_submit_button('Procurar')
if submit_new_loc:
st.session_state.pressed_change_addr = False
try:
location = get_location_from_addr(loc_input)
new_loc_name = (
f"{location.raw['address']['city']}, "
f"{location.raw['address']['ISO3166-2-lvl4'][-2:]}"
)
except BaseException:
st.error('Could not find address')
else:
st.session_state.loc_name = new_loc_name
lat, lon = float(location.raw['lat']), float(location.raw['lon'])
if lat == st.session_state.loc_lat and lon == st.session_state.loc_lon:
# If lat and lon doesn't change, ignore
pass
else:
# Else update app
st.session_state.changed_addr = True
st.session_state.loc_lat, st.session_state.loc_lon = lat, lon
st.experimental_rerun()
if not st.session_state.pressed_change_addr:
m = get_map(st.session_state.loc_lat, st.session_state.loc_lon)
st_folium(m, height=100, width=None)
##########################
st.header('🔌 Energy Generation Forecaster')
pred_irrad_sum = st.session_state.irrad_data.query('type == "predicted"')['shortwave_radiation_sum'].sum()
# Calculate generated energy
gen_energy = pred_irrad_sum * panels_area * panels_rend / 100
savings = gen_energy * energy_price
col_mainsub1, col_mainsub2 = st.columns(2)
with col_mainsub1:
st.metric(label='Total Estimated Generated Energy', value=f'⚡ {gen_energy:,.0f} kW/h')
with col_mainsub2:
st.metric(label='Estimated saving', value=f'💲 R$ {savings:,.2f}')
st.subheader('Monthly Potential Generated Energy')
ten_years_ago = datetime.today() - timedelta(days=365 * 10)
st.session_state.irrad_data = st.session_state.irrad_data.assign(
kWh=st.session_state.irrad_data[
'shortwave_radiation_sum'] * panels_area * panels_rend / 100
)
fig1 = plot_monthly_energy_generated(
# Plot last 10 years of data and predicted values
data=st.session_state.irrad_data[(st.session_state.irrad_data['time'] >= ten_years_ago)]
)
st.plotly_chart(fig1, theme="streamlit", use_container_width=True)