Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
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 = {
|
@@ -11,45 +12,49 @@ company_dict = {
|
|
11 |
"Tesla": "TSLA"
|
12 |
}
|
13 |
|
14 |
-
# Function to fetch ESG data
|
15 |
-
def
|
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
|
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 |
-
#
|
29 |
-
|
30 |
-
|
|
|
|
|
31 |
|
32 |
-
return
|
33 |
else:
|
34 |
-
|
|
|
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
|
43 |
-
|
44 |
|
45 |
-
#
|
46 |
-
|
47 |
-
download_link = gr.File(label="Download CSV")
|
48 |
|
49 |
-
# Define the action when the button is clicked
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
|
54 |
return app
|
55 |
|
@@ -58,3 +63,4 @@ app = app_interface()
|
|
58 |
app.launch()
|
59 |
|
60 |
|
|
|
|
1 |
import gradio as gr
|
2 |
import yfinance as yf
|
3 |
import pandas as pd
|
4 |
+
import random
|
5 |
|
6 |
# Mapping company names to their ticker symbols
|
7 |
company_dict = {
|
|
|
12 |
"Tesla": "TSLA"
|
13 |
}
|
14 |
|
15 |
+
# Function to fetch ESG data for the selected company
|
16 |
+
def fetch_esg_data(company_name):
|
17 |
# Get the ticker symbol from the company name
|
18 |
ticker = company_dict[company_name]
|
19 |
|
20 |
# Fetch ESG data from Yahoo Finance
|
21 |
stock = yf.Ticker(ticker)
|
22 |
esg_data = stock.sustainability
|
23 |
+
|
24 |
+
# If ESG data is available, process it into a DataFrame
|
25 |
if esg_data is not None:
|
|
|
26 |
esg_df = pd.DataFrame(esg_data)
|
27 |
+
|
28 |
+
# Extract only the relevant ESG scores and convert to a DataFrame
|
29 |
+
esg_scores = esg_df.loc[["environmentScore", "socialScore", "governanceScore"], :].dropna().astype(float)
|
30 |
|
31 |
+
# Prepare a DataFrame for plotting
|
32 |
+
plot_df = pd.DataFrame({
|
33 |
+
"ESG Category": ["Environment", "Social", "Governance"],
|
34 |
+
"Score": esg_scores.squeeze().values
|
35 |
+
})
|
36 |
|
37 |
+
return plot_df
|
38 |
else:
|
39 |
+
# Return an empty DataFrame if no data is available
|
40 |
+
return pd.DataFrame()
|
41 |
|
42 |
+
# Gradio interface with a dropdown for company selection and line plot visualization
|
43 |
def app_interface():
|
44 |
with gr.Blocks() as app:
|
45 |
# Dropdown to select company name
|
46 |
company = gr.Dropdown(label="Select Company", choices=list(company_dict.keys()), value="Apple")
|
47 |
|
48 |
+
# Button to fetch and plot ESG data
|
49 |
+
plot_button = gr.Button("Generate ESG Plot")
|
50 |
|
51 |
+
# LinePlot component for displaying the ESG data
|
52 |
+
plot_output = gr.LinePlot(label="ESG Scores Plot", x="ESG Category", y="Score", overlay_point=True)
|
|
|
53 |
|
54 |
+
# Define the action when the "Generate ESG Plot" button is clicked
|
55 |
+
plot_button.click(fn=fetch_esg_data,
|
56 |
+
inputs=company,
|
57 |
+
outputs=plot_output)
|
58 |
|
59 |
return app
|
60 |
|
|
|
63 |
app.launch()
|
64 |
|
65 |
|
66 |
+
|