Manasa1 commited on
Commit
ac0650f
·
verified ·
1 Parent(s): 76ba4f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -20
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 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
 
@@ -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
+