-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
29 lines (25 loc) · 980 Bytes
/
app.py
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
from flask import Flask, request, jsonify
from flask_cors import CORS # Import CORS from flask_cors module
import yfinance as yf
app = Flask(__name__)
CORS(app) # Add this line to enable CORS for your Flask app
def get_hist_data(symbol):
stock = yf.Ticker(symbol)
hist = stock.history(period="1y")
# Reset index to get the Date as a column
hist.reset_index(inplace=True)
# Convert Date to string format to ensure JSON serializable
hist['Date'] = hist['Date'].dt.strftime('%Y-%m-%d')
return hist.to_json(orient='records')
@app.route('/get_historical_data', methods=['GET'])
def get_historical_data():
symbol = request.args.get('symbol')
if symbol is None:
return jsonify({"error": "Symbol parameter is missing"}), 400
try:
historical_data = get_hist_data(symbol)
return historical_data
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(debug=True)