File size: 8,640 Bytes
76ba4f4
2086e56
 
f042f47
 
 
 
 
 
 
a8a3333
76ba4f4
f042f47
a8a3333
 
76ba4f4
a8a3333
f042f47
 
 
 
a8a3333
 
d9463be
f042f47
 
 
 
 
 
d9463be
 
 
f042f47
 
d9463be
f042f47
 
d9463be
 
 
f042f47
631a049
f042f47
d9463be
f042f47
 
 
 
 
 
d9463be
 
 
 
 
 
 
 
f042f47
 
 
d9463be
f042f47
 
d9463be
 
 
f042f47
631a049
f042f47
d9463be
f042f47
 
 
 
 
d9463be
f042f47
 
 
d9463be
f042f47
 
d9463be
 
 
f042f47
631a049
f59d75e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631a049
d9463be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631a049
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f042f47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631a049
 
 
 
 
73fc605
d9463be
f59d75e
 
 
 
d9463be
f042f47
 
f59d75e
d9463be
f59d75e
e03238f
f59d75e
631a049
 
 
 
d9463be
f59d75e
631a049
f59d75e
d9463be
 
 
f59d75e
631a049
f59d75e
090fb84
0e731dc
f042f47
631a049
f042f47
f59d75e
f042f47
 
090fb84
f042f47
 
 
 
 
 
 
 
d9463be
 
f59d75e
 
 
f042f47
 
 
 
 
 
090fb84
76ba4f4
 
090fb84
f042f47
631a049
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import gradio as gr
import yfinance as yf
import pandas as pd
import plotly.graph_objects as go
from typing import Tuple, Optional
import logging

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Mapping company names to their ticker symbols
COMPANY_DICT = {
    "Apple": "AAPL",
    "Google": "GOOGL",
    "Microsoft": "MSFT",
    "Amazon": "AMZN",
    "Tesla": "TSLA",
    "Meta": "META",
    "NVIDIA": "NVDA",
    "Netflix": "NFLX"
}

def create_line_plot(df: pd.DataFrame, company_name: str) -> go.Figure:
    """Create a line plot using plotly"""
    fig = go.Figure()
    fig.add_trace(go.Scatter(
        x=df["ESG Category"],
        y=df["Score"],
        mode='lines+markers',
        name='ESG Score',
        line=dict(color='rgb(55, 83, 109)', width=2),
        marker=dict(size=10)
    ))
    fig.update_layout(
        title=f"ESG Scores Trend for {company_name}",
        xaxis_title="ESG Category",
        yaxis_title="Score",
        yaxis_range=[0, 100],
        height=400,
        template='plotly_white'
    )
    return fig

def create_scatter_plot(df: pd.DataFrame, company_name: str) -> go.Figure:
    """Create a scatter plot using plotly"""
    fig = go.Figure()
    fig.add_trace(go.Scatter(
        x=df["ESG Category"],
        y=df["Score"],
        mode='markers',
        marker=dict(
            size=15,
            color='rgb(55, 83, 109)',
            line=dict(
                color='rgb(8,48,107)',
                width=2
            )
        ),
        name='ESG Score'
    ))
    fig.update_layout(
        title=f"ESG Score Distribution for {company_name}",
        xaxis_title="ESG Category",
        yaxis_title="Score",
        yaxis_range=[0, 100],
        height=400,
        template='plotly_white'
    )
    return fig

def create_bar_plot(df: pd.DataFrame, company_name: str) -> go.Figure:
    """Create a bar plot using plotly"""
    fig = go.Figure()
    fig.add_trace(go.Bar(
        x=df["ESG Category"],
        y=df["Score"],
        marker_color='rgb(55, 83, 109)',
        name='ESG Score'
    ))
    fig.update_layout(
        title=f"ESG Scores Comparison for {company_name}",
        xaxis_title="ESG Category",
        yaxis_title="Score",
        yaxis_range=[0, 100],
        height=400,
        template='plotly_white'
    )
    return fig
def create_pie_chart(df: pd.DataFrame, company_name: str) -> go.Figure:
    """Create a pie chart using plotly"""
    total_score = df['Score'].sum()
    percentages = (df['Score'] / total_score * 100).round(1)
    
    # Create labels with both category and percentage
    labels = [f"{cat} ({pct}%)" for cat, pct in zip(df['ESG Category'], percentages)]
    
    fig = go.Figure()
    fig.add_trace(go.Pie(
        labels=labels,
        values=df['Score'],
        hole=0.4,  # Creates a donut chart
        marker=dict(
            colors=['rgb(55, 83, 109)', 'rgb(26, 118, 255)', 'rgb(178, 200, 223)']
        ),
        textinfo='label+value',
        textposition='outside',
        texttemplate='%{label}<br>Score: %{value:.1f}'
    ))
    fig.update_layout(
        title=f"ESG Score Distribution for {company_name}",
        height=400,
        template='plotly_white',
        showlegend=False
    )
    return fig


def create_empty_plot(message: str) -> go.Figure:
    """Create an empty plot with an error message"""
    fig = go.Figure()
    fig.add_annotation(
        text=message,
        xref="paper",
        yref="paper",
        x=0.5,
        y=0.5,
        showarrow=False,
        font=dict(size=14)
    )
    fig.update_layout(
        xaxis_visible=False,
        yaxis_visible=False,
        height=400
    )
    return fig

def fetch_esg_data(company_name: str) -> Tuple[Optional[pd.DataFrame], str]:
    """
    Fetch and process ESG data for the selected company.
    """
    try:
        ticker = COMPANY_DICT[company_name]
        logger.info(f"Fetching ESG data for {company_name} ({ticker})")
        
        stock = yf.Ticker(ticker)
        esg_data = stock.sustainability
        
        if esg_data is None:
            return None, f"No ESG data available for {company_name}"
            
        esg_df = pd.DataFrame(esg_data)
        esg_scores = esg_df.loc[["environmentScore", "socialScore", "governanceScore"], :].dropna().astype(float)
        
        plot_df = pd.DataFrame({
            "ESG Category": ["Environment", "Social", "Governance"],
            "Score": esg_scores.squeeze().values
        })
        
        return plot_df, f"Successfully fetched ESG data for {company_name}"
        
    except Exception as e:
        logger.error(f"Error fetching ESG data: {str(e)}")
        return None, f"Error fetching ESG data: {str(e)}"

def create_interface() -> gr.Blocks:
    """Create the Gradio interface"""
    with gr.Blocks(theme=gr.themes.Soft()) as app:
        gr.Markdown("# ESG Data Visualization Dashboard")
        gr.Markdown("Analyze Environmental, Social, and Governance scores for major tech companies.")
        
        with gr.Tab("ESG Analysis"):
            with gr.Row():
                with gr.Column():
                    company = gr.Dropdown(
                        label="Select Company",
                        choices=list(COMPANY_DICT.keys()),
                        value="Apple"
                    )
                    plot_button = gr.Button("Generate ESG Analysis", variant="primary")
                    status_message = gr.Textbox(
                        label="Status",
                        interactive=False,
                        visible=True
                    )
            
            with gr.Row():
                with gr.Column():
                    line_plot = gr.Plot(label="Trend Analysis")
                with gr.Column():
                    pie_plot = gr.Plot(label="Distribution Analysis")
            
            with gr.Row():
                with gr.Column():
                    scatter_plot = gr.Plot(label="Score Distribution")
                with gr.Column():
                    bar_plot = gr.Plot(label="Score Comparison")
            
            def process_esg_request(company_name: str) -> Tuple[go.Figure, go.Figure, go.Figure, go.Figure, str]:
                # Fetch the data
                plot_df, status = fetch_esg_data(company_name)
                
                if plot_df is None:
                    empty_plot = create_empty_plot(status)
                    return empty_plot, empty_plot, empty_plot, empty_plot, status
                
                # Create all plots
                line = create_line_plot(plot_df, company_name)
                scatter = create_scatter_plot(plot_df, company_name)
                bar = create_bar_plot(plot_df, company_name)
                pie = create_pie_chart(plot_df, company_name)
                
                return line, pie, scatter, bar, status
            
            # Connect the button click to the process function
            plot_button.click(
                fn=process_esg_request,
                inputs=company,
                outputs=[line_plot, pie_plot, scatter_plot, bar_plot, status_message],
                api_name="generate_esg_analysis"
            )
        
        with gr.Tab("About"):
            gr.Markdown("""
                ## About This Dashboard
                
                This dashboard provides ESG (Environmental, Social, and Governance) data visualization for major technology companies. The data is sourced from Yahoo Finance and updated regularly.
                
                ### How to Use
                1. Select a company from the dropdown menu
                2. Click 'Generate ESG Analysis' to view multiple visualizations:
                   - Trend Analysis: Shows the progression across ESG categories
                   - Distribution Analysis: Shows the relative proportion of each ESG component
                   - Score Distribution: Displays the spread of ESG scores
                   - Score Comparison: Compares ESG scores side by side
                
                ### Metrics Explained
                - **Environmental Score**: Measures company's environmental impact and sustainability initiatives
                - **Social Score**: Evaluates company's relationships with employees, suppliers, customers, and communities
                - **Governance Score**: Assesses company's leadership, executive pay, audits, internal controls, and shareholder rights
            """)
    
    return app

if __name__ == "__main__":
    app = create_interface()
    app.launch(share=True, debug=True)