File size: 4,404 Bytes
19341ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d486d32
19341ef
 
 
 
 
 
d486d32
19341ef
 
 
 
 
 
f484ffe
 
 
19341ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207fedd
19341ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import plotly.graph_objects as go
import plotly.express as px

from PIL import Image
import pdfkit
import pandas as pd 
import plotly.io as pio
import tempfile


def construct_plot():
    # Define data
    stakeholders = st.session_state['pp_grouped']

    if stakeholders is None or len(stakeholders) == 0:
        st.error("Aucune partie prenante n'a été définie")
        return None
    # Create plot
    fig = go.Figure()

    # Add category areas
    fig.add_shape(type="rect", x0=0, y0=50, x1=50, y1=100, fillcolor="lightblue", opacity=0.2, line_width=0)
    fig.add_shape(type="rect", x0=50, y0=50, x1=100, y1=100, fillcolor="lightyellow", opacity=0.2, line_width=0)
    fig.add_shape(type="rect", x0=0, y0=0, x1=50, y1=50, fillcolor="lightcoral", opacity=0.2, line_width=0)
    fig.add_shape(type="rect", x0=50, y0=0, x1=100, y1=50, fillcolor="lightcyan", opacity=0.2, line_width=0)

    # Add category titles
    fig.add_annotation(x=10, y=90, text="Rendre satisfait", showarrow=False)
    fig.add_annotation(x=60, y=90, text="Gérer étroitement", showarrow=False)
    fig.add_annotation(x=10, y=40, text="Suivre de près", showarrow=False)
    fig.add_annotation(x=60, y=40, text="Tenir informé", showarrow=False)

    x_array = [stakeholder['x'] for stakeholder in stakeholders]
    y_array = [stakeholder['y'] for stakeholder in stakeholders]
    name_array = [stakeholder['name'] for stakeholder in stakeholders]

    #color_sequence = px.colors.qualitative.Plotly
    # Add stakeholders to plot
    for i,stakeholder in enumerate(stakeholders):
        fig.add_trace(go.Scatter(
            x=[stakeholder['x']],
            y=[stakeholder['y']],
            mode='markers+text',
            marker=dict(color=stakeholder['color'],size=33),
            textposition="top center",
            name=stakeholder['name']
        ))

    # Update layout
    fig.update_layout(
        legend=dict( orientation="h", yanchor="bottom",y=1.02,title="Parties prenantes"),
        height=600,
        title=dict(text="Cartographie des parties prenantes", x=0.5, y=1, xanchor="center", yanchor="top"),
        xaxis=dict(title="Influence", range=[0, 100]),
        yaxis=dict(title="Pouvoir", range=[0, 100]),
        showlegend=True
    )

    # Display plot in Streamlit
    return fig

def save_plot_as_pdf(fig, logo_path, title):
    st.write("saving plot as pdf")

    with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmpfile:
        st.write("tmpfile created")
        image_bytes = pio.to_image(fig, format='png')
        st.write("image_bytes")

        tmpfile.write(image_bytes)
        tmpfile.close()
        plot_image_path = tmpfile.name
    
    st.write(plot_image_path)

    html_content = f"""
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>{title}</title>
        <style>
            body {{ font-family: Arial, sans-serif; text-align: center; }}
            .logo {{ width: 100px; margin-top: 20px; }}
            .title {{ font-size: 24px; margin-top: 20px; }}
            .plot-image {{ width: 80%; margin-top: 20px; }}
        </style>
    </head>
    <body>
        {"<img src='" + logo_path + "' class='logo'>" if logo_path else ""}
        <div class="title">{title}</div>
        <img src="{plot_image_path}" class="plot-image">
    </body>
    </html>
    """
    
    with tempfile.NamedTemporaryFile(delete=False, suffix=".html") as tmpfile:
        tmpfile.write(html_content.encode('utf-8'))
        tmpfile.close()
        html_path = tmpfile.name
    
    pdf_path = html_path.replace('figure.html', '.pdf')
    st.write(pdf_path)
    pdfkit.from_file(html_path, pdf_path)
    return pdf_path

def download_pdf():
    # Construct plot
    fig = construct_plot()
    
    if fig is None:
        return None
    st.write("fig constructed")

    logo_path = "https://static.wixstatic.com/media/d7d3da_b69e03ae99224f7d8b6e358918e60071~mv2.png/v1/crop/x_173,y_0,w_1906,h_938/fill/w_242,h_119,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/BZIIIT_LOGO-HORIZ-COULEUR.png"
    pdf_title = "Cartographie des parties prenantes"
    # Button to download PDF
    pdf_path = save_plot_as_pdf(fig, logo_path, pdf_title)

    st.write("pdf saved")
    with open(pdf_path, "rb") as pdf_file:
        st.download_button(label="Download PDF", data=pdf_file, file_name="stakeholder_analysis.pdf")