ESG_Dashboard / app.py
Manasa1's picture
Update app.py
e03238f verified
raw
history blame
2.87 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):
# 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
})
# 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 # Return the plot DataFrame and the CSV filename
else:
# Return an empty DataFrame and None if no data is available
return pd.DataFrame(), None
# Gradio interface with multiple tabs
def app_interface():
with gr.Blocks() as app:
with gr.Tab("ESG Data Analysis"):
# 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)
bar_plot = gr.BarPlot(label="Sample Bar Plot", x="ESG Category", y="Score", x_bins=10, y_aggregate="sum")
# 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=[plot_output,bar_plot, csv_output])
with gr.Tab("Tab 2"):
gr.Markdown("This is Tab 2. You can add more content here.")
with gr.Tab("Tab 3"):
gr.Markdown("This is Tab 3. Add your custom functionality here.")
with gr.Tab("Tab 4"):
gr.Markdown("This is Tab 4. Add more features or visualization here.")
return app
# Launch the Gradio app
app = app_interface()
app.launch()