File size: 3,174 Bytes
86e2833
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50ac51e
86e2833
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
### Création de l'application Streamlit de SmartRescue ###

import base64
from streamlit_option_menu import option_menu
import uvicorn
from api import api
from multiprocessing import Process
from pathlib import Path
import streamlit as st

from views.home import home_page
from views.dashboard import dashboard_page
from views.aide_telephonique import aide_telephonique_page
from views.admin import adm_page

APP_TITLE = "SmartRescue"
API_PORT = 8901

logopath = Path(__file__).parent.parent / "assets" / "logo.png"
icopath = Path(__file__).parent.parent / "assets" / "icone.png"

st.set_page_config(
    page_title=APP_TITLE,
    layout="wide",
    initial_sidebar_state="expanded",
    page_icon=icopath,
)


def add_logo():
    """

    Fonction d'insertion du logo en arrière-plan du menu latéral de Streamlit.



    Il s'agit de la méthode conseillée par les mainteneurs de Streamlit :

    https://discuss.streamlit.io/t/put-logo-and-title-above-on-top-of-page-navigation-in-sidebar-of-multipage-app/28213/6

    """
    # Lecture du fichier image local
    with open(logopath, "rb") as f:
        logo_data = base64.b64encode(f.read()).decode()

    st.markdown(
        f"""

        <style>

            [data-testid="stSidebar"] > div {{

                background-image: url("data:image/png;base64,{logo_data}");

                background-repeat: no-repeat;

                margin-top: 25px;

                background-position: 20px 20px;

                background-size: 300px;

            }}

        </style>

        """,
        unsafe_allow_html=True,
    )


def run_fastapi(port: int) -> None:
    """

    Démarre le serveur API FastAPI en arrière-plan.



    Le serveur est configuré pour écouter sur 127.0.0.1,

    et le port spécifié en argument.



    Param :

            - port: Port d'écoute du serveur API

    """
    try:
        uvicorn.run(api, host="127.0.0.1", port=port)
    except Exception as e:
        st.error(f"Échec du démarrage du serveur API : {str(e)}")


def main():
    """

    Fonction main de l'application Streamlit.



    On lance d'abord le serveur FastAPI en arrière-plan,

    puis on affiche l'interface utilisateur Streamlit.

    """
    # Démarrer FastAPI en arrière-plan, dans un autre process
    if "api_process" not in st.session_state:
        st.session_state["api_process"] = Process(
            target=run_fastapi, args=(API_PORT,), daemon=True
        )
        st.session_state["api_process"].start()

    # Interface utilisateur Streamlit
    with st.sidebar:
        add_logo()
        selected = option_menu(
            menu_title="Navigation",
            options=["Home", "Aide téléphonique", "Dashboard", "Admin"],
            icons=["house", "phone", "bar-chart", "key"],
            default_index=0,
        )

    if selected == "Home":
        home_page()
    elif selected == "Aide téléphonique":
        aide_telephonique_page()
    elif selected == "Dashboard":
        dashboard_page()
    elif selected == "Admin":
        adm_page()


if __name__ == "__main__":
    main()