Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,63 +13,64 @@ company_dict = {
|
|
13 |
|
14 |
# Function to fetch ESG data for the selected company
|
15 |
def fetch_esg_data(company_name):
|
|
|
16 |
ticker = company_dict[company_name]
|
|
|
|
|
17 |
stock = yf.Ticker(ticker)
|
18 |
esg_data = stock.sustainability
|
19 |
-
|
|
|
|
|
20 |
esg_df = pd.DataFrame(esg_data)
|
|
|
|
|
21 |
esg_scores = esg_df.loc[["environmentScore", "socialScore", "governanceScore"], :].dropna().astype(float)
|
|
|
|
|
22 |
plot_df = pd.DataFrame({
|
23 |
"ESG Category": ["Environment", "Social", "Governance"],
|
24 |
"Score": esg_scores.squeeze().values
|
25 |
})
|
|
|
26 |
# Save the ESG data to a CSV file
|
27 |
csv_filename = f"{ticker}_esg_data.csv"
|
28 |
esg_df.to_csv(csv_filename)
|
29 |
-
|
|
|
30 |
else:
|
|
|
31 |
return pd.DataFrame(), None
|
32 |
|
33 |
-
# Gradio interface with a dropdown and
|
34 |
def app_interface():
|
35 |
with gr.Blocks() as app:
|
36 |
-
|
37 |
-
|
38 |
-
with gr.Tab("ESG Data"):
|
39 |
-
company = gr.Dropdown(
|
40 |
-
label="Select Company",
|
41 |
-
choices=list(company_dict.keys()), # Show company names
|
42 |
-
value="Apple", # Default value
|
43 |
-
interactive=True # Make it interactive
|
44 |
-
)
|
45 |
-
|
46 |
-
plot_button = gr.Button("Generate ESG Plot")
|
47 |
-
|
48 |
-
# Graphs for ESG scores
|
49 |
-
line_plot = gr.LinePlot(label="ESG Scores Line Plot", x="ESG Category", y="Score", overlay_point=True)
|
50 |
-
scatter_plot = gr.ScatterPlot(label="ESG Scores Scatter Plot", x="ESG Category", y="Score", overlay_point=True)
|
51 |
-
|
52 |
-
# File output for CSV download
|
53 |
-
csv_output = gr.File(label="Download CSV")
|
54 |
-
|
55 |
-
# Define the action when the "Generate ESG Plot" button is clicked
|
56 |
-
plot_button.click(fn=fetch_esg_data, inputs=company, outputs=[line_plot, csv_output, scatter_plot])
|
57 |
-
|
58 |
-
# Additional tabs for other content
|
59 |
-
with gr.Tab("Tab 2"):
|
60 |
-
gr.Markdown("Content for Tab 2 goes here.")
|
61 |
|
62 |
-
|
63 |
-
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
return app
|
69 |
|
70 |
# Launch the Gradio app
|
71 |
app = app_interface()
|
72 |
-
|
73 |
-
app.launch()
|
74 |
-
|
75 |
|
|
|
13 |
|
14 |
# Function to fetch ESG data for the selected company
|
15 |
def fetch_esg_data(company_name):
|
16 |
+
# Get the ticker symbol from the company name
|
17 |
ticker = company_dict[company_name]
|
18 |
+
|
19 |
+
# Fetch ESG data from Yahoo Finance
|
20 |
stock = yf.Ticker(ticker)
|
21 |
esg_data = stock.sustainability
|
22 |
+
|
23 |
+
# If ESG data is available, process it into a DataFrame
|
24 |
+
if esg_data is not None:
|
25 |
esg_df = pd.DataFrame(esg_data)
|
26 |
+
|
27 |
+
# Extract only the relevant ESG scores and convert to a DataFrame
|
28 |
esg_scores = esg_df.loc[["environmentScore", "socialScore", "governanceScore"], :].dropna().astype(float)
|
29 |
+
|
30 |
+
# Prepare a DataFrame for plotting
|
31 |
plot_df = pd.DataFrame({
|
32 |
"ESG Category": ["Environment", "Social", "Governance"],
|
33 |
"Score": esg_scores.squeeze().values
|
34 |
})
|
35 |
+
|
36 |
# Save the ESG data to a CSV file
|
37 |
csv_filename = f"{ticker}_esg_data.csv"
|
38 |
esg_df.to_csv(csv_filename)
|
39 |
+
|
40 |
+
return plot_df, csv_filename # Return the plot DataFrame and the CSV filename
|
41 |
else:
|
42 |
+
# Return an empty DataFrame and None if no data is available
|
43 |
return pd.DataFrame(), None
|
44 |
|
45 |
+
# Gradio interface with a dropdown for company selection, line plot, scatter plot visualization, and CSV download
|
46 |
def app_interface():
|
47 |
with gr.Blocks() as app:
|
48 |
+
# Dropdown to select company name
|
49 |
+
company = gr.Dropdown(label="Select Company", choices=list(company_dict.keys()), value="Apple")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
+
# Button to fetch and plot ESG data
|
52 |
+
plot_button = gr.Button("Generate ESG Plots")
|
53 |
|
54 |
+
# LinePlot component for displaying the ESG data
|
55 |
+
line_plot_output = gr.LinePlot(label="ESG Scores Line Plot", x="ESG Category", y="Score", overlay_point=True)
|
56 |
+
|
57 |
+
# ScatterPlot component for displaying the ESG data
|
58 |
+
scatter_plot_output = gr.ScatterPlot(label="ESG Scores Scatter Plot", x="ESG Category", y="Score", overlay_point=True)
|
59 |
+
|
60 |
+
# Textbox to display messages
|
61 |
+
message = gr.Textbox(label="Message", interactive=False)
|
62 |
+
|
63 |
+
# File output for CSV download
|
64 |
+
csv_output = gr.File(label="Download CSV")
|
65 |
+
|
66 |
+
# Define the action when the "Generate ESG Plot" button is clicked
|
67 |
+
plot_button.click(fn=fetch_esg_data,
|
68 |
+
inputs=company,
|
69 |
+
outputs=[line_plot_output, scatter_plot_output, csv_output])
|
70 |
+
|
71 |
return app
|
72 |
|
73 |
# Launch the Gradio app
|
74 |
app = app_interface()
|
75 |
+
app.launch()
|
|
|
|
|
76 |
|