Home > OS >  Unable to provide multiple methods in same route using Flask [closed]
Unable to provide multiple methods in same route using Flask [closed]

Time:10-02

I need to give option to user that they can input text or a pdf file to give input for further processing. Both methods are working fine in separate Flask apps, but when I try to use same route as following.

@app.route("/", methods = ['POST', 'GET'])
def input_str():

    # code to get text from request.form
    return render_template('index.html',  input_txt = var)


def upload_file():

    # code to get text from pdf file
    return render_template('index.html',  input_txt = var)

only first method def input_str(): works while trying to upload file it gives error 400. I have tried to use different route methods but still only one method works since both are of type 'POST'. Can someone help me with how can I run both methods with same route? Thank you

EDIT 1: There is JavaScript and CSS added in HTML so here is gist having HTML code of it.

EDIT 2:

both function return a DataFrame generated by a question generation model by hugging face. Code for both functions is here

CodePudding user response:

Function decorators (the line starting with @) are per-function. In flask, you'd need to either combine those functions or use a dispatching function (shown below):

@app.route("/", methods = ['POST', 'GET'])
def home_dispatch():
    if request.method == 'POST':
        return upload_file()
    else:
        return input_str()

def input_str():

    # code to get text from request.form
    return render_template('index.html',  input_txt = var)

def upload_file():

    # code to get text from pdf file
    return render_template('index.html',  input_txt = var)

CodePudding user response:

You can call the function depending on which input has been submitted in the form.

index.html

    <form  method="POST" action="/" id="form">
        <input class="backup-button" type ="submit" name="text-inp" value="Text Input">
        <input class="restore-button" type ="submit" name="pdf-inp" value="PDF Input">
    </form>

app.py

@app.route('/', methods=['POST','GET'])
def home():
    if request.method == "POST":
            if request.form.get('text-inp'):
                gen_q = input_str()
            elif request.form.get('pdf-inp'):
                gen_q = upload_file()
    return render_template('index.html',  tables=[gen_q.to_html(classes='data')], titles=gen_q.columns.values)

Processing Functions

def input_str():
    global gen_q
    input_txt = request.form['text'] 
    clean_txt =  re.sub('[^a-zA-Z0-9 \n\.]', '', input_txt)
    gen_q = flask_qg.gen_qa(clean_txt)
    return gen_q
   
    
def upload_file():
    uploaded_file = request.files['file']
    if uploaded_file.filename != '':
        data = uploaded_file.read()
        doc = fitz.open(stream=data, filetype="pdf")
        all_text = ""
        for page in doc:
            all_text  = page.get_text("text")
    clean_txt =  re.sub('[^a-zA-Z0-9 \n\.]', '', all_text)
    gen_q = flask_qg.gen_qa(clean_txt)
    return gen_q
  • Related