1

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.

2 Answers 2

3

Also, it seems really strange to be setting up your app and your database models and creating the API inside a view.

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, I'm still trying to get my head around python in web and databases. As a result I'm cobbling lots of code from different sources rather than properly thinking things through.
2

For the first question:

the reason is clear, your view function should return a response, it can be a str, unicode, WSGI object or a tuple.

For the second question:

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.

1 Comment

Just wanted to say thank you. I tweaked the code and it's pretty much working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.