Inquiring Javascript runtime properties of elements #240
Replies: 3 comments 2 replies
-
This is very interesting. Can you make inquire_properties a method of the element (wp.div_content in this case)? I think it could then be a good addition to JustPy. |
Beta Was this translation helpful? Give feedback.
-
Thanks Docm64 … This is quite a useful technique to return actual co-ordinates from the live web page, using say getBoundingClientRect(). This is most useful for the dynamic application of Svg components that link other page elements. Ideally, a set of such properties could be defined during initialisation of the component and be always available for use as the the page layout and the overall application state evolved. |
Beta Was this translation helpful? Give feedback.
-
This method no longer works. Is there a another way of getting results back from javascript? |
Beta Was this translation helpful? Give feedback.
-
The more I work with JustPy framework, the more I like it. Really great stuff.
There are situations where it is useful to inquire runtime properties of components as they are rendered in the client's webbrowser. The WebPage.run_javascript() method provides a hook to do so, but the result of it is delivered back to Python via a 'result-ready' page event which is not very convenient as it leaves the application programmer with the task to associate this response with the original request by means of its 'request_id'
I thought to simplify it by a little bit of coroutine magic. And ended up with a small helper class I wanted to share here.
Basically it offers two coroutines.
js.inquire_properties()
takes as argument a JustPy element, a WebPage that contains this element and a list of js properties to be inquired from this object. It returns to the awaiting coroutine an (addict) Dict of property: value pairs.What it does internally is to invoke
WebPage.run_javascript()
with a js oneliner built from the passed arguments. It then creates and awaits a Future which will be completed by the 'result-ready' event. Every request will create a unique reference-id which is used as key to retrieve the Future from a Dict.The user has to assure two things: The element to be inquired must have an id, so use temp=False when creating it. And the 'result-ready' page event must be linked to
js.handle_page_event()
Please see the source code below. It also includes an example. I am using Quasar classes in that example but ordinary JustPy Tailwind classes work in the same manner.
Note the b_on_click() event handler. Inquiring the runtime size of an element using js is jnow ust a Python oneliner.
Hope someone finds it useful. Have fun!
Beta Was this translation helpful? Give feedback.
All reactions