Allow only one flask request for a particular route
<p>I have a flask app which uses angularjs front end. I make the http request through $http service. As shown in the code below.</p>

<pre><code>$http.post('/updateGraph', $scope.graphingParameters).success(function(response) {
        $scope.graphingParameters.graph = response.graph;
        $scope.listUnits = JSON.parse(response.listUnits);
        $scope.myHTML = $sce.trustAsHtml($scope.graphingParameters.graph);
        $scope.showME = true;
    })
</code></pre>

<p>and the updateGraph function in flask is as follows.</p>

<pre><code>@app.route('/updateGraph', methods = ['POST'])
     def updateGraph():
           selectValues = request.json['selectValues']
           selectSelected = np.array(request.json['selectSelected']).tolist()
           if len(selectSelected) == 0:
                selectSelected = np.array([selectValues[1:3]]).tolist()
           fig, listUnits = plot_Stock_vs_Sales(selectSelected)
           graph = py_offline.plot(fig, include_plotlyjs=False, output_type='div', show_link=False)
           return json.dumps({ 'graph': graph, 'listUnits':listUnits.reset_index().to_json(orient='records')})
</code></pre>

<p>The problem is that suppose make the $http post from angular twice, The flask route is running twice. this is the code from the server.</p>

<blockquote>
  <p>
  Seconds:92
  127.0.0.1 - - [12/Sep/2016 09:46:35] "POST /updateGraph HTTP/1.1" 200 -
  Seconds:110
  127.0.0.1 - - [12/Sep/2016 09:47:02] "POST /updateGraph HTTP/1.1" 200 -</p>
</blockquote>

<p>I want to either make the $http post request to only allow one request or make flask run only one route per user. Is this possible through flask? if not what would be the best approach through angular?</p>

<p>From your description of the situation, this is something that would be better solved on the client side.</p>

<p>I would just set a flag (define it on a global or class level) if there's a request already ongoing for that task. For example:</p>

<pre><code>if (processing) {
    return;
}
processing = true;
$http.post('/updateGraph', $scope.graphingParameters).success(function(response) {
        $scope.graphingParameters.graph = response.graph;
        $scope.listUnits = JSON.parse(response.listUnits);
        $scope.myHTML = $sce.trustAsHtml($scope.graphingParameters.graph);
        $scope.showME = true;
        processing = false;
    })
</code></pre>

<p>This implementation can also be used to hide/disable the button or whatever triggers the request from the user so that they cannot trigger it while there's an ongoing request.</p>

<p>Note that I'm not very familiar with angular 1, so you might want to trigger that <code>processing = false</code> after the request and not only on <code>success</code>.</p>

jquery python angularjs ajax flask