Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

439
Views
How do you pass a variable from javascript to python (oTree) or django?

I have some created some variables in Javascript to tracker how long an individual holds a button. I would like to pass these variables within a class in python (oTree) to store the data.

So, suppose the Javascript code is as follows:

let javascriptvariable = 0;

This javascriptvariable will be updated as the user clicks the button, so let's say the final value is javascriptvariable = 100. The python code is below.

class DisplayCharacteristics2(Page):
    form_model = 'player'
    pythonvariable = javascriptvariable


    @staticmethod
    def vars_for_template(player: Player):
        return {
            "var1": player.var1,
        }

I tried created an empty variable, but I get an error which says something like javascript can't "add" to a null variable. I also tried using django in combination with javascript (see below), but javascript cannot add to a null variable. I tried to let it equal to zero first, but then I only get data points of 0.

let javascriptvariable = `{{ pythonvariable|safe }}`

Any leads will be appreciated! Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Using live pages seems to solve the issue.

https://otree.readthedocs.io/en/latest/live.html

So in Javascript:

 function sendTracker1() {
          liveSend({'which_char': 'char_1', 'value': tracker1})
      }

And in Python:

class MyPage(Page):
    form_model = 'player'

    @staticmethod
    def live_method(player, data):
        if data['which_char'] == 'char_1':
            player.timer_char1 = int(data['value'])


about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!