0

I have this python code to deploy to Heroku:

import flask
import pandas as pd


def create_app():
    app = flask.Flask(__name__)

    @app.route('/', methods=['GET', 'POST'])
    def index():
        """
        Index page view handler.
        :return: rendered index.html template
        """
        return flask.render_template('index.html')

    @app.route('/data', methods=['GET', 'POST'])
    def data():
        """
        Data view handler
        :return: JSON object of the data CSV file
        """
        data = pd.read_csv('task_data.csv')

        context = {
            'sensor_data': data.to_dict(orient='list')
        }
        return flask.jsonify(context)

    return app


if __name__ == "__main__":
    app = create_app()
    app.config['TEMPLATES_AUTO_RELOAD'] = True
    # serve the application on port 7410
    app.run(host='0.0.0.0', port=7410)

Procfile

web: python main.py

After I uploaded the files I get now this errors in log view:

2020-02-26T14:08:32.485820+00:00 heroku[web.1]: State changed from crashed to starting

2020-02-26T14:08:38.283732+00:00 heroku[web.1]: Starting process with command python main.py

2020-02-26T14:08:41.705572+00:00 app[web.1]: * Serving Flask app "main" (lazy loading)

2020-02-26T14:08:41.705637+00:00 app[web.1]: * Environment: production

2020-02-26T14:08:41.705644+00:00 app[web.1]: WARNING: This is a development server. Do not use it in a production deployment.

2020-02-26T14:08:41.705705+00:00 app[web.1]: Use a production WSGI server instead.

2020-02-26T14:08:41.705763+00:00 app[web.1]: * Debug mode: off

2020-02-26T14:08:41.722252+00:00 app[web.1]: * Running on http://0.0.0.0:7410/ (Press CTRL+C to quit)

2020-02-26T14:09:38.524175+00:00 heroku[web.1]: State changed from starting to crashed

2020-02-26T14:09:38.453425+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

2020-02-26T14:09:38.453485+00:00 heroku[web.1]: Stopping process with SIGKILL

2020-02-26T14:09:38.509347+00:00 heroku[web.1]: Process exited with status 137

Please advise how to fix this error ?

buran
  • 13,682
  • 10
  • 36
  • 61
zac
  • 4,495
  • 15
  • 62
  • 127
  • Does this answer your question? [What port to use on heroku python app](https://stackoverflow.com/questions/27899666/what-port-to-use-on-heroku-python-app) – ginkul Feb 26 '20 at 14:32

1 Answers1

1

As I know you should take port from environment variables, so it would be:

port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)

which can be found here. Also there was a similar question. Hope this will make it clear.

ginkul
  • 1,026
  • 1
  • 5
  • 17
  • I tried to access the url when I used in the code `localhost` but it did not work do I must use the heroku url or I can use localhost ? – zac Feb 26 '20 at 15:04