-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTEST1.py
More file actions
60 lines (48 loc) · 1.97 KB
/
Copy pathTEST1.py
File metadata and controls
60 lines (48 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
51
52
53
54
55
56
57
58
59
60
import requests
import json
import sys
from flask import Flask, render_template, request
# Create the Flask application instance
app = Flask(__name__, template_folder='.')
# Your Alpha Vantage API Key
API_KEY = "GK60QK1ES9JP7NK1"
def get_api_data(function, symbol, api_key):
"""
Generic function to fetch data from the Alpha Vantage API.
"""
url = f'https://www.alphavantage.co/query?function={function}&symbol={symbol}&apikey={api_key}'
print(f"Fetching {function} data for '{symbol}'...")
try:
r = requests.get(url)
r.raise_for_status()
data = r.json()
if not data or 'Error Message' in data or 'Note' in data:
print(f"API Error for {function}: {data.get('Error Message', data.get('Note', 'No data found.'))}", file=sys.stderr)
return None
return data
except requests.exceptions.RequestException as e:
print(f"Network error: {e}", file=sys.stderr)
return None
except json.JSONDecodeError:
print("JSON decode error.", file=sys.stderr)
return None
# Define the main route for the web page
@app.route('/', methods=['GET', 'POST'])
def index():
overview_data = None
cash_flow_data = None
symbol = None
# Handle user input from the form
if request.method == 'POST':
symbol = request.form.get('ticker_symbol').upper()
if symbol:
overview_data = get_api_data('OVERVIEW', symbol, API_KEY)
# Only proceed to fetch cash flow if overview was successful
if overview_data:
cash_flow_data = get_api_data('CASH_FLOW', symbol, API_KEY)
return render_template('stock_dashboard_final.html',
overview=overview_data,
cash_flow=cash_flow_data,
symbol=symbol)
if __name__ == '__main__':
app.run(debug=True)