@app.route('/view', methods=['POST', 'GET'])
data = get_current_data(query)
return render_template('graph.html', data=data)
Assuming a globally accessible function, just call it in the module with the data converted to json on the server with the tojson and safe filters.
It's a bit hard to follow the logic when you mix together server side templating and client side scripting like this. But sometimes you gotta do it.