2017-01-02 134 views
0

我創建了一個可以從MySQL獲取數據的API。它在定義stockList時工作。但是,在現實世界中,我需要從Ionic應用程序獲取它,並且stockList由個人用戶定義。燒瓶Python:接受離子應用程序進入燒瓶的數據

簡單的說stockList =[]不起作用。 目前flask_app.py是如下:

from flask import Flask,jsonify,abort,make_response,request 
import MySQLdb 
import MySQLdb.cursors 

app = Flask(__name__) 
@app.route('/KLSEwatch', methods=['GET']) 
def KLSEwatch(): 
    db = MySQLdb.connect(host='vinvin.mysql.pythonanywhere-services.com',user='vinvin',passwd='xxx',db='vinukdb$default',cursorclass=MySQLdb.cursors.DictCursor) 
    curs = db.cursor() 
    stockList = ['SHELL','GENM'] 
    placeholders = ','.join(['%s'] * len(stockList)) 
    query = 'SELECT * FROM KLSE WHERE Stock IN ({})'.format(placeholders) 
    curs.execute(query,tuple(stockList)) 
    f = curs.fetchall() 
    return jsonify({'Stock': f}) 

我拿什麼取代stockList,因爲它必須得到用戶的數據,這是由離子的應用程序。該數據可以是字符串或一個4位數字

下面是離子應用在watchlistCtrl.js代碼

//setting get counter-number of get requests- 
    var getCounter = 0; 
    for (var market in watchListQuery) { 
     if(watchListQuery[market].length>0){ 
     getCounter += 1; 
     } 
    } 
    if(getCounter == 0) 
     $ionicLoading.hide(); 


    $scope.watchedStocks = []; 
    for (var market in watchListQuery) { 
     if(watchListQuery[market].length>0){ 
     var queryString = watchListQuery[market].toString().replace(/,/g, "','"); 

     $webServicesFactory.get($marketProvider[market].queryURL+"/watchlist_query", {AnonymousToken: $marketProvider[market].token}, {parameters:{stockList:queryString}}).then(
      function success(data) { 
      getCounter -=1 ; 

      $scope.watchedStocks = $scope.watchedStocks.concat(data); 
      if(getCounter <= 0) 
       $ionicLoading.hide(); 
      }, 
      function error() { 
      $ionicLoading.hide(); 
      } 
     ); 
     } 
    }//end of for each loop 

回答

0

你沒告訴我們你的任何離子的代碼,但這裏有一個簡單的例子從您的Ionic應用程序獲取輸入並將其提交給Flask。首先,一些HTML針對前端(我只使用角度,因爲這是這裏的共同的主題 - 離子其餘不相關的這個問題):

<!-- templates/home.html --> 
<!doctype html> 
<html lang="en"> 
<head> 
    <title>Ionic/Flask</title> 
</head> 
<body> 
<div ng-app="app"> 
    <div ng-controller="MyCtrl"> 
    <p>Enter a comma-separated string value, like "BAC,XYZ"</p> 
    <input type="text" ng-model="stockList"> 
    <button ng-click="submit()">Submit</button> 
    </div> 
</div> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.14/angular.js"></script> 
<script> 
    angular.module('app', []) 
    .controller('MyCtrl', function ($http, $log, $scope) { 
     $scope.stockList = ''; 
     $scope.submit = function() { 
     $http.get('/KLSEwatch', {params: {stockList: $scope.stockList}}).then(function (result) { 
      $log.log('This is the query to execute: ',result.data) 
     }) 
     }; 
    }) 
</script> 
</body> 
</html> 

然後這裏是修改後的版本你的瓶的應用程序,以證明這將產生正確的查詢:

# app.py 
from flask import Flask,jsonify,abort,make_response,request, render_template 
import MySQLdb 
import MySQLdb.cursors 

app = Flask(__name__) 
app.config['DEBUG'] = True 

@app.route('/') 
def home(): 
    return render_template('home.html') 

@app.route('/KLSEwatch', methods=['GET']) 
def KLSEwatch(): 
    stockList = request.args['stockList'].split(',') 
    placeholders = ','.join(['%s'] * len(stockList)) 
    query = 'SELECT * FROM KLSE WHERE Stock IN ({})'.format(placeholders) 
    print('This is the query: %s' % (query % tuple(stockList))) 
    return query % tuple(stockList) 

app.run() 

所有你需要做的是運行應用程序,輸入一個字符串值到輸入字段&提交,然後檢查結果在瀏覽器控制檯日誌或Flask應用程序的輸出中。

+0

什麼是'@ pp.route('/')'哪個'返回render_template('home.html')'? – vindex

+0

我添加它在watchlistCtrl.js中的Ionic代碼部分 – vindex