在系统目录上保存文件时,在烧瓶中将错误显示为“内部服务器错误”

时间:2019-11-05 09:56:33

标签: python flask file-handling

我正在创建一个Web应用程序,以从雅典娜下载excel数据,并使用flask框架将其保存在用户系统上。 当excel数据下载到根目录时,代码工作正常,现在的问题是,当我使用asksaveasfile()方法为用户选择文件夹提供一个选项,并在其本地目录上下载excel时,出现错误,为“内部服务器错误服务器遇到内部错误,无法完成您的请求。服务器超载或应用程序中有错误。”

from flask import Flask, render_template, request
from pyathena import connect
import os 
from os.path import join, dirname, realpath
import pandas as pd
import openpyxl
import matplotlib
from openpyxl import Workbook
from IPython import get_ipython
from datetime import datetime
from flask import send_file
from openpyxl.styles import Color, PatternFill, Font, Border
from openpyxl.styles import colors
from openpyxl.cell import Cell
import getpass
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from flask import request, redirect, url_for,flash
from werkzeug.utils import secure_filename
import os




app = Flask(__name__)


app = Flask(__name__)

filename = ''

def colorExcle(loc):
    wb = Workbook()
    location = loc


    redFill = PatternFill(start_color='99e6ff',
                    end_color='99e6ff',
                    fill_type='solid')

    ws = wb.active
    wb = openpyxl.load_workbook(location)

    sheet = wb['Sheet1']
    sheet.cell(row=1, column=11).value = 'UserName'
    sheet.cell(row=1, column=12).value = 'Result'
    sheet.cell(row=1, column=13).value = 'Coding Time in min'
    sheet.cell(row=1, column=14).value = 'QC1 By'
    sheet.cell(row=1, column=15).value = 'QC1 Result'
    sheet.cell(row=1, column=16).value = 'QC1 Time in min'
    sheet.cell(row=1, column=17).value = 'Comments'
    sheet.cell(row=1, column=18).value = 'KM'
    sheet.cell(row=1, column=19).value = 'Rework'
    sheet.cell(row=1, column=20).value = 'QC2 Time in min'
    sheet.cell(row=1, column=21).value = 'QC2 Results'



    sheet.cell(row=1,column=1).fill = redFill
    sheet.cell(row=1,column=2).fill = redFill
    sheet.cell(row=1,column=3).fill = redFill
    sheet.cell(row=1,column=4).fill = redFill
    sheet.cell(row=1,column=5).fill = redFill
    sheet.cell(row=1,column=6).fill = redFill
    sheet.cell(row=1,column=7).fill = redFill
    sheet.cell(row=1,column=8).fill = redFill
    sheet.cell(row=1,column=9).fill = redFill
    sheet.cell(row=1,column=10).fill = redFill
    sheet.cell(row=1,column=11).fill = redFill
    sheet.cell(row=1,column=12).fill = redFill
    sheet.cell(row=1,column=13).fill = redFill
    sheet.cell(row=1,column=14).fill = redFill
    sheet.cell(row=1,column=15).fill = redFill
    sheet.cell(row=1,column=16).fill = redFill
    sheet.cell(row=1,column=17).fill = redFill
    sheet.cell(row=1,column=18).fill = redFill
    sheet.cell(row=1,column=19).fill = redFill

    sheet.cell(row=1,column=20).fill = redFill
    sheet.cell(row=1,column=21).fill = redFill
    wb.save(location)   

    return render_template('index.html') 





@app.route('/', methods = ['GET', 'POST'])

def index():

    if "HAD" in request.form:

        projectTag = request.form.get('projectTag')
        region = request.form.get('region')

        if request.method == "POST":
            region = request.form.get("region", None)
            if region!=None:

                conn = connect(aws_access_key_id='AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
                    aws_secret_access_key='VVVVVVVVVVVVVVVVVVVVVVV',
                    s3_staging_dir='s3://CVD-analytics-athena-stating-dir/',
                    region_name='us-west-1')

                cursor = conn.cursor()
                now = datetime.today().strftime("%d-%m-%y")
                excelName=region.upper()+"_"+projectTag+"_"+now
                wb = Workbook()
                ws = wb.active

                data1 = r"C:\\Users\\"+ getpass.getuser()+"\\Downloads\\"+excelName+'.xlsx'



                wb.save(data1)
                wb = openpyxl.load_workbook(data1)
                sheet = wb['Sheet']
                data =  pd.read_sql("SELECT * from table;",conn)
                data.to_excel(data1)

                filename = filedialog.asksaveasfile(mode='w')
                data2 = data.to_excel(data1)
                filename.write(data2)
                return colorExcle(data1)
                return render_template('index.html', projectTag,region)


    return render_template('index.html')


@app.route('/home', methods = ['GET', 'POST'])



@app.route('/about')
def about():
    return render_template('about.html')



if __name__ == "__main__":
    app.run()

python和flask非常新。 描述性代码可以为我提供更多帮助。

0 个答案:

没有答案