1

I am using FLASK to create a series of websites for doing a survey, I want to run the server once and invite multiple participants to help me fill the form on the webpage.

1. I am aiming for assigning a unique ID (currently using python UUID) to different visitors and save their input info into different JSON files. (eg,. A's id is 00001, after he submiting the form, the info will be saved in the file named 00001.json, B's info will be saved in 00002.json). For now, I only create a UUID when the server start running, every visitor's information will be rewritten in the same file, (what I want is n visitors will produce n files).

2. I want to show different survey questions to different visitors.

I am quite confused about if it is related to Multi-thread? Session? Cookie? Flask-login.

Hope someone can guide me on this. Thanks a lot!

assignmentID = str(uuid.uuid1())
jsonFileName = "upload/" + assignmentID + ".json"
jsonSurvey = "upload/" + assignmentID + "_survey.json"

#it ideally should randomly select different info/question no. to different user
Idx = random.sample(range(0,10), 5)   


@app.route("/")
def index():
    return render_template("surveyPage.html", data=Idx)


# write input information from webpage to JSON files, each visitor ideally should have their own JSON file.
@app.route("/record")
def recordData():
    if request.method == 'POST':
        print("READING FORM")
        with open(jsonSurvey, 'w') as f:
            json.dump(request.form, f)
        f.close()


if __name__ == "__main__":
    app.config['JSON_SORT_KEYS'] = False
    app.run(debug = True)
# app.config["TEMPLATES_AUTO_RELOAD"] = True
adun
  • 13
  • 2
  • Welcome to Stack Overflow! Your current question specify multiple answers. Could you edit it to have just one? This would help others to give a focused answer. – Nikolay Shebanov Dec 03 '20 at 23:12
  • You should move the top level code into the view functions, and pass Idx as a query parameter on the post request. Usually it is accomplished either with a hidden field or by specifying the id as a part of the post endpoint url. – Nikolay Shebanov Dec 03 '20 at 23:19

1 Answers1

0

You are defining assignmentID, jsonFileName, jsonSurvey and Idx outside of the request handlers which means they will be set once: when the server starts. jsonSurvey will therefore have the same value for each request which means that every new survey you store will overwrite the previous one.

The solution is to move the definition into the request handlers so that you genereate new values on each request.

@app.route("/")
def index():
    Idx = random.sample(range(0,10), 5) 

    return render_template("surveyPage.html", data=Idx)

@app.route("/record")
def recordData():
    if request.method == 'POST':
        assignmentID = str(uuid.uuid1())
        jsonFileName = "upload/" + assignmentID + ".json"
        jsonSurvey = "upload/" + assignmentID + "_survey.json"

        print("READING FORM")
        with open(jsonSurvey, 'w') as f:
            json.dump(request.form, f)
        f.close()
Simeon Nedkov
  • 1,097
  • 7
  • 11
  • Hi Simeon, thanks for the reply, do I need to take care of the session or multithread by assign id to the different user session? Let's say, I have 2 webpages of surveys for each participant filling, so I will create ids for people on the first survey page, if 2 people submit the 2nd form at the same time, which will call the recordData() function, how to make sure the correct input information of 2 survey forms will be saved in the correct person's jsonsurvey file? – adun Dec 04 '20 at 17:37
  • No, that should not be necessary as each request will generate unique `assignmentID` and `Idx` values regardless of the thread or session it is processed in. You may get duplicate values from `uuid1()` when you generate 2^14 of them in less than 100ns, see https://stackoverflow.com/questions/1785503/when-should-i-use-uuid-uuid1-vs-uuid-uuid4-in-python but you'll run into other problems long before you're able to do that. – Simeon Nedkov Dec 07 '20 at 08:31