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

runPythonSource should allow additional options like debug, class, cache currently available only using script tag #2421

Open
shlomil opened this issue Apr 23, 2024 · 2 comments

Comments

@shlomil
Copy link

shlomil commented Apr 23, 2024

Current runPythonSource supports specifing only script_id:
__BRYTHON__.runPythonSource(src[, script_id])
However it does not allow setting additional options like class, debug , etc , forcing usage of the script tag.

Feature request:
If possible, have the function recieve an extra optinal parameter for additional options:
__BRYTHON__.runPythonSource(src[[, script_id], options])
example:
__BRYTHON__.runPythonSource(src, "main", {debug:"1"}])

PierreQuentel added a commit that referenced this issue Apr 27, 2024
…reted as script attributes. Can be used to pass execution options. Related to issue #2421
@PierreQuentel
Copy link
Contributor

Good point Shlomi ! In the commit referenced above I have changed the implementation of runPythonSource() so that the second argument can be either the script id (for compatibility with previous versions) or a JS object interpreted as the script attributes. Execution options can be passed this way.

I don't mark the issue as closed yet, could you check if the result works for you ?

@shlomil
Copy link
Author

shlomil commented Apr 29, 2024

Thanks Pierre :)
I tested the following scenarios (uncomented each in a turn):

        //// (V) The second one appeared in devel tools network tab with the ?v=timestamp search param:
        //__BRYTHON__.runPythonSource("import some_module",{id: 'main1', cache:"1"})
        //__BRYTHON__.runPythonSource("import some_module2",{id: 'main2', cache:"0"})

        //// (V) The second one printed the js code that was generated:
        //__BRYTHON__.runPythonSource("import some_module",{id: 'main3', debug:"0"})
        //__BRYTHON__.runPythonSource("import some_module2",{id: 'main4', debug:"2"})

        //// (V) Import from one to the other - works
        //__BRYTHON__.runPythonSource("print('hello from imported modu1')",{id: 'modu1', debug:"0"})
        //__BRYTHON__.runPythonSource("import modu1",{id: 'main4', debug:"1"})

        //// (V) args - works
        //__BRYTHON__.runPythonSource("import sys;print(sys.argv)",{id: 'main4', args:["aaa","bbb","ccc"]})

        
        //// (?) returns python_script_769094014825749 - Shouldn't it be __main__ ?
        //// I expected runPythonScript to behave the same as <script> tag when no id is given however I guess there is logic
        //// in alllowing this in api call so the function could be called repeatedly with no additional arguments.
        //__BRYTHON__.runPythonSource("print(__name__)")


        //// (V) import from pythonpath
        //__BRYTHON__.runPythonSource("import sys;print(sys.path); import some_module",{id: 'main', pythonpath:["/somepath1/somepath2/"]})

All worked with the latest version. I couldn't make the following webworker example work:

        __BRYTHON__.runPythonSource(`
from browser import bind, self
@bind(self, "message")
def message(evt):
    result = 'pong' if evt.data == 'ping' else evt.data
    self.send(result)
`,{id:'worker','class':'webworker'})

        __BRYTHON__.runPythonSource(`
from browser import worker

def ready(w):
  w.send('ping')

def message(ev):
  print(ev.data)

worker.create_worker("worker", onready=ready, onmessage=message)
`,{id: 'main'})

It throws : RuntimeError: No webworker with id 'worker'
I did expect it to work since I expected to be able to do everything that can be done using a <script> tag.
I don't know if that's considered as a show stopper by you so I'll leave it to you to decide whether to close the bug or not.

Best regards,
Shlomi

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

No branches or pull requests

2 participants