-1

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!

romajc
  • 19
  • 1
  • 6

1 Answers1

0

If you want to add data through url, then you have to pass that data in url like :

@app.route('/stocks/add/<string:stock>', methods=['POST'])
def add_stocks(stock):
    sl.stock_list.append(stock)

@app.route('/stocks/remove/<string:stock>', methods=['POST'])
def remove_stocks(stock):
    sl.stock_list.pop(stock)

If you want to add data through any form then use form.validate_on_submit() method like shown here or something similar

om_prakash
  • 70
  • 9