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?
datavariable might be different for HTML and JSON). You could use content negotiation, or a parameter in the URL, or a different URL altogether. There is no "right way" to separate these concerns.