pywebview icon indicating copy to clipboard operation
pywebview copied to clipboard

Problems using evaluate_js function and parameter js_api

Open kobeee opened this issue 8 years ago • 8 comments

Hello, recently I was using pywebview to show my own website. I used 'create_window' with a url "http://localhost" which running on the local server, it seemed that the js_api didn't work, but it worked when I loaded a single html. Can you help me? The evaluate_js function didn't work neither, I wonder know how this function running, is that js codes running inside the webview? Thank you very much!!!

kobeee avatar Mar 13 '18 13:03 kobeee

You can use alert to see if your js code executes

r0x0r avatar Mar 13 '18 17:03 r0x0r

Actually, the evaluate_js function did run in my codes, the situation is that I using pywebview to show a threejs rendered web page, and I want to control the threejs object outside the webpage, I tried the evaluate_js function to control a global variant defined in my webpage, but it didn't work, so I changed to js_api to send some controlling parameters and it was still failed. Is that possiple to controll the global variant in the web page using pywebview? Thank you for the fast response

kobeee avatar Mar 14 '18 00:03 kobeee

js_api is for controlling Python object from JavaScript. To control JS objects in Python, you should use evaluate_js. You can do this from Python (JS code taken from threejs docs):

def create_scene():
    webview.evaluate_js("""
        var scene = new THREE.Scene();
        var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

        var renderer = new THREE.WebGLRenderer();
        renderer.setSize( window.innerWidth, window.innerHeight );
        document.body.appendChild( renderer.domElement );
    """)

You have to call this function from a thread. Refer the evaluate_js.py file in the examples folder.

shivaprsd avatar Mar 14 '18 06:03 shivaprsd

I see. But if I want to do a real time controlling, should I keep the function evaluate_js running all the time? It looks like that I can call many times evaluate_js before the the function create_window running, but when this function did run I can't call any function until it ended, unless I create another thread to run the evaluate_js. Any idea for real time controlling?

kobeee avatar Mar 14 '18 06:03 kobeee

Yes, you have to create a thread. What is your use case of having to control the threejs object from Python? Can't this be done in JavaScript itself?

shivaprsd avatar Mar 14 '18 06:03 shivaprsd

The threejs big scene has been done and we need to send some external parameters to the scene without network, so I found pywebview as a wrapper. It's a great tools. I think this issue can be closed now, thank you very much for all your help.

kobeee avatar Mar 14 '18 06:03 kobeee

Great. evaluate_js is the tool you need. For real time control keep a thread running throughout your program, and call evaluate_js from it whenever you need. Feel free to ping back if you get stuck. 👍

shivaprsd avatar Mar 14 '18 07:03 shivaprsd

To add to Shiva's answer, you may also use a Python method defined via js_api. It may be a better choice, if you have to react to a JS event. This way you don't have to resort to busy polling in Python code.

r0x0r avatar Mar 14 '18 09:03 r0x0r