Search code examples
pythonpandasdataframeflaskflask-restful

Everytime restart required for flask to work


I have a flask app which will read some dataframes and displays some output in front end. I have two routes. One will accept user input and is send to second route. Second route make use of this input and process some dataframe and provides some output. Issue is, if go to user input page again and if I try submitting another input, it gives me page not working error. Is it because of any memory issue? If I restart my sever, then I repeat it once(every time I need to restart).

from flask import flash, redirect, render_template, url_for, request, jsonify
import math
import os
import glob
import pandas as pd
from . import fa
from database import connection
UPLOAD_DIRECTORY = './uploads'
if not os.path.exists(UPLOAD_DIRECTORY):
    os.makedirs(UPLOAD_DIRECTORY)

@fa.route('/fc', methods=['GET', 'POST'])
def index():
    return render_template('fc.html',flag=0)

@fa.route('/fc/s', methods=['GET', 'POST'])
def start():
    if request.method == 'POST':
            material_number = request.form['ma']
            path = UPLOAD_DIRECTORY
            extension = 'xlsx'
            os.chdir(path)
            result = [i for i in glob.glob('*.{}'.format(extension))]
            allFiles = result
            frame = pd.DataFrame()
            list_ = []
            for file_ in allFiles:
                df = pd.read_excel(file_)
                list_.append(df)
            frame = pd.concat(list_)
            frame = frame.reset_index(drop=True)
            df1 = frame[frame['Ma'].str.contains(ma,regex=True)]
            pr = df1['Pr'].unique().tolist() 
            pro = pd.read_excel(r'~pathhiddn~\dtrvsproj.xlsx')
            return render_template('fc.html',flag=1,ma=ma,prs=pr)
    return redirect(url_for('fa.index'))

Solution

  • os.chdir(path)
    

    This was causing the issue. I removed that and replaced df = pd.read_excel(file_) with df = pd.read_excel('./uploads/data/'+file_)