Skip to content
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

Elegant way to comunicate variables to JS #965

Closed
martinlombana opened this issue Oct 6, 2022 · 4 comments
Closed

Elegant way to comunicate variables to JS #965

martinlombana opened this issue Oct 6, 2022 · 4 comments
Labels

Comments

@martinlombana
Copy link
Contributor

Specification

  • pywebview version: pywebview==3.6.3

  • operating system: win10

  • web renderer: edgeView2

Description

Per the docs, I am using evaluate_js. Nevertheless, I wonder if there should be (or maybe there is and I don't know about it), a way to just call js functions that are exposed to an object from python directly, the same way that we can call and expose an API class from python to JS. This could make it much more "coherent", and if we needed to pass objects like arrays or dicts, from Python to JS, it could be done in a much cleaner way.

Right now, If we want to pass an array to JS, unless I am missing something, we have to do something like dump the data to a dict to have it in the raw code, but I find it ugly. I know how to write my own code, in order to abstract it and create a pseudo_api, like the exposed API python for the JS side.

'''
send_data({"pepe": 4, "lol": [3, 3, 4, 5, 5]})

def send_data(self, data):
	json_string = json.dumps(data)
	result = self.window.evaluate_js(
		f"""
		report_data({json_string})
		"""
	)

'''

But I wonder if it shouldn't be best, for pywebview, to have the option to pass a JS class, with functions to the python side, and have the python side be able to call them, in order to avoid the raw strings "madness" for general domain intercomunication.

Practicalities

  • YES, I am willing to work on this issue myself.
@r0x0r
Copy link
Owner

r0x0r commented Oct 26, 2022

I like this idea, but to be honest I do not see a clear way how this should be handled. Do you have suggestions how this should be implemented on the API level? Both in Python and Javascript?

In theory it would be great to auto-update variables as well, but I am afraid this is not something that could be done in Python

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale label Nov 26, 2022
@github-actions
Copy link

github-actions bot commented Dec 1, 2022

The message to post on the issue when closing it. If none provided, will not comment when closing an issue.

@github-actions github-actions bot closed this as completed Dec 1, 2022
@r0x0r
Copy link
Owner

r0x0r commented Feb 6, 2025

I am implementing this now in form of state. Details here #1594

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants