-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlit_app.py
161 lines (133 loc) · 4.9 KB
/
streamlit_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import streamlit as st
from streamlit_extras.stylable_container import stylable_container
from entsoe import Area as entsoe_areas
# from entsoe.mappings import lookup_area
import trio
import pathlib
import logging
from etl.etl import Orchestrator
app_logger = logging.getLogger("app_logger")
app_logger.addHandler(logging.StreamHandler())
app_logger.setLevel(logging.DEBUG)
API_URL = ""
APP_TITLE = "Energy Dashboard"
cfd = pathlib.Path(__file__).parent
COUNTRY_CODES = entsoe_areas.__members__.keys()
container_style = """
{
background-color: white;
border-radius: 8px;
margin: 0px 0px;
}
"""
def set_page_settings():
st.set_page_config(
page_title=APP_TITLE,
page_icon="⚡",
layout="wide",
)
with open(cfd / "static" / "style.css") as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
def initialize_session_state():
if "container_counter" not in st.session_state:
st.session_state.container_counter = 0
if "country_code" not in st.session_state:
st.session_state.country_code = "de" # default value
if "charts" not in st.session_state:
st.session_state.charts = {}
if "warning" not in st.session_state:
st.session_state.warning = None
if "grid_created" not in st.session_state:
st.session_state.grid_created = False
if "data_orchestrator" not in st.session_state:
st.session_state.data_orchestrator = None
if "warning_text" not in st.session_state:
st.session_state.warning_text = []
def display_header():
with stylable_container(
key="header_container",
css_styles=[
"""
{
background-color: #1B8A85;
color: white;
text-align: center;
[data-testid="stHorizontalBlock"] {
align-items: center;
column-gap: 0;
}
[data-testid="stSelectbox"] {
padding-top: 32px;
padding-bottom: 20px;
}
[data-testid="stVerticalBlockBorderWrapper"] {
margin: 0rem 2rem 0rem 2rem;
}
#energy-dashboard {
padding-bottom:0rem;
}
}
""",
],
):
left, mid, right = st.columns((0.1, 0.5, 0.4))
with left:
with st.container():
st.markdown("")
st.image("./static/icon.png", width=40)
with mid:
with st.container():
st.title(APP_TITLE) # , help="data from ENTSO-E and Energy Charts")
st.markdown("""powered with data from ENTSO-E and Energy Charts""")
with right:
with st.container():
st.selectbox(
"country_code",
COUNTRY_CODES,
24,
format_func=lambda x: ": ".join((x, entsoe_areas[x].meaning)),
label_visibility="collapsed",
on_change=set_country_code,
key="select_box",
)
def set_country_code():
st.session_state.country_code = st.session_state.select_box.lower()
print("st.session_state.country_code now: ", st.session_state.country_code)
st.session_state.data_orchestrator.country_code = st.session_state.country_code
st.session_state.data_orchestrator.data.clear()
st.session_state.warning_text.clear()
st.session_state.grid_created = False
def main_page():
if not st.session_state.grid_created:
st.session_state.warning = st.empty()
# generate chart grid and store in session_state (only once)
top_left, top_right = st.columns(2)
bottom_left, bottom_right = st.columns(2)
with top_left:
col1, col2, col3 = st.columns([3, 3, 4])
with col1:
st.session_state.charts["metric1"] = st.empty()
st.session_state.charts["total_generation"] = st.empty()
with col2:
st.session_state.charts["metric2"] = st.empty()
st.session_state.charts["renewables_generation"] = st.empty()
with col3:
st.session_state.charts["location"] = st.empty()
with top_right:
st.session_state.charts["current_generation_by_source"] = st.empty()
with bottom_right:
st.session_state.charts["current_electricity_mix"] = st.empty()
with bottom_left:
st.session_state.charts["daily_capacity_factor_by_source"] = st.empty()
st.session_state.grid_created = True
if not st.session_state.data_orchestrator:
st.session_state.data_orchestrator = Orchestrator("de")
trio.run(st.session_state.data_orchestrator.run_orchestration)
def main():
"""put the streamlit app together"""
set_page_settings()
initialize_session_state()
display_header()
main_page()
if __name__ == "__main__":
main()