Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,60 @@
|
|
|
|
1 |
import yfinance as yf
|
2 |
import pandas as pd
|
3 |
-
import gradio as gr
|
4 |
-
import os
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
"Apple": "AAPL",
|
9 |
-
"Microsoft": "MSFT",
|
10 |
"Google": "GOOGL",
|
|
|
11 |
"Amazon": "AMZN",
|
12 |
"Tesla": "TSLA"
|
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 |
|
|
|
1 |
+
import gradio as gr
|
2 |
import yfinance as yf
|
3 |
import pandas as pd
|
|
|
|
|
4 |
|
5 |
+
# Mapping company names to their ticker symbols
|
6 |
+
company_dict = {
|
7 |
"Apple": "AAPL",
|
|
|
8 |
"Google": "GOOGL",
|
9 |
+
"Microsoft": "MSFT",
|
10 |
"Amazon": "AMZN",
|
11 |
"Tesla": "TSLA"
|
12 |
}
|
13 |
|
14 |
+
# Function to fetch ESG data and save it to a CSV file
|
15 |
+
def fetch_and_save_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 the data is available, save it to a CSV file
|
24 |
+
if esg_data is not None:
|
25 |
+
# Convert the ESG data to a pandas DataFrame
|
26 |
+
esg_df = pd.DataFrame(esg_data)
|
27 |
+
|
28 |
+
# Save it to a CSV file
|
29 |
+
filename = f"{ticker}_esg_data.csv"
|
30 |
+
esg_df.to_csv(filename)
|
31 |
+
|
32 |
+
return f"ESG data for {company_name} saved to {filename}", filename
|
33 |
+
else:
|
34 |
+
return f"No ESG data available for {company_name}", None
|
35 |
+
|
36 |
+
# Gradio interface with a dropdown for company selection
|
37 |
+
def app_interface():
|
38 |
+
with gr.Blocks() as app:
|
39 |
+
# Dropdown to select company name
|
40 |
+
company = gr.Dropdown(label="Select Company", choices=list(company_dict.keys()), value="Apple")
|
41 |
+
|
42 |
+
# Button to trigger ESG data extraction
|
43 |
+
fetch_button = gr.Button("Fetch ESG Data")
|
44 |
+
|
45 |
+
# Output text and download link
|
46 |
+
output_text = gr.Textbox(label="Output")
|
47 |
+
download_link = gr.File(label="Download CSV")
|
48 |
+
|
49 |
+
# Define the action when the button is clicked
|
50 |
+
fetch_button.click(fn=fetch_and_save_esg_data,
|
51 |
+
inputs=company,
|
52 |
+
outputs=[output_text, download_link])
|
53 |
+
|
54 |
+
return app
|
55 |
+
|
56 |
+
# Launch the Gradio app
|
57 |
+
app = app_interface()
|
58 |
+
app.launch()
|
59 |
+
|
60 |
|