0

I am creating a Flask based web application. On my homepage, I take certain inputs from the user and use them in other routes for doing certain operations. I am currently using global but I know that's not a good way. I looked for Sessions in Flask but my web app doesn't register a user, so I don't know how session would work in that case. In a nutshell:

  • Webapp doesn't require user to register
  • A user select passes three lists of arguments coming through the form.
  • These three lists, list of floats, list of string and list of integers, have to be passed to other routes for processing information.

Is there any neat way to do it?

enterML
  • 2,110
  • 4
  • 26
  • 38

1 Answers1

0

You can pass the user inputs from home page through url parameters. i.e You can append all the parameters entered by the user as parameters in the receiver url and retrieve them back at the receiver url side. Please find below, a sample flow of the same:

from flask import Flask, request, redirect

@app.route("/homepage", methods=['GET', 'POST'])
def index():
    ##The following will be the parameters to embed to redirect url.
    ##I have hardcoded the user inputs for now. You can change this
    ##to your desired user input variables.
    userinput1 = 'Hi'
    userinput2 = 'Hello'

    redirect_url = '/you_were_redirected' + '?' + 'USERINPUT1=' + userinput1 + '&USERINPUT2=' + userinput2
    ##The above statement would yield the following value in redirect_url:
    ##redirect_url = '/you_were_redirected?USERINPUT1=Hi&USERINPUT2=Hello'

    return redirect(redirect_url)

@app.route("/you_were_redirected", methods=['GET', 'POST'])
def redirected():
    ##Now, userinput1 and userinput2 can be accessed here using the below statements in the redirected url
    userinput1 = request.args.get('USERINPUT1', None) 
    userinput2 = request.args.get('USERINPUT2', None)
return userinput1, userinput2
Karthick Mohanraj
  • 1,565
  • 2
  • 13
  • 28