-
-
Notifications
You must be signed in to change notification settings - Fork 574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A call from CEF to js_api to a Python standard json library's dumps() returns a promise, but the resolved promise returns an object and not a string #352
Comments
You don't have to use json.dumps, but you can return a dict as is. It will be automatically converted to a Javascript object |
@r0x0r I don't understand you. In Python, json.dumps returns a string, which I want to use in CEF. I know there's the javascript JSON.stringify, which does the same, but I was testing wether it's possible in principle, to get to know the js_api. My function json_stringify in Python returns a string, and in my Javascript's json_stringify when the promise is solved it returns an object, but what to do with it? For instance, when using |
What do you want to achieve? Types are converted automatically between JS
and Python. You don't need to call json.dumps or JSON.stringify. See
examples/js_api.py
… |
JSON.stringify/json.dumps returns a string representation of a javascript/python object, but JSON is not a data type in any of the languages, so I don't think automatic type conversion is happening. JSON can be Object -> String conversion and the other way round, in any language that supports objects and strings. What you're talking about is 1:1 type conversion, where a data structure from one language has an analog in the other (i.e. Javascript Object -> Python Dict). I am still figuring out js_api's purpose, so bear with me. As I understand it, you can call a python function from javascript, asynchronously (because of the promise returned). Well, in that case I expect json_stringify to do as I tell it: when the promise resolves, I expect my JSON string, just like Python's json.dumps and just like Javascript's JSON.stringify does. But it doesn't: the resolved promise gives me this object of type "object". Even if I were happy with that "object", I look further and there's no JSON formatted string to be found inside. |
Sorry I am not following this conversation at all. Here is how a js api call is made. Line 86 in ac53fa9
Another file worth investigating is https://github.com/r0x0r/pywebview/blob/master/webview/js/api.py |
Thanks for the link. Please try this easier example (I'm using CEF): Case 1
Python:
Javascript Output:
Python Output: Case 2 Javascript Output:
Python Output: json.dumps returns a string in Python, so it doesn't make sense that javascript receives some random object and not a string when my_string uses json.dumps. I can't find my_string inside this random object. The only difference is that a literal notation is used in the second case. But we're talking about strings in both cases. EDIT: I know I forgot a brace in case 2, but this shouldn't lead to the differing outputs that you see, where case 1 shows "object" as the resolved promise and case 2 shows "string" as the resolved promise. In any case strings are sent to js_api, but somewhere there is some implicit conversion happening to a JS object when my_string is JSON formatted. |
This works as expecteda as JSON serialization is used for converting types |
js_api should not implicitly convert my_string to a javascript object type! I figured out in what circumstances this implicit Python string to JS object conversion takes place:
Javascript output says the resolved promise is of type object. If you set my_string to anything other than JSON, such as Why do you think this implicit conversion from a Python string type to a JS object type is logical if the string happens to be JSON formatted? Why not let the user decide in their JS code what to do with Python dictionary syntax or JSON syntax inside a JS string that came from a Python string ( |
Took me while to understand the issue, but I am getting there. Please bear with me 😆 |
Hahaha @r0x0r you had me on the edge of walking away mad; me screaming: "let the bugs be set free!!11" or me posting a mathematical logical proof of the problem with svg flow chart diagrams in the hopes that it would become clear. ^-^ |
I have pushed a fix to master. Could you verify if it works for you? |
Firstly, I get relative import errors with your import in test_js_api.py (using python 3.7) when it is in the webview folder. I used
|
Pull the master branch, then execute in the pywebview root directory
|
Did you mean "download" the master branch? Pulling means submitting my own project branch for review to be potentially merged with a master, right? Anyway, I downloaded the master. I never use cmd environment variables, so could you explain how to run that? Why do I need PYTHONPATH and can't I run test_js_api.py from its folder? I don't have PYTHONPATH available so I do:
I put pywebview folder in site-packages. And in the pywebview folder I run:
But I get something with not recognized as internal command error. |
Fix is deployed to 3.0.2 |
Specification
pywebview version: I don't know where to find the version. There's no readme in the webview folder. Folder was created on 5th of june 2019.
platform / version: Windows 7, 32-bit
Description
A call from CEF to js_api to a Python standard json library's dump() returns a promise, but the resolved promise returns an object and not a string
Here is the Javascript:
The Python:
Side question: is there a way to make the js_api run Python functions synchronously? So that the javascript is halted, python is run, then javascript continues?
Note: I even added str() to json.dumps (which should be redundant) but that doesn't work either.
The text was updated successfully, but these errors were encountered: