Manasa1 commited on
Commit
a8a3333
·
verified ·
1 Parent(s): cef2a36

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -49
app.py CHANGED
@@ -1,52 +1,50 @@
1
- import gradio as gr
2
- my_theme = gr.Theme.from_hub("gradio/seafoam")
3
-
4
- import gradio as gr
5
  import yfinance as yf
6
  import pandas as pd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- # Function to fetch ESG data and save it to a CSV file
9
- def fetch_and_save_esg_data(ticker):
10
- # Fetch ESG data from Yahoo Finance
11
- stock = yf.Ticker(ticker)
12
- esg_data = stock.sustainability
13
-
14
- # If the data is available, save it to a CSV file
15
- if esg_data is not None:
16
- # Convert the ESG data to a pandas DataFrame
17
- esg_df = pd.DataFrame(esg_data)
18
-
19
- # Save it to a CSV file
20
- filename = f"{ticker}_esg_data.csv"
21
- esg_df.to_csv(filename)
22
-
23
- return f"ESG data for {ticker} saved to {filename}", filename
24
- else:
25
- return f"No ESG data available for {ticker}", None
26
-
27
- # List of companies (tickers) for dropdown selection
28
- company_list = ["AAPL", "GOOGL", "MSFT", "AMZN", "TSLA"]
29
-
30
- # Gradio interface with a dropdown for company selection
31
- def app_interface():
32
- with gr.Blocks() as app:
33
- # Dropdown to select company ticker
34
- company = gr.Dropdown(label="Select Company", choices=company_list, value="AAPL")
35
-
36
- # Button to trigger ESG data extraction
37
- fetch_button = gr.Button("Fetch ESG Data")
38
-
39
- # Output text and download link
40
- output_text = gr.Textbox(label="Output")
41
- download_link = gr.File(label="Download CSV")
42
-
43
- # Define the action when the button is clicked
44
- fetch_button.click(fn=fetch_and_save_esg_data,
45
- inputs=company,
46
- outputs=[output_text, download_link])
47
-
48
- return app
49
-
50
- # Launch the Gradio app
51
- app = app_interface()
52
- app.launch()
 
 
 
 
 
1
  import yfinance as yf
2
  import pandas as pd
3
+ import gradio as gr
4
+ import os
5
+
6
+ # Company dictionary mapping names to tickers
7
+ companies = {
8
+ "Apple": "AAPL",
9
+ "Microsoft": "MSFT",
10
+ "Google": "GOOGL",
11
+ "Amazon": "AMZN",
12
+ "Tesla": "TSLA"
13
+ }
14
+
15
+ def fetch_esg_data(company_name):
16
+ ticker = companies[company_name]
17
+ try:
18
+ stock = yf.Ticker(ticker)
19
+ esg_data = stock.sustainability
20
+ if esg_data is not None:
21
+ csv_file = f"{ticker}_esg_data.csv"
22
+ esg_data.to_csv(csv_file)
23
+ return f"ESG data for {company_name} saved to {csv_file}", csv_file
24
+ else:
25
+ return f"No ESG data available for {company_name}", None
26
+ except Exception as e:
27
+ return str(e), None
28
+
29
+ def main():
30
+ company_names = list(companies.keys())
31
+
32
+ def wrapper(company_name):
33
+ msg, csv_file = fetch_esg_data(company_name)
34
+ if csv_file and os.path.exists(csv_file):
35
+ return msg, gr.File(csv_file)
36
+ return msg, None
37
+
38
+ interface = gr.Interface(
39
+ fn=wrapper,
40
+ inputs=gr.inputs.Dropdown(choices=company_names, label="Select a company"),
41
+ outputs=["text", "file"],
42
+ title="Fetch ESG Data",
43
+ description="Select a company to fetch its ESG data and save it to a CSV file."
44
+ )
45
+
46
+ interface.launch()
47
+
48
+ if __name__ == "__main__":
49
+ main()
50