Как прочитать загруженный файл csv на heroku?
Загружаю файл на heroku - прочитать не могу. Где сохраняется файл при загрузке POST c формы? Прочитала, что есть папка tmp. Как прочитать?
def index():
if request.method == "POST":
t = 0
file = request.files["file"]
if bool(file.filename):
file_bytes = file.read(MAX_FILE_SIZE)
args["file_size_error"] = len(file_bytes) == MAX_FILE_SIZE
args["method"] = "POST"
try:
t1 = file.filename
t2 = "tmp/"
t3 = t1 + t2
dataset = pd.read_csv(t3)
t=0
except IOError as e:
t = file.filename
else:
t = 1
args["t"] = t
return render_template("main.html", args=args)
<body>
{% if args["method"] == "POST" %}
{% if args["file_size_error"] %}
<h1>Размер файла превышает 1мб.!</h1>
{% else %}
<h1>Файл успешно загружен.</h1>
{% endif %}
{% endif %}
<form action="/" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">Загрузить</button>
</form>
<div class="result" align="center">
<br> <p style="font-size:50px">Week predict:</p>
<p style="font-size:50px">{{ args }}</p>
</div>
</body>
Ответы (1 шт):
Автор решения: svil
→ Ссылка
if request.method == "POST":
t = 0
file = request.files["file"]
str_file_value = file.read().decode('utf-8')
file_t = str_file_value.splitlines()
csv_reader = csv.reader(file_t, delimiter=',')
file_data = [row for row in csv_reader][1:]
dataset = pd.DataFrame(file_data)
if bool(file.filename):
file_bytes = file.read(MAX_FILE_SIZE)
args["file_size_error"] = len(file_bytes) == MAX_FILE_SIZE
args["method"] = "POST"
args["t"] = prediction(dataset)
<!doctype html>
<html lang="en">
<style>
form {
margin: auto;
width: 35%;
}
.result {
margin: auto;
width: 35%;
border: 1px solid #ccc;
}
</style>
<head>
<meta charset="UTF-8">
<title>Predicts</title>
</head>
<body>
{% if args["method"] == "POST" %}
{% if args["file_size_error"] %}
<h1>Размер файла превышает 1мб.!</h1>
{% else %}
<h1>Файл успешно загружен.</h1>
{% endif %}
{% endif %}
<form action="/" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">Загрузить</button>
</form>
<div class="result" align="center">
<br> <p style="font-size:50px">Week predict:</p>
<p style="font-size:50px">{{ args["t"] }}</p>
</div>
</body>
</html>