[PY] Added the ability to run Brython code#8640
[PY] Added the ability to run Brython code#8640Ahsoka wants to merge 25 commits intoSeleniumHQ:trunkfrom
Conversation
Used to load the necessary Javascript files to run Brython code.
This method allows you to execute Brython code just like the execute_script() method allows you to execute Javascript code.
Before the stdlib was accessed via files in the Brython Github repository, but it now accessed through the brython_stdlib.js file. Initially, I thought this method of acessing the Brython stdlib would not work because for some unknown reason, when brython_stdlib,js accessed the Indexed Database API, it caused the following error: "access to the Indexed Database API is denied in this context". However, recently I discovered in the brython() function there is an option to disable access to the database (which is used to load the Brython stdlib) and access through some other method (which I don't fully understand).
|
I am not sure how I would go about adding documentation. I have added a docstring to both of the new methods I created. Does this satisfy the |
AutomatedTester
left a comment
There was a problem hiding this comment.
I don't mind adding support for Brython but ongoing it might break and there are no tests in this patch to make sure that never happens.
Would it not be better to have a package that you own that has a dependency on Selenium that you ship?
| 'args': converted_args})['value'] | ||
|
|
||
| def load_brython(self, | ||
| src="https://cdn.jsdelivr.net/npm/brython@3.8.9/brython.min.js", |
There was a problem hiding this comment.
Are you sure Brython users would always be in the internet? Who not bundle as data like some of the other JS packages that are shipped
There was a problem hiding this comment.
I also thought of something similar to what you suggested but unfortunately it can be unreliable. For example, in Chrome whenever I try to access local files I get the follow error Not allowed to load local resource: {file name here}. However, I believe in Firefox it would work with no problem (I have not tested this but I have reason to believe this to be the case from the exhaustive amount of research I have conducted on this topic). Additionally, unless configured in a certain way, Brython has a reliance on certain external files anyway. Loading Brython from a file would still require access to the external files via the internet.
There was a problem hiding this comment.
We need to use bazel here. I will help with this part.
| @@ -0,0 +1,4 @@ | |||
| var script = document.createElement('script') | |||
There was a problem hiding this comment.
These files should in /javascript/brython
There was a problem hiding this comment.
When you refer to /javascript/brython do you mean py/selenium/webdriver/remote/javascript/brython or the top level directory /javascript/brython.
There was a problem hiding this comment.
top level /javascript/brython and add a bazel file or if you want to use npm then we can add it to the package.json and I will help with the bazel stuff .
There was a problem hiding this comment.
Alright I added the files to the top level directory /javascript/brython. See this commit for reference. I am not too familiar with Bazel so please let me know what I need to do to reference the files in my code.
As of now they are currently referenced like this:
See the below here.
_pkg = '.'.join(__name__.split('.')[:-1])
template = pkgutil.get_data(_pkg, 'loadBrython.js').decode('UTF-8')and like this
_pkg = '.'.join(__name__.split('.')[:-1])
running = pkgutil.get_data(_pkg, 'runBrython.js').decode('UTF-8').format(repr(script))See the above here.
Before this patch there was a **MAJOR BUG** that would cause an error when using the `execute_brython()` too soon after using the `load_brython()` method. Now, the `load_brython()` method will now terminate **ONLY** after Brython has been succesfully loaded.
This test tests for several things: - Can Brython be successfully loaded in a reasonable amount of time (5 seconds to be exact)? - Are the stdlib modules accesible? (this is tested by accessing the turtle module) - Is execute_brython functioning properly? (This is tested by running a simple script with turtle)
|
I added a single test that will pretty reliable test whether or not Brython works. See these changes for reference, |
|
@AutomatedTester Whenever you get a chance please let me know what the next steps are in getting this PR ready to merge. Thank you! |
The reasoning behind this is instead of constantly changing the URL everytime a newer version of Brython comes out, use a URL that will always point to the latest development version. If users do not like this they always have the option to override the default and pass in whatever version they would like instead.
For compatibility with older versions of Python
|
@AutomatedTester Any updates on this PR? |
|
Hey @AutomatedTester, I just wanted to follow up again to see if there are any plans to merge this PR? If not, please let me know so I can stop bothering you and close this PR. 🙃 |
I need to find time to fix the build system for this PR. I am hoping soon |
|
Awesome, thank you so much for getting back to me. If there is anything I can do, please let me know! 😁 |
|
Any updates on this? |
Description
For the uninitiated Brython is a way to use Python code on the front-end instead of Javascript. This pull-request adds a
execute_brython()method which allows you to execute Brython code much in the same way you would use theexecute_script()method to execute JavaScript code. There is also now aload_brython()method which is used to load the necessary JavaScript files to run Brython code and must be called before using theexecute_brython()method.Examples
Here are a couple of examples of using the
load_brython()andexecute_brython()methods.Example 1
Example 2
This is
turtle_script⬇Result of Example 2 ⬇
Types of changes
Checklist