Forums

need to reload everytime make changes in flask?

I am trying to make a restful API using flask. I am taking data from mysql. I notice I run flask_app.py, the changes will not take place. I need to reload the web app, in web tab. In this case, when I deploy it, where the database changes with schedule tasks, I need to reload it every time?

Also, every time I update mysql, I need to reload the web app also.

A very simple Flask Hello World app for you to get started with...

from flask import Flask,jsonify,abort,make_response
import MySQLdb
import MySQLdb.cursors

app = Flask(__name__)
db = MySQLdb.connect(host='vinus.mysql.pythonanywhere-services.com',user='vinus',passwd='pw',db='vinuspp$default',cursorclass=MySQLdb.cursors.DictCursor)

@app.route('/')
def hello_world():
    return 'Hello from vee!'

@app.route('/KLSE', methods=['GET'])
def KLSE():
    curs = db.cursor()
    try:
        curs.execute("SELECT * FROM KLSE")
        a = curs.fetchall()
    except Exception:
        return 'Error: unable to fetch items'
    #return "hihi"
    return jsonify({'Stock': a})

if __name__ == '__main__':
    app.run()

read that pythonanywhwre need to add if __name__ == '__main__':but unable to run.

 * Running on http://127.0.0.1:5000/
Traceback (most recent call last):
  File "/home/vinus/mysite/flask_app.py", line 49, in <module>
    app.run()
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 772, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 710, in run_simple
    inner()
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 692, in inner
    passthrough_errors, ssl_context).serve_forever()
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 486, in make_server
    passthrough_errors, ssl_context)
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 410, in __init__
    HTTPServer.__init__(self, (host, int(port)), handler)
  File "/usr/lib/python3.4/socketserver.py", line 430, in __init__
    self.server_bind()
  File "/usr/lib/python3.4/http/server.py", line 133, in server_bind
    socketserver.TCPServer.server_bind(self)
  File "/usr/lib/python3.4/socketserver.py", line 444, in server_bind
    self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use

You can't run servers in a console - they are not connected to the outside world. We have web apps for running web apps.

Hi Glenn, How should I run it after I completed the part in flask_app.py? Also, I have error with

if __name__ == '__main__':
    app.run()

Try setting up a flask webapp from the webapp tab. Basically we will automatically create a wsgi.py file which points to your flask_app.py and import your app variable. You can try creating a new webapp and then changing where the wsgi.py is pointing to.

the wsgi.py is like this, what I need to modify?

import sys

# add your project directory to the sys.path
project_home = u'/home/vinus/mysite'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# import flask app but need to call it "application" for WSGI to work
from flask_app import app as application

Do I really need the app.run() for PA?

no you don't need app.run()

you need to modify the project_home so that it points to wherever your project is (from which you canimport app from flask_app)

Check out this help page on setting us Flask -- it has a section on app.run which should make things clear (and if it doesn't clarify things, please do let us know!)