File size: 4,556 Bytes
226f63d
5f37cd7
213d2b5
19b905a
8d6d0f6
d033501
 
5f37cd7
4470c81
62b4495
d033501
19b905a
213d2b5
 
 
 
 
 
19b905a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62b4495
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19b905a
d033501
 
 
 
 
 
 
 
 
 
 
 
5f37cd7
 
 
 
 
 
 
d033501
 
 
 
 
 
 
 
 
62b4495
d033501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62b4495
d033501
 
213d2b5
 
8d6d0f6
213d2b5
 
 
4470c81
8d6d0f6
 
 
213d2b5
4470c81
9b5d4f4
4470c81
 
9b5d4f4
4470c81
9b5d4f4
4470c81
 
213d2b5
 
 
8d6d0f6
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"""Functions for the layout of the Streamlit app, including the sidebar."""

import json
import os
from typing import Literal

import streamlit as st

from src import hf_utils
from src.config_parameters import params


def get_aoi_id_from_selector_preview(all_aois, name_id_preview):
    for aoi_id, aoi in all_aois.items():
        if aoi["name_id_preview"] == name_id_preview:
            return aoi_id


# Check if app is deployed
def is_app_on_streamlit():
    """Check whether the app is on streamlit or runs locally."""
    return "HOSTNAME" in os.environ and os.environ["HOSTNAME"] == "streamlit"


# General layout
def toggle_menu_button():
    """If app is on streamlit, hide menu button."""
    if is_app_on_streamlit():
        st.markdown(
            """
        <style>
            #MainMenu {visibility: hidden;}
            footer {visibility: hidden;}
        </style>
        """,
            unsafe_allow_html=True,
        )


# Home page
def set_home_page_style():
    """Set style home page."""
    st.markdown(
        """
    <style> p { font-size: %s; } </style>
    """
        % params["docs_fontsize"],
        unsafe_allow_html=True,
    )


# Documentation page
def set_doc_page_style():
    """Set style documentation page."""
    st.markdown(
        """
    <style> p { font-size: %s; } </style>
    """
        % params["docs_fontsize"],
        unsafe_allow_html=True,
    )


# Tool page
def set_tool_page_style():
    """Set style tool page."""
    st.markdown(
        """
            <style>
                .streamlit-expanderHeader {
                    font-size: %s;
                    color: #000053;
                }
                .stDateInput > label {
                    font-size: %s;
                }
                .stSlider > label {
                    font-size: %s;
                }
                .stRadio > label {
                    font-size: %s;
                }
                .stButton > button {
                    font-size: %s;
                    font-weight: %s;
                    background-color: %s;
                }
            </style>
        """
        % (
            params["expander_header_fontsize"],
            params["widget_header_fontsize"],
            params["widget_header_fontsize"],
            params["widget_header_fontsize"],
            params["button_text_fontsize"],
            params["button_text_fontweight"],
            params["button_background_color"],
        ),
        unsafe_allow_html=True,
    )


# Sidebar
def add_about():
    """
    Add about and contacts to sidebar.

    Inputs:
        None
    Returns:
        None
    """
    # About textbox
    st.sidebar.markdown("## About")
    st.sidebar.markdown(
        f"""
        <p>
            Todo: general about stuff <br />
            <a href='{params["url_github_repo"]}'>
            Github Repo</a>
        </p>
        """,
        unsafe_allow_html=True,
    )

    # Contacts textbox
    st.sidebar.markdown(" ")
    st.sidebar.markdown("## Contacts")

    # Add data scientists and emails
    contacts_text = ""
    for ds, email in params["data_scientists"].items():
        contacts_text += ds + (
            "<span style='float:right; margin-right: 3px;'>"
            "<a href='mailto:%s'>%s</a></span><br>" % (email, email)
        )

    # Add text box
    st.sidebar.markdown(
        """
        <div class='warning' style='
            background-color: %s;
            margin: 0px;
            padding: 1em;'
        '>
            <p style='
                margin-left:1em;
                font-size: 1rem;
                margin: 0px
            '>
                %s
            </p>
        </div>
        """
        % (params["about_box_background_color"], contacts_text),
        unsafe_allow_html=True,
    )


def get_existing_geojson(product_id, file_type: Literal["flood", "footprint"]):
    """
    Getting a saved GFM flood geojson in an output folder of GFM files. Merge in one feature group if multiple.
    """
    index_df = hf_utils.get_geojson_index_df()
    path_in_repo = index_df[index_df["product"] == product_id][
        f"{file_type}_geojson_path"
    ].values[0]

    hf_api = hf_utils.get_hf_api()
    subfolder, filename = path_in_repo.split("/")
    geojson_path = hf_api.hf_hub_download(
        repo_id="rodekruis/flood-mapping",
        filename=filename,
        repo_type="dataset",
        subfolder=subfolder,
    )

    with open(geojson_path, "r") as f:
        geojson_data = json.load(f)

    return geojson_data