I am trying to learn how to add/delete stocks in my Python API from a Flutter app I am making. I finally figured out how to retrieve the data, but I do not know how to add/delete from my stock list.
Here is the Yahoo Finance scraper and the stock list:
import pandas as pd
import yfinance as yf
import json
from flask import Flask, json
stock_list = stock_list = ['AAPL', 'AMD', 'TSLA']
tnx = yf.Ticker('^TNX')
tenYr = round(tnx.info['previousClose'], 2)
# print('10yr Note:', str(tenYr) + '%')
myJson = {}
for stock in stock_list:
info = yf.Ticker(stock).info
symbol = stock
price = info.get('previousClose')
tEps = info.get('trailingEps')
fEps = info.get('forwardEps')
tRatio = (tEps / (tenYr * 0.01)) / (price * 4)
fRatio = (fEps / (tenYr * 0.01)) / (price * 4)
myJson[symbol] = {
"price": price,
"tEps": tEps,
"fEps": fEps,
"tRatio": tRatio,
"fRatio": fRatio
}
# print(json.dumps(myJson))
Here is the Flask API:
from flask import Flask, request, jsonify
import stock_list as sl
app = Flask(__name__)
@app.route('/stocks', methods=['GET'])
def get_stocks():
# d = {}
# d['Query'] = str(request.args['Query'])
return jsonify(sl.myJson)
@app.route('/stocks/add', methods=['POST'])
def add_stocks():
return null
@app.route('/stocks/remove', methods=['POST'])
def remove_stocks():
return null
if __name__ == '__main__':
app.run()
How can I pass a new stock, say GOOGL, into the stock list, and also remove it? Thank you for the help!