Spaces:
Running
Running
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") | |