Spaces:
Sleeping
Sleeping
File size: 2,302 Bytes
76ba4f4 2086e56 a8a3333 76ba4f4 a8a3333 76ba4f4 a8a3333 ac0650f 76ba4f4 1cfde01 76ba4f4 ac0650f 0919ad9 2de254e 76ba4f4 2de254e 76ba4f4 51a15a5 76ba4f4 51a15a5 49a5185 51a15a5 76ba4f4 51a15a5 1cfde01 2de254e |
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 |
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 with multiple tabs
def app_interface():
with gr.Blocks() as app:
gr.Markdown("# ESG Data Explorer")
with gr.Tab("ESG Data"):
with gr.Row():
company = gr.Dropdown(
label="Select Company",
choices=list(company_dict.keys()),
value="Apple", # Default value
interactive=True # Make it interactive
)
plot_button = gr.Button("Generate ESG Plot")
line_plot = gr.LinePlot(label="ESG Scores Line Plot", x="ESG Category", y="Score", overlay_point=True)
scatter_plot = gr.ScatterPlot(label="ESG Scores Scatter Plot", x="ESG Category", y="Score", overlay_point=True)
csv_output = gr.File(label="Download CSV")
plot_button.click(fn=fetch_esg_data, inputs=company, outputs=[line_plot, csv_output, scatter_plot])
with gr.Tab("Tab 2"):
gr.Markdown("Content for Tab 2 goes here.")
with gr.Tab("Tab 3"):
gr.Markdown("Content for Tab 3 goes here.")
with gr.Tab("Tab 4"):
gr.Markdown("Content for Tab 4 goes here.")
return app
# Launch the Gradio app
app = app_interface()
if __name__ == "__main__":
app.launch()
|