'Flask Requests.Args works in local machine although not on Heroku
I have made a simple flask app that collects data from the arguments in URL and puts them into a python module which sends output in Json format which works fine on Local machine although gives error when deployed to Heroku . The error message says nothing other than TIMEOUT ERROR. Here is the heroku link for deployed app - https://shodhapi.herokuapp.com/ Here's Code
from nsepy import get_history
from datetime import date
import pandas as pd
from flask import Flask
from flask_caching import Cache
from flask import request
config = {
"CACHE_TYPE": "SimpleCache", # Flask-Caching related configs
"CACHE_DEFAULT_TIMEOUT": 300,
}
app = Flask(__name__)
# tell Flask to use the above defined config
app.config.from_mapping(config)
cache = Cache(app)
@app.route("/api")
@cache.cached(timeout=600)
def index():
ticker = request.args.get( 'ticker', None)
sy = int(request.args.get('sy', None))
sm = int(request.args.get('sm', None))
sd = int(request.args.get('sd', None))
ey = int(request.args.get('ey', None))
em = int(request.args.get('em', None))
ed = int(request.args.get('ed', None))
# 2022,1,31
results = get_history(symbol=ticker,start=date(sy,sm,sd),end=date(ey,em,ed))
results.reset_index(inplace=True)
results.set_index('Deliverable Volume', inplace=True)
results['Date'] = pd.to_datetime(results['Date'])
# convert dataframe to json
result_json = results.to_json(orient="records")
return result_json
if __name__ == "__main__":
app.run(debug=True)
Solution 1:[1]
I am not an everyday flask user, but have deployed some Nodejs servers to heroku. In Nodejs, although it uses a default port of 3000, you have to first check if the heroku environment provides an environment variable called PORT. Then you have to make your server listen at that port.(If no such variable is provided then you can listen on the default Nodejs port i.e., 3000).
app.listen(process.env.PORT || 3000);
In flask, you should probably do something like
import os
app.run(debug=True, port=int(os.environ.get('PORT', 5500)))
assuming 5500 is the default port for flask.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chaitanya Chavali |
