我有一个烧瓶应用程序,我想用它来对postgresql数据库中的一个非常大的数据集进行一些数据可视化。
我有sqlalchemy,plotly,google maps设置,但我目前的问题是我想从我的数据库中获取一个相当大的数据集,并将其传递给一个模板,该模板将负责做一些不同的可视化。我只想在网站启动时加载一次数据,然后在导航其他页面时避免以后再运行它。
我在某处读到了session
,我试图实现这样的功能,但后来我得到了一个“不可序列化的错误”,我通过将我的对象解析为JSON来修复,但后来我“太大了”标题“错误。我觉得我有点不可能......
只是为了展示一些代码,这就是我目前正在搞乱的问题。
from sqlalchemy import BigInteger, Column, JSON, Text
from app import db
class Table910(db.Model):
__tablename__ = 'table_910'
id = db.Column(BigInteger, primary_key=True)
did = db.Column(Text)
timestamp = db.Column(BigInteger)
data = db.Column(JSON)
db_timestamp = db.Column(BigInteger)
def items(self):
return {'id': self.id, 'did': self.did, 'timestamp': self.timestamp, 'data': self.data, 'db_timestamp': self.db_timestamp}
def __repr__(self):
return '<1_Data %r, %r>' % (self.did, self.id)
from flask import render_template, session
from app import app, db, models
from datetime import datetime
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import numpy as np
import json
import plotly
@app.route('/')
@app.route('/index')
def index():
# fetching from the database (takes ~50 seconds to run)
rows = models.Table910.query \
.filter_by(did='357139052424715') \
.filter((models.Table910.timestamp > 1466846920000) & (models.Table910.timestamp < 1467017760000)) \
.order_by(models.Table910.timestamp.asc())
#
# managing and drawing data on plots
#
return render_template('index.html',
ids=ids,
graphJSON=graphJSON,
rows=rows,
routeCoordinates=routeCoordinates))
我可能有点过于模糊地解释了我的问题,因此要详细说明我想使用特定页面来加载数据,然后让另一页面负责绘制图形。我能想到的最好的程序是能够按下按钮来获取数据,一旦加载了数据,按下另一个按钮来绘制图形。我怎样才能实现这一目标?