2017-05-05 32 views
1

我使用的燒瓶中,SQLAlchemy的查詢的結果繪製的matplotlib數據被用來提取數據的列。我有一些工作代碼,但它不夠優雅而且效率低下。我希望能夠從一個單一的查詢中獲取數據的多個列中,我可以在matplotlib使用的一種形式。我是Python的新手(使用v3.4),而且我還沒有真正「獲得」它。目前我正在爲每一列數據查詢數據庫一次。如何從表單中的燒瓶中,SQLAlchemy的查詢,可與matplotlib

所有幫助感激地接受。

# views.py 

import matplotlib.pyplot as plt 
from .models import Sensor 

def DrawChart(): 
    # Draw a chart 
    x = Sensor.query.with_entities(Sensor.UnixTime).filter(Sensor.UnixTime >= config.startDateTime).filter(
     Sensor.UnixTime <= config.endDateTime).all() 
    y1 = Sensor.query.with_entities(Sensor.SupplyVoltage).filter(Sensor.UnixTime >= config.startDateTime).filter(
     Sensor.UnixTime <= config.endDateTime).all() 
    y2 = Sensor.query.with_entities(Sensor.TotalCurrent).filter(Sensor.UnixTime >= config.startDateTime).filter(
     Sensor.UnixTime <= config.endDateTime).all() 

    # Draw the chart 
    fig, ax = plt.subplots() 
    ax.plot(x, y1, 'k--') 
    ax.plot(x, y2, 'ro') 

# models.py 

from . import db 

db.Model.metadata.reflect(db.engine) 

class Sensor(db.Model): 
    """Sensor model links to sensor data table for displaying data and feeding MatPlotLib""" 
    __table__ = db.Model.metadata.tables['sensorReadings'] 

    def __repr__(self): 
     return 'Sensor model' 


# __INIT__.py 

from flask import Flask 
from flask_sslify import SSLify 
from flask_sqlalchemy import SQLAlchemy 
from flask_login import LoginManager 
from flask_bcrypt import Bcrypt 
import matplotlib 

matplotlib.use('Agg') # Stops errors when maplotlob tries to use X-windows backend 

app = Flask(__name__, instance_relative_config=True) # Use Flask instance folders to keep some settings out of GIT. 
app.config.from_object('config') # Main config file 
app.config.from_pyfile('config.py') # Instance config file 

sslify = SSLify(app) # Force Flask to use SSL i.e. HTTPS 
bcrypt = Bcrypt(app) # Initialize encryption for hashing passwords 
db = SQLAlchemy(app) # Initialize SQLite database 

回答

0

我不認爲你應該擔心有多個查詢的低效率。但是,你的確可以使它更優雅,採取這一問題,例如照顧:如果這些查詢斷裂的一個?

我建議你保留所有3個獨立的查詢,但信封裏面他們試圖/ except語句,因此,如果一個查詢失敗,你會得到知道哪一個。它將幫助您進行調試,並且還可以提示用戶提供適當的反饋,從而改善用戶體驗。

這裏有一個例子:

def DrawChart(): 
     # Draw a chart 
     try: 
      x = Sensor.query.with_entities(Sensor.UnixTime).filter(
       Sensor.UnixTime >= config.startDateTime 
       and Sensor.UnixTime <= config.endDateTime 
      ).all() 
     except: 
      # Throw some error 
      print "error1" 

     try: 
      y1 = Sensor.query.with_entities(Sensor.SupplyVoltage).filter(
       Sensor.UnixTime >= config.startDateTime 
       and Sensor.UnixTime <= config.endDateTime 
      ).all() 
     except: 
      print "error2" 

     try: 
      y2 = Sensor.query.with_entities(Sensor.TotalCurrent).filter(
       Sensor.UnixTime >= config.startDateTime 
       and Sensor.UnixTime <= config.endDateTime 
      ).all() 
     except: 
      print "error3" 

     # Draw the chart 
     fig, ax = plt.subplots() 
     ax.plot(x, y1, 'k--') 
     ax.plot(x, y2, 'ro') 
+0

感謝您有用的答案。我將執行錯誤處理。祝一切順利! – Martin