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

How to use return values from JS function in Python script #308

Closed enzok closed 4 years ago

enzok commented 4 years ago

I'd like to use JS functions in my Python script to decode obfuscated text. I can send the input parameters to JS, but have not been able to use the results as return variables in Python.

I tried with a callback, but was only able to print the value. Not sure what I'm missing here.

Thanks.

Code snippet(s)

import eel

eel.init("web")

n = "foo"
res =  eel.decoder(n)
# do something with res
eel.start('decoder.html', size=(400, 300))
<html>
    <script type="text/javascript" src="/eel.js"></script>
    <script type="text/javascript">
      eel.expose(decoder);
      function decoder(n) {
      return "bar" }
    </script>
</html>
samuelhwilliams commented 4 years ago

Hi @enzok. Sorry you're having trouble.

The code snippets you've provided aren't functional - eel isn't initialised or started, and the JS decoder function isn't exposed to Eel. Could you please provide working code snippets and then perhaps we can help you more effectively? :)

enzok commented 4 years ago

i've updated the snippets. is it possible to use the return values from JS in my python script?

samuelhwilliams commented 4 years ago

Yes - see the example here: https://github.com/samuelhwilliams/Eel/blob/master/examples/03%20-%20sync_callbacks/sync_callbacks.py

Essentially you need to change res = eel.decoder(n) to res = eel.decoder(n)().

Does that work for you?

enzok commented 4 years ago

Thank you I can't believe I missed that. After making the changes to resemble that example, it appears to be working now. Awesome.