Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -87,6 +87,34 @@ def create_bar_plot(df: pd.DataFrame, company_name: str) -> go.Figure:
|
|
87 |
template='plotly_white'
|
88 |
)
|
89 |
return fig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
def create_empty_plot(message: str) -> go.Figure:
|
92 |
"""Create an empty plot with an error message"""
|
@@ -157,34 +185,38 @@ def create_interface() -> gr.Blocks:
|
|
157 |
)
|
158 |
|
159 |
with gr.Row():
|
160 |
-
|
|
|
|
|
|
|
161 |
|
162 |
with gr.Row():
|
163 |
with gr.Column():
|
164 |
-
scatter_plot = gr.Plot(label="Distribution
|
165 |
with gr.Column():
|
166 |
-
bar_plot = gr.Plot(label="Comparison
|
167 |
|
168 |
-
def process_esg_request(company_name: str) -> Tuple[go.Figure, go.Figure, go.Figure, str]:
|
169 |
# Fetch the data
|
170 |
plot_df, status = fetch_esg_data(company_name)
|
171 |
|
172 |
if plot_df is None:
|
173 |
empty_plot = create_empty_plot(status)
|
174 |
-
return empty_plot, empty_plot, empty_plot, status
|
175 |
|
176 |
-
# Create all
|
177 |
line = create_line_plot(plot_df, company_name)
|
178 |
scatter = create_scatter_plot(plot_df, company_name)
|
179 |
bar = create_bar_plot(plot_df, company_name)
|
|
|
180 |
|
181 |
-
return line, scatter, bar, status
|
182 |
|
183 |
# Connect the button click to the process function
|
184 |
plot_button.click(
|
185 |
fn=process_esg_request,
|
186 |
inputs=company,
|
187 |
-
outputs=[line_plot, scatter_plot, bar_plot, status_message],
|
188 |
api_name="generate_esg_analysis"
|
189 |
)
|
190 |
|
@@ -198,8 +230,9 @@ def create_interface() -> gr.Blocks:
|
|
198 |
1. Select a company from the dropdown menu
|
199 |
2. Click 'Generate ESG Analysis' to view multiple visualizations:
|
200 |
- Trend Analysis: Shows the progression across ESG categories
|
201 |
-
- Distribution Analysis:
|
202 |
-
-
|
|
|
203 |
|
204 |
### Metrics Explained
|
205 |
- **Environmental Score**: Measures company's environmental impact and sustainability initiatives
|
|
|
87 |
template='plotly_white'
|
88 |
)
|
89 |
return fig
|
90 |
+
def create_pie_chart(df: pd.DataFrame, company_name: str) -> go.Figure:
|
91 |
+
"""Create a pie chart using plotly"""
|
92 |
+
total_score = df['Score'].sum()
|
93 |
+
percentages = (df['Score'] / total_score * 100).round(1)
|
94 |
+
|
95 |
+
# Create labels with both category and percentage
|
96 |
+
labels = [f"{cat} ({pct}%)" for cat, pct in zip(df['ESG Category'], percentages)]
|
97 |
+
|
98 |
+
fig = go.Figure()
|
99 |
+
fig.add_trace(go.Pie(
|
100 |
+
labels=labels,
|
101 |
+
values=df['Score'],
|
102 |
+
hole=0.4, # Creates a donut chart
|
103 |
+
marker=dict(
|
104 |
+
colors=['rgb(55, 83, 109)', 'rgb(26, 118, 255)', 'rgb(178, 200, 223)']
|
105 |
+
),
|
106 |
+
textinfo='label+value',
|
107 |
+
textposition='outside',
|
108 |
+
texttemplate='%{label}<br>Score: %{value:.1f}'
|
109 |
+
))
|
110 |
+
fig.update_layout(
|
111 |
+
title=f"ESG Score Distribution for {company_name}",
|
112 |
+
height=400,
|
113 |
+
template='plotly_white',
|
114 |
+
showlegend=False
|
115 |
+
)
|
116 |
+
return fig
|
117 |
+
|
118 |
|
119 |
def create_empty_plot(message: str) -> go.Figure:
|
120 |
"""Create an empty plot with an error message"""
|
|
|
185 |
)
|
186 |
|
187 |
with gr.Row():
|
188 |
+
with gr.Column():
|
189 |
+
line_plot = gr.Plot(label="Trend Analysis")
|
190 |
+
with gr.Column():
|
191 |
+
pie_plot = gr.Plot(label="Distribution Analysis")
|
192 |
|
193 |
with gr.Row():
|
194 |
with gr.Column():
|
195 |
+
scatter_plot = gr.Plot(label="Score Distribution")
|
196 |
with gr.Column():
|
197 |
+
bar_plot = gr.Plot(label="Score Comparison")
|
198 |
|
199 |
+
def process_esg_request(company_name: str) -> Tuple[go.Figure, go.Figure, go.Figure, go.Figure, str]:
|
200 |
# Fetch the data
|
201 |
plot_df, status = fetch_esg_data(company_name)
|
202 |
|
203 |
if plot_df is None:
|
204 |
empty_plot = create_empty_plot(status)
|
205 |
+
return empty_plot, empty_plot, empty_plot, empty_plot, status
|
206 |
|
207 |
+
# Create all plots
|
208 |
line = create_line_plot(plot_df, company_name)
|
209 |
scatter = create_scatter_plot(plot_df, company_name)
|
210 |
bar = create_bar_plot(plot_df, company_name)
|
211 |
+
pie = create_pie_chart(plot_df, company_name)
|
212 |
|
213 |
+
return line, pie, scatter, bar, status
|
214 |
|
215 |
# Connect the button click to the process function
|
216 |
plot_button.click(
|
217 |
fn=process_esg_request,
|
218 |
inputs=company,
|
219 |
+
outputs=[line_plot, pie_plot, scatter_plot, bar_plot, status_message],
|
220 |
api_name="generate_esg_analysis"
|
221 |
)
|
222 |
|
|
|
230 |
1. Select a company from the dropdown menu
|
231 |
2. Click 'Generate ESG Analysis' to view multiple visualizations:
|
232 |
- Trend Analysis: Shows the progression across ESG categories
|
233 |
+
- Distribution Analysis: Shows the relative proportion of each ESG component
|
234 |
+
- Score Distribution: Displays the spread of ESG scores
|
235 |
+
- Score Comparison: Compares ESG scores side by side
|
236 |
|
237 |
### Metrics Explained
|
238 |
- **Environmental Score**: Measures company's environmental impact and sustainability initiatives
|