ESG_Dashboard / app.py
Manasa1's picture
Update app.py
2de254e verified
raw
history blame
2.2 kB
import gradio as gr
import yfinance as yf
import pandas as pd
# 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):
ticker = company_dict[company_name]
stock = yf.Ticker(ticker)
esg_data = stock.sustainability
if esg_data is not None and not esg_data.empty:
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
})
# Save the ESG data to a CSV file
csv_filename = f"{ticker}_esg_data.csv"
esg_df.to_csv(csv_filename)
return plot_df, csv_filename
else:
return pd.DataFrame(), None
# Gradio interface
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", # Default value
interactive=True # Make it interactive
)
# Button to fetch and plot ESG data
plot_button = gr.Button("Generate ESG Plot")
# LinePlot component for displaying the ESG data
line_plot = gr.LinePlot(label="ESG Scores Line Plot", x="ESG Category", y="Score", overlay_point=True)
# ScatterPlot component for displaying the ESG data
scatter_plot = gr.ScatterPlot(label="ESG Scores Scatter Plot", x="ESG Category", y="Score", overlay_point=True)
# File output for CSV download
csv_output = gr.File(label="Download CSV")
# Define the action when the "Generate ESG Plot" button is clicked
plot_button.click(fn=fetch_esg_data, inputs=company, outputs=[line_plot, csv_output, scatter_plot])
return app
# Launch the Gradio app
app = app_interface()
app.launch()