python-eel / Eel

A little Python library for making simple Electron-like HTML/JS GUI apps
MIT License
6.44k stars 587 forks source link

Circular reference error #384

Closed CombatVolcano closed 4 years ago

CombatVolcano commented 4 years ago

When calling into Python from JS ( originates from an onclick event to a js function call ) I am getting this error;

Uncaught TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    |     property '_buttons' -> object with constructor 'Array'
    |     index 0 -> object with constructor 'Object'
    |     ...
    |     property 'context' -> object with constructor 'Array'
    --- index 0 closes the circle
    at JSON.stringify (<anonymous>)
    at Object._toJSON (eel.js:73)
    at WebSocket.eel._websocket.onmessage (eel.js:135)

I can't point it to my own code as the error doesn't reference it. What should I look for? There are a lot of variables involved.

It roughly goes button click > js function collects a lot of things by ID > eel call to python > tons of python things > eel call to JS function... but there is a lot of code that does that without this problem

Thank you

samuelhwilliams commented 4 years ago

Can you include your own code please, to help us understand what might be going on?

CombatVolcano commented 4 years ago

Can you include your own code please, to help us understand what might be going on?

It is extensive and confidential, unfortunately for this case.

Can you tell me what cause such an error, that I might look?

Things seem to work regardless of this error, however, I am having problems with the page saving itself (posted that in issues) and I wonder if this is related.

samuelhwilliams commented 4 years ago

It doesn't seem like an issue with Eel - it looks to me like JavaScript isn't able to convert your data into a JSON object because there are circular references, e.g. something nested in your data references something above it in your data. For example if you have an object A that references object B, which itself references object A, then trying to serialize A will try to serialize B, which will try to serialize A again, which is either an infinite loop or caught as a circular reference.

I'm afraid we can't help as, to me, this looks like it lies in your code - and if you're unable to share your code then there isn't much help we can reasonably offer.

CombatVolcano commented 4 years ago

I appreciate your hard work on eel. I will try to do some work to eliminate factors and see what I can come up with, as you said there must be some circular reference, but it is not obvious what it is. Does the " property '_buttons' " get included simply because it originated from a button click function? Or is that a deeper clue? My python is far stronger than my JS

samuelhwilliams commented 4 years ago

Eel only transfers what you send - I don't know what _buttons is in your case but if it's getting sent it's because you're sending it as an argument to an Eel call. You'll need to work out what you're calling and what you're sending - I suggest only sending the bare minimum information you need to.

CombatVolcano commented 4 years ago

I believe the problem is that I was issuing a return(window.variable) from a py call into JS where no variable was being assigned in py. So py was just calling eel.whatever(), with no assignment, but JS was trying to return a window.variable that was just set further up in the function. So that was weird, and I didn't need the return value anyway, so I removed the return statement from my weird JS (this is all complicated stuff having to do with building heavily interactive customized bootstrap tables that is frankly at the edge of my JS skill) and I no longer seem to get the circular error and my page seems to have stopped trying to save itself (hope that lasts).

Thank you so much for being here to help with Eel and for maintaining the project, I really appreciate your work. Projects like this enable productivity that is otherwise not possible for individuals like myself with heavy tasks and few resources. Thank you.