0

I've created a web application in which user can download reports from athena in .xlsx format.

When I am hosting application on my system users are able to access my flask's web page but when user downloads report the reports are getting saved in my system.

When I try to change location or some other method I get INTERNAL SERVER ERROR

'''

CGIHandler().run(app)


app = Flask(__name__)



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='xxxxxxxxxxxxxxxxxxxxxxxxx',
                    aws_secret_access_key='xxxxxxxxxxxxxxxxxxxxxxxxxxx',
                    s3_staging_dir='s3://xxxxxxxxxxxxxxxxxxxxxx/',
                    region_name='us-west-1')


                userName = getpass.getuser()

                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 geocord;",conn)
                data.to_excel(data1)

                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(host="192.168.0.103",port=80)

'''

I've also used : ''' path = "C:\Flask"

return send_file(path, as_attachment=True)

''' Getting error as : cmd: PermissionError: [Errno 13] Permission denied web page: Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

Descriptive code will help me more since I am new to flask framework.

  • you need to change this `wb.save(location)` to location of user system not your system – sahasrara62 Nov 09 '19 at 19:13
  • Yep, I tried and got INTERNAL SERVER ERROR –  Nov 09 '19 at 19:14
  • create a new api which return the file from your system / temp location to user – sahasrara62 Nov 09 '19 at 19:16
  • 1
    Possible duplicate of [Flask Download a File](https://stackoverflow.com/questions/24577349/flask-download-a-file) – Trenton McKinney Nov 09 '19 at 19:16
  • @TrentonMcKinney- When tried this( return send_file(path, as_attachment=True)) Getting error as Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application. and thank you soo much for quick replies guys –  Nov 09 '19 at 19:25
  • @VishalUpadhyay did you try adding the `as_attachment=True` flag to `send_file`? – Dan Nov 09 '19 at 19:42
  • Also instead of setting all those cells individually to the same state, surely you can just use a range and do it all in one go. – Dan Nov 09 '19 at 19:44
  • Yes @Dan I tried that and got same 500 INTERNAL SERVER ERROR and on cmd I'm getting error as **PermissionError: [Errno 13] Permission denied ** –  Nov 09 '19 at 19:49

0 Answers0