Issue
I have this code and whether I return jsonify(quote)
or just return quote
it still ends up as an escaped string on the Javascript front end. json.dumps()
doesn't seem to do anything either. I'm a js dev... dunno why such a simple thing is so involved in Python. What is the solution for this?
@app.route('/dev/api/stock/price/<symbol>', methods=['GET'])
@cross_origin(origin='*')
def get_stock_price(symbol):
quote = si.get_quote_table(symbol)
quote['price'] = si.get_live_price(symbol)
print(quote)
return quote
The print output of the quote is
{'1y Target Est': 700.3, '52 Week Range': '379.11 - 900.40', 'Ask': '849.30 x 900', 'Avg. Volume': 18457193.0, 'Beta (5Y Monthly)': 1.89, 'Bid': '849.00 x 800', "Day's Range": '822.35 - 843.21', 'EPS (TTM)': 1.9, 'Earnings Date': 'Oct 20, 2021', 'Ex-Dividend Date': nan, 'Forward Dividend & Yield': 'N/A (N/A)', 'Market Cap': '844.522B', 'Open': 823.74, 'PE Ratio (TTM)': 444.4, 'Previous Close': 818.32, 'Quote Price': 843.030029296875, 'Volume': 18924617.0, 'price': 843.030029296875}
On clientside it is received like this
data: "{\"1y Target Est\": 700.3, \"52 Week Range\": \"379.11 - 900.40\", \"Ask\": \"849.30 x 900\", \"Avg. Volume\": 18457193.0, \"Beta (5Y Monthly)\": 1.89, \"Bid\": \"849.00 x 800\", \"Day's Range\": \"822.35 - 843.21\", \"EPS (TTM)\": 1.9, \"Earnings Date\": \"Oct 20, 2021\", \"Ex-Dividend Date\": NaN, \"Forward Dividend & Yield\": \"N/A (N/A)\", \"Market Cap\": \"844.522B\", \"Open\": 823.74, \"PE Ratio (TTM)\": 444.4, \"Previous Close\": 818.32, \"Quote Price\": 843.030029296875, \"Volume\": 18924617.0, \"price\": 843.030029296875}"
Solution
So the issue was I forgot I had an axios transform happening on the clientside which I believe was the primary driver of the issue. Also some data formatting was added to the backend.
try {
// const response = await api.post(path, obj, { transformResponse: [] });
const response = await api.post(path, obj); // Correct
return response;
}
the backend was changed to this
@app.route("/dev/api/stock/price/<symbol>", methods=["GET"])
@cross_origin(origin="*")
def get_stock_price(symbol):
quote = si.get_quote_table(symbol)
obj = {}
for key in quote:
_key = key.replace(" ", "_")
obj[_key] = (
"" if type(quote[key]) is float and math.isnan(quote[key]) else quote[key]
)
return jsonify(obj)
Answered By - Michael Paccione
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.