File size: 2,067 Bytes
76ba4f4
2086e56
 
ac0650f
a8a3333
76ba4f4
 
a8a3333
 
76ba4f4
a8a3333
 
 
 
ac0650f
 
76ba4f4
 
 
 
 
 
ac0650f
 
76ba4f4
 
ac0650f
 
 
76ba4f4
ac0650f
 
 
 
 
76ba4f4
ac0650f
76ba4f4
ac0650f
 
76ba4f4
ac0650f
76ba4f4
 
 
 
 
ac0650f
 
76ba4f4
ac0650f
 
76ba4f4
ac0650f
 
 
 
76ba4f4
 
 
 
 
 
 
2086e56
ac0650f
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
import gradio as gr
import yfinance as yf
import pandas as pd
import random

# Mapping company names to their ticker symbols
company_dict = {
    "Apple": "AAPL",
    "Google": "GOOGL",
    "Microsoft": "MSFT",
    "Amazon": "AMZN",
    "Tesla": "TSLA"
}

# Function to fetch ESG data for the selected company
def fetch_esg_data(company_name):
    # Get the ticker symbol from the company name
    ticker = company_dict[company_name]
    
    # Fetch ESG data from Yahoo Finance
    stock = yf.Ticker(ticker)
    esg_data = stock.sustainability
    
    # If ESG data is available, process it into a DataFrame
    if esg_data is not None:
        esg_df = pd.DataFrame(esg_data)

        # Extract only the relevant ESG scores and convert to a DataFrame
        esg_scores = esg_df.loc[["environmentScore", "socialScore", "governanceScore"], :].dropna().astype(float)
        
        # Prepare a DataFrame for plotting
        plot_df = pd.DataFrame({
            "ESG Category": ["Environment", "Social", "Governance"],
            "Score": esg_scores.squeeze().values
        })
        
        return plot_df
    else:
        # Return an empty DataFrame if no data is available
        return pd.DataFrame()

# Gradio interface with a dropdown for company selection and line plot visualization
def app_interface():
    with gr.Blocks() as app:
        # Dropdown to select company name
        company = gr.Dropdown(label="Select Company", choices=list(company_dict.keys()), value="Apple")
        
        # Button to fetch and plot ESG data
        plot_button = gr.Button("Generate ESG Plot")
        
        # LinePlot component for displaying the ESG data
        plot_output = gr.LinePlot(label="ESG Scores Plot", x="ESG Category", y="Score", overlay_point=True)

        # Define the action when the "Generate ESG Plot" button is clicked
        plot_button.click(fn=fetch_esg_data, 
                          inputs=company, 
                          outputs=plot_output)

    return app

# Launch the Gradio app
app = app_interface()
app.launch()