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"""