Best practices to combine REST API with web interface using FLASK

Viewed 3535

I currently build a web application using flask, sqlalchemy and jinja2.

To get a proper web interface, I build my views as follows:

@app.route('/mydata/', methods=['GET'])
@login_required
def mydata_list():
    # build data here...
    return render_template('mydata/index.html', data=data))

Now, if I need to build a REST API, I am supposed to terminate with

return jsonify(data)

So, how to handle this to avoid code duplication? Is it a good practice to add a ?api=True to my url, test it in my view, then return appropriate answer?

2 Answers
Related