Pandas数据框一旦部署到heroku(Django项目)后就无法按预期方式运行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Pandas数据框一旦部署到heroku(Django项目)后就无法按预期方式运行相关的知识,希望对你有一定的参考价值。
我创建了一个程序,该程序可以进行数据输入并返回一个熊猫表(信息存储在views.py文件顶部的列表中,称为JOB_INFO-我将在下面附加此列表),结果如下:已下载。我还在具有数据输入表单的html页面上具有表呈现-该表呈现在表单下方,并随着每个用户输入(表单提交)进行更新(追加)。这在我的本地计算机上的开发中非常完美,但是当成功将其部署到heroku时,它就会开始起作用,并且行为异常。
示例包括:不更新表格-表单似乎已提交,但表格未更新。如果我继续尝试,它会突然起作用。但随后,我将尝试另一种,而上一个将消失,而表中将仅显示最新的一个。似乎太随意了。不清空列表-我有一个按钮可以清除表格,但这也不起作用。如果我多次单击按钮,它可能会突然工作。但是然后随机地,一些信息会再次出现在表中,我认为它已被清除!当我以为我已清除文件时,下载文件中的信息-这是不言自明的。
简而言之,将其称为随机可能是描述它的最佳方法。它只是不做我想做的事情和对它的期望。
views.py
from django.shortcuts import render, redirect, get_object_or_404, reverse, HttpResponse
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from .models import Data
from .forms import DataForm, SearchAndUpdateForm
from io import BytesIO
import pandas as pd
import xlsxwriter
JOB_INFO = {
"Plentific Job Number": [],
"WorkOrder Number": [],
"Invoice Number Found": [],
"Payments": [],
"Job Number Exists": [],
}
def data_entry(request):
"""
Creates new instance of data entry form and renders it on html page
along with a report table, which updates with each data entry input
"""
form = SearchAndUpdateForm()
df = pd.DataFrame(JOB_INFO,
columns=[
"Plentific Job Number",
"WorkOrder Number",
"Invoice Number Found",
"Payments",
"Job Number Exists",
])
if not df.empty:
df_html = df.to_html(classes="table table-striped table-hover")
return render(request, "data_entry.html", {
"form": form, "df_html": df_html})
else:
return render(request, "data_entry.html", {
"form": form})
def export_job_info_report(request):
"""
Exports the job_info list as an Excel spreadsheet
and saves it to user's local directory
"""
df = pd.DataFrame(JOB_INFO)
output = BytesIO()
writer = pd.ExcelWriter(output, engine='xlsxwriter')
df.to_excel(writer, sheet_name='report', index=False)
writer.save()
output.seek(0)
response = HttpResponse(output,
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = 'attachment; filename=%s.xlsx' % 'Download'
return response
def clear_report_info(request):
"""Empties the job_info dictionary"""
JOB_INFO["Plentific Job Number"].clear()
JOB_INFO["WorkOrder Number"].clear()
JOB_INFO["Invoice Number Found"].clear()
JOB_INFO["Payments"].clear()
JOB_INFO["Job Number Exists"].clear()
return redirect(reverse(data_entry))
html页面
{% extends "base.html" %}
{% block content %}
{% load crispy_forms_tags %}
<div class="jumbotron">
<h1>Data Entry Tool:</h1>
<form action="{% url 'search_update_feedback' %}" method="GET">
{{ form|crispy }}
<button class="btn btn-success" type="submit" data-toggle="tooltip" data-placement="right" title="Updates the database with the info provided above">
Update and Fetch Info
</button>
</form>
<br>
<form action="{% url 'export_job_info_report' %}" method="GET">
<button class="btn btn-success" type="submit" data-toggle="tooltip" data-placement="right" title="Downloads the report below in .xlsx format">
Download Report
</button>
</form>
<br>
<form action="{% url 'clear_report_info' %}" method="GET">
<button class="btn btn-success" type="submit" data-toggle="tooltip" data-placement="right" title="Warning! You will lose all of the data below (does not affect database data)">
Clear Report - hover over me!
</button>
</form>
</div>
<hr>
{{ df_html | safe }}
{% endblock %}
网址
urlpatterns = [
path("", data_entry, name="data_entry"),
path("export_job_info_report/", export_job_info_report, name="export_job_info_report"),
path("clear_report_info/", clear_report_info, name="clear_report_info"),
]
当然,我省去了很多功能来节省阅读时间,但是如果可以/应该添加其他任何内容以帮助理解该问题,请告诉我。任何帮助将不胜感激。
我设法通过用具有相同字段名称的新Django模型替换JOB_INFO列表来解决此问题,如下所示:
from django.db import models
class JobInfoReport(models.Model):
"""The following are prefixed with 'r' to represent report"""
r_plentific_job_number = models.CharField(max_length=7)
r_wo_number = models.CharField(max_length=6)
r_invoice_number_found = models.CharField(max_length=5)
r_payments = models.CharField(max_length=100)
r_job_number_exists = models.CharField(max_length=50)
def __str__(self):
return self.r_plentific_job_number
以上是关于Pandas数据框一旦部署到heroku(Django项目)后就无法按预期方式运行的主要内容,如果未能解决你的问题,请参考以下文章
在heroku上部署后无法读取mongo数据库,返回503错误,但它在本地工作