def download(guid=None): form = DownloadForm() if request.method == 'POST' and form.validate(): s = hp.find_device(form.guid.data) if s is None: s = hp.find_sensor(form.guid.data) if s is None: flash("ID not found") else: try: df = s.get_data(head=pd.Timestamp(form.start.data), tail=pd.Timestamp(form.end.data), resample=form.resample.data) except: flash( "Error connecting to the data storage, please try again later" ) else: filename = '{}.csv'.format(s.key) filepath = safe_join(download_path, filename) df.to_csv(filepath, encoding='utf-8') del df gc.collect() return send_file(filepath, as_attachment=True) if guid is not None: form.guid.data = guid return render_template('download.html', form=form)
def download(guid=None): form = DownloadForm() if request.method == 'POST' and form.validate(): s = hp.find_device(form.guid.data) if s is None: s = hp.find_sensor(form.guid.data) if s is None: flash("ID not found") else: try: # We need to connect and disconnect with tmpo # to make sure the website doesn't lock access to the sqlite hp.init_tmpo() tmpos = hp.get_tmpos() output = StringIO() df = s.get_data( head=pd.Timestamp(form.start.data), tail=pd.Timestamp(form.end.data), resample=form.resample.data ) tmpos.dbcon.close() except: # This will happen if another process is currently using the tmpo flash("Error connecting to the data storage, please try again later") else: df.to_csv(output, encoding='utf-8') output.seek(0) return send_file( output, mimetype="text/csv", as_attachment=True, attachment_filename='{}.csv'.format(s.key) ) if guid is not None: form.guid.data = guid return render_template( 'download.html', form=form )
def download(guid=None): form = DownloadForm() if request.method == 'POST' and form.validate(): s = hp.find_device(form.guid.data) if s is None: s = hp.find_sensor(form.guid.data) if s is None: flash("ID not found") else: try: # We need to connect and disconnect with tmpo # to make sure the website doesn't lock access to the sqlite hp.init_tmpo() tmpos = hp.get_tmpos() output = StringIO() df = s.get_data(head=pd.Timestamp(form.start.data), tail=pd.Timestamp(form.end.data), resample=form.resample.data) tmpos.dbcon.close() except: # This will happen if another process is currently using the tmpo flash( "Error connecting to the data storage, please try again later" ) else: df.to_csv(output, encoding='utf-8') output.seek(0) return send_file(output, mimetype="text/csv", as_attachment=True, attachment_filename='{}.csv'.format(s.key)) if guid is not None: form.guid.data = guid return render_template('download.html', form=form)
def download(guid=None): form = DownloadForm() if request.method == 'POST' and form.validate(): s = hp.find_device(form.guid.data) if s is None: s = hp.find_sensor(form.guid.data) if s is None: flash("ID not found") else: try: df = s.get_data( head=pd.Timestamp(form.start.data), tail=pd.Timestamp(form.end.data), resample=form.resample.data ) except: flash("Error connecting to the data storage, please try again later") else: filename = '{}.csv'.format(s.key) filepath = safe_join("static/downloads", filename) df.to_csv(filepath, encoding='utf-8') del df gc.collect() return send_file( filepath, as_attachment=True ) if guid is not None: form.guid.data = guid return render_template( 'download.html', form=form )