roadlabs / cefpython

Automatically exported from code.google.com/p/cefpython
0 stars 0 forks source link

Allow to rebind javascript function so that Python's reload() works #12

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.import my_modual
2.while running CEF, change my_modual.py
3.reload(my_modual) ,which I'm binding to F5

What is the expected output? What do you see instead?
The change to my_modual.py should take effect

What version of the product are you using? On what operating system?
cef_python 3.7, for Python 2.7, on Window 7

Please provide any additional information below.
This is no show stopper, but could speed up testing if it worked.

Original issue reported on code.google.com by rich...@gmail.com on 9 Sep 2012 at 8:14

GoogleCodeExporter commented 9 years ago
Can you prepare a simple example of this behavior? I don't think that CEF has 
anything to do with this, you're probably doing something wrong.

Original comment by czarek.t...@gmail.com on 9 Sep 2012 at 8:22

GoogleCodeExporter commented 9 years ago
Here's the simple sample.  
3 files, the cef file, the html file, and the imported module.
It simply returns a number, and puts it in the input box.

1) run the app and press the button.  It returns 1.
2) change test.py to return 2
3) press F5 (reload) on the CEF window
4) press the button again.  It should return 2, but it still returns 1.

If there was a way to reload the Binding, that would be awesome.
As it is now, any HTML changes are done without reloading the app.
Imported modules should be able to do the same.
However I don't expect the CEF file to be able to reload, that is the
nature of apps.

Original comment by rich...@gmail.com on 9 Sep 2012 at 8:48

Attachments:

GoogleCodeExporter commented 9 years ago
This is how reload() works, references to old functions still exist, you need 
to re-bind all functions to make it work.

What you should do is to create a function called "do_bindings()" that you call 
when creating browser, and call again after you click F5.

But there is currently a problem with this, as you are not allowed to call 
bindings.SetFunction("test", test.test) again, when you try to do this you get 
this error:

  Exception: JavascriptBindings.SetFunction() failed: browser was already created, you are not allowed to call this function now.

Attaching: test_noreload2.zip

Original comment by czarek.t...@gmail.com on 10 Sep 2012 at 3:14

Attachments:

GoogleCodeExporter commented 9 years ago
So the method you included in your sample file will be the correct method to 
reload an imported file, but currently it causes an error.  Is that correct?

Original comment by rich...@gmail.com on 10 Sep 2012 at 4:10

GoogleCodeExporter commented 9 years ago
Yes, that is correct. That's the only way I see it, if you have other idea then 
introduce it.

Original comment by czarek.t...@gmail.com on 10 Sep 2012 at 7:49

GoogleCodeExporter commented 9 years ago
Fixed application crash when tried to call Frame.SetProperty() from OnKeyEvent, 
it happened because v8 object was being created in wrong context. It is now 
possible to do rebinding using Frame.SetProperty(), but it won't work if you 
call browser.Reload() or browser.ReloadIgnoreCache(), as it calls 
asynchronously and you lose the binding that was made with Frame.SetProperty().

Next step is to implement JavascriptBindings.Rebind() method.

Original comment by czarek.t...@gmail.com on 14 Sep 2012 at 3:58

GoogleCodeExporter commented 9 years ago
Issue fixed, see Revision 085042108e55.

This feature will make into 0.41 release today.

Original comment by czarek.t...@gmail.com on 14 Sep 2012 at 5:34

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Attachment didn't go through, but can't wait to see a reload work.
Will it work with F5?

Original comment by rich...@gmail.com on 14 Sep 2012 at 5:49

GoogleCodeExporter commented 9 years ago
Attachment works, check again.
Yes it works with F5.

Original comment by czarek.t...@gmail.com on 14 Sep 2012 at 5:56

GoogleCodeExporter commented 9 years ago
My fault, I was looking in Email.  :P

Original comment by rich...@gmail.com on 14 Sep 2012 at 6:11

GoogleCodeExporter commented 9 years ago
I've updated reload_example to support Python 3 (use of "imp.reload" instead of 
"reload"). Attaching reload_example.zip again.

Version 0.41 released, go to Downloads.

Original comment by czarek.t...@gmail.com on 14 Sep 2012 at 6:49

Attachments:

GoogleCodeExporter commented 9 years ago
Rebind works great.  This should make things more productive!
And thanks for the sample too, since we need to know that 
browser.ReloadIgnoreCache() needs to be called after a ReBind.

Original comment by rich...@gmail.com on 14 Sep 2012 at 7:10

GoogleCodeExporter commented 9 years ago
Use the code below to reload all loaded modules:

import sys, imp
for mod in sys.modules.values():
    if mod and mod.__name__ != "__main__": imp.reload(mod)

Original comment by czarek.t...@gmail.com on 15 Sep 2012 at 2:13

GoogleCodeExporter commented 9 years ago
Remember that if you have code like this:

    from mymodule import SomeClass

Then after you reload mymodule, SomeClass will still reference the old module, 
to reference the new module you would have to execute "from module" statement 
again, so better to avoid "from module" statements when using reload().

Original comment by czarek.t...@gmail.com on 15 Sep 2012 at 2:21

GoogleCodeExporter commented 9 years ago
After reloading all modules you need to set exception handler for the sys 
module again:

    sys.excepthook = cefpython.ExceptHook

I've added a Rebind/reload example to cefadvanced.py, see revision cc81ffd26efe.

Original comment by czarek.t...@gmail.com on 15 Sep 2012 at 4:03

GoogleCodeExporter commented 9 years ago
Rebind example updated, now it only reloads application modules, it checks it 
by module source file path, see revision 1ef1382e9dea.

Original comment by czarek.t...@gmail.com on 15 Sep 2012 at 5:11