Created
November 9, 2019 23:24
-
-
Save kylelix7/0dc0998feec6ae948aa5402664b1ea2b to your computer and use it in GitHub Desktop.
app.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pandas as pd | |
from flask import Flask | |
from flask import abort | |
from flask import request | |
from flask_cors import CORS | |
from flask import jsonify, make_response | |
app = Flask(__name__, template_folder="templates") | |
@app.route('/price') | |
def portfolio(): | |
ticker = request.args.get('ticker') | |
if ticker is None: | |
app.logger.info('tickers not set') | |
abort(400) | |
path = '{}/data/{}.csv'.format(os.path.dirname(os.path.realpath(__file__)), ticker) | |
df = pd.read_csv(path, index_col='Date', parse_dates=True, usecols=['Date', 'Close'], | |
na_values=['nan']) | |
port_series = [] | |
for index, row in df.iterrows(): | |
port_series.append({'name': index.strftime("%Y-%m-%d"), 'value': df.loc[index, 'Close']}) | |
resp = make_response(jsonify([{'name': ticker, 'series': port_series}]), 200) | |
resp.headers['Access-Control-Allow-Origin'] = '*' | |
return resp | |
if __name__ == '__main__': | |
CORS(app) | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment