I need this data (NIO Inc. NIO 21.05 -0.29257 77123545 68510787 99146121216 ) as an output of my flask web app in a Json format.
my expecting output image is given as a link - also required data's are in yellow boxed
(Or)
You can guide me how to get these below mentioned output as an flask web API format. Also, fine for me to proceed next step.
import yfinance as yf
stocks = yf.Ticker("NIO")
a = stocks.info['shortName']
b = stocks.info['symbol']
c = stocks.info['currentPrice']
d = stocks.info['profitMargins']
e = stocks.info['volume']
f = stocks.info['averageVolume']
g = stocks.info['marketCap']
#h = stocks.info['trailingPE']
print(a, b, c, d, e, f, g)
Output - NIO Inc. NIO 21.05 -0.29257 77123545 68510787 99146121216
But, i actually get an complete info as a json format, need any one guidance.
app.py
# importing Flask and other modules
from flask import Flask, request, render_template
import yfinance as yf
# Flask constructor
app = Flask(__name__)
@app.route('/info/', methods =["GET", "POST"])
def display_quote():
if request.method == "POST":
# getting input text in HTML form
text = request.form['text']
symbol = request.args.get('symbol', text)
quote = yf.Ticker(symbol)
return quote.info
return render_template("form.html")
if __name__=='__main__':
app.run()
templates/form.html
<form method="POST">
<input name="text">
<input type="submit">
</form>
CodePudding user response:
You need just to create a dict with your information you want and return that in the flask view
quote = yf.Ticker(symbol)
return {
"a": quote.info['shortName'],
"b": quote.info['symbol'],
"c": quote.info['currentPrice'],
"d": quote.info['profitMargins'],
"e": quote.info['volume'],
"f": quote.info['averageVolume'],
"g": quote.info['marketCap'],
}
This gives you a json repsonse on the flask route