'pyodide.JsProxy' object is not subscriptable(PyScript)

TL;DR scroll to bottom

PyScript is a framework that allows for Python in HTML. Technically, this should also be tagged as HTML/JS/CSS.

The function move in the below class p serves the purpose of incrementing x/y values, when key inputs from JS(held in an object known as keys) match the object’s registered input values from the parameter keys. I now realize how confusing that is, but please bear with me.

Python Code:

class p:

  def __init__(self, x, y, id, imgs, keys):
    self.x = x
    self.y = y
    self.id = id
    self.imgs = imgs
    self.framenum = 0
    self.score = 0
    self.alive = True
    self.motion = False
    self.keys = keys

  def cld(self, ob):
    if self.x == ob.x and self.y == ob.y:
      return True
    else:
      return False

  def move(self,inputsrc):
    if inputsrc[self.keys[0]]:
      self.y -= 1
    if inputsrc[self.keys[1]]:
      self.y += 1
    if inputsrc[self.keys[2]]:
      self.x -= 1
    if inputsrc[self.keys[3]]:
      self.x += 1

The object p1 is created in Python:

p1 = p(0,0,"1",[0,1,2,3,4],["keyUp","keyDown","keyLeft","keyRight"])

JS for keys(the object):

let keys = {};
  document.addEventListener('keydown', (event) => {
    keys[event.key] = true;
  }, false);
  document.addEventListener('keyup', (event) => {
    keys[event.key] = false;
  }, false);

//assume p1 object has already been imported from python
p1.move(keys)

When ran, the console returns the following:

File “”, line 66, in move
TypeError: ‘pyodide.JsProxy’ object is not subscriptable

Either the Python parameter keys is not being read as an array, or the JS keys object is being read as an array. Another possibility is that the JS object keys is empty, yet is still being read.

Here’s a link, 'cause I explained that poorly:
https://replit.com/@arn5891/idk-man-its-something

nvm, syntax error. forgot to use range() for python.

1 Like

Why not just write the class in JavaScript?

class p {
  constructor(x, y, id, imgs, keys) {
    this.x = x;
    this.y = y;
    this.id = id;
    this.imgs = imgs;
    this.framenum = 0;
    this.score = 0;
    this.alive = true;
    this.motion = false;
    this.keys = keys;
  }
  cld(ob) {
    return this.x === ob.x && this.y === ob.y;
  }
  move(inputsrc) {
    if (inputsrc[this.keys[0]]) {
      this.y -= 1; // or this.y--;
    }
    if (inputsrc[this.keys[1]]) {
      this.y += 1; // or this.y++;
    }
    if (inputsrc[this.keys[2]]) {
      this.x -= 1; // or this.x--;
    }
    if (inputsrc[this.keys[3]]) {
      this.x += 1; // or this.x++;
    }
  }
}
1 Like

Cause js is pain (/jk), and most people afaik don’t even know js has classes. I mean honestly, people should all just use WASM lol.

2 Likes

Imagine if we lived in a world where every site was WebAssembly!

2 Likes

Only downside is that you cannot interact with DOM through WASM yet unless you use JavaScript. Yk what, I am gonna make a poll.

1 Like

@MattDESTROYER I didn’t use JS 'cause of what @dragonhunter1 said- JS is incredibly slow; idk if creating the classes in JS would make the program take up more memory. The only thing JS exists for in this program is graphics- plus html allows for running the game in a new tab.

1 Like

You do realise that what you’re doing is writing Python and then using JS (albeit with WASM) to ‘run’ it, it’s not real Python it’s just an attempt at a Python interpreter with JS and WASM, so that’s most likely even slower… It would potentially take less memory in pure JS, but also the amount of memory it would take is very little regardless.

1 Like

Idk what I said, but here is a correction: JS is slow, python is incredibly slow, and python running on js is unbearably slow (All a little extreme, depends on the situation)

2 Likes

@MattDESTROYER @dragonhunter1 yeah I’m dumb. Really, the only benefit of this is that I can get the program to run on any screen size without getting graphics messed up. I wasn’t able to get text working in pygame, plus I can do a lot more fonts and styling with css.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.