I'm trying to learn how to serve up data in a Flask app and mysql database both served on pythonanywhere.
I've added a route to /test with a function which is designed to connect to an existing mysql database table, and then serve it up as an API with flask-restless's create_api.
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from flask.ext.cors import CORS
app.config["SQLALCHEMY_DATABASE_URI"] = SomeLoginDetails
app.config["SQLALCHEMY_POOL_RECYCLE"] = 299
db = SQLAlchemy(app)
@app.route("/test")
def create_api():
app.config['CORS_ALLOW_HEADERS'] = "Content-Type"
app.config['CORS_RESOURCES'] = {r"/api/*": {"origins": "*"}}
cors = CORS()
engine = create_engine(SomeLoginDetails)
Base = declarative_base()
Base.metadata.bind = engine
class Prices(Base):
__tablename__ = 'table'
col1 = Column(Integer, primary_key=True)
col2 = Column(Float)
col3 = Column(Float)
col4 = Column(Float)
Base.metadata.create_all()
manager = flask.ext.restless.APIManager(app,
flask_sqlalchemy_db = db)
manager.create_api(Prices, methods=['GET'], max_results_per_page =1000)
return "OK"
If I leave off the last Return "OK" I get an error along the lines of:
File "/home/username/.local/lib/python2.7/site-packages/flask/app.py", line 1566, in make_response
raise ValueError('View function did not return a response')
ValueError: View function did not return a response
However, if I add it, I receive no errors.
Why is that?
And where is the endpoint? I had tried
http://xxx-sitename-xxx.pythonanywhere.com/test/api
but I receive a Not Found page error.
the reason is clear, your view function should return a response, it can be a str, unicode, WSGI object or a tuple.
If you want to know what is endpoint in flask, you can see this question: What is an 'endpoint' in Flask?. And the reason why you get a NotFound
error, is that you didn't define the /test/api
route, you only have /test
route.