custom.py not updating

12 views
Skip to first unread message

Gaia Molinaro

unread,
Feb 5, 2022, 1:45:46 PMFeb 5
to PsiTurk
Hi!

I've created a custom.py that fails to get called at the end of my experiment. 
The server.log outputs:
File "/home/username/env/lib/python3.8/site-packages/flask/templating.py", line 86, in _get_source_fast
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: compute_bonus


However, I'm not raising any exceptions of that type in my custom.py file (I even tried commenting out the jinja2 import) so I'm afraid the server is still using the original template, even after restarting it (js files get updated as expected though). 
How can I force the server to use the new version of custom.py? 
Or is it possible that the error is somewhere else?

Here is my custom.py code:
@custom_code.route("/http://localhost:22362/compute_bonus", methods=["GET"])
def compute_bonus():
    # check that user provided the correct keys
    # errors will not be that graceful here if being
    # accessed by the Javascript client
    if not "uniqueId" in request.args:
        # i don't like returning HTML to JSON requests...  maybe should change this
        raise ExperimentError("improper_inputs")
    uniqueId = request.args["uniqueId"]

    def get_completion(user_df):
        criterion = "questionnaire" in user_df.phase.unique().tolist()
        return criterion

    def get_time_bonus(elapsed_ms, hourly_rate=8, dp=2):
        return round(hourly_rate*(elapsed_ms/3600000), dp)

    def get_perf_bonus(perf, worst_perf=0.5, best_perf=0.8, best_bonus=2.5, dp=2):
        if perf >= best_perf:
            return best_bonus
        elif perf <= worst_perf:
            return 0
        elif (perf > worst_perf) and (perf < best_perf):
            m = best_bonus/(best_perf-worst_perf)
            q = -m * worst_perf
            return round(m*perf + q, dp)
        else:
            return 0

    user = Participant.query.\
            filter(Participant.uniqueid == uniqueId).\
            one()
    user_data = loads(user.datastring)  # load datastring from JSON
    flat_user_data = [record["trialdata"] for record in user_data["data"]]
    user_df = pd.DataFrame(flat_user_data)  # get user data as pandas df

    max_elapsed_time = user_df.time_elapsed[user_df.time_elapsed.last_valid_index()]  # in ms
    bonus = get_time_bonus(max_elapsed_time)

    if get_completion(user_df):
            perf = round(user_df_learn_points[user_df_learn_points.correct.astype("str") == "True"].correct.count()/(num_trials/2), 4)
            bonus += get_perf_bonus(perf)  # add performance bonus
    print(bonus)

    user.bonus = bonus
    db_session.add(user)
    db_session.commit()
    resp = {"bonusComputed": "success"}
    return jsonify(**resp)


Thank you so much for your time and help!
Gaia

Gaia Molinaro

unread,
Feb 5, 2022, 1:50:36 PMFeb 5
to PsiTurk
*correction:
This is not my whole custom.py code, just the part where I compute the bonus. The rest is the same as the original template.

Dave Eargle

unread,
Feb 5, 2022, 1:56:41 PMFeb 5
to Gaia Molinaro, PsiTurk
Your code snippet looks broken, and you didn't paste the full relevant server.log, but I think the problem might be that your .route() is wrong (look at the example), so flask can't find a route named 'compute_bonus' so maybe it's assuming it's a template, which it's not, hence the exception. Psiturk is a flask app which uses jinja2 templating, that's why you're seeing jinja exceptions. Anyway, fix your route specification.

--
You received this message because you are subscribed to the Google Groups "PsiTurk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psiturk+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psiturk/7e9ea5b3-11c5-4be9-992f-5b43591cfb20n%40googlegroups.com.

Gaia Molinaro

unread,
Feb 5, 2022, 2:36:56 PMFeb 5
to Dave Eargle, PsiTurk
Thanks Dave, 

And sorry about that, I'm fairly new to all of this. Happy to share more of the server.log's info if that helps!
I corrected my python function but I'm not sure what the error is with the route specification. I followed the directions I found here, where it says "For example, if you are running your task locally on port 5000, then type in http://localhost:5000/compute_bonus" (and added a leading backslash). 
Could you please clarify?

Thanks again!
Gaia
--
Gaia Molinaro
PhD Student at UC Berkeley
Computational Cognitive Neuroscience Lab

Dave Eargle

unread,
Feb 5, 2022, 3:43:09 PMFeb 5
to Gaia Molinaro, PsiTurk
That guide language is confusing, that's referring to something else -- maybe to something with the browser? Leave the python route decorator just as it is in those docs, and as shown below. Go through a flask quickstart if you want to learn more.

@custom_code.route('/compute_bonus', methods=['GET'])

Gaia Molinaro

unread,
Feb 5, 2022, 7:59:03 PMFeb 5
to Dave Eargle, PsiTurk
Got it, thank you so much!
Reply all
Reply to author
Forward
0 new messages