When using <py-script src="/myscript.py"></py-script>
I would like to call it with arguments since myscript.py can be called from the command line like myscript.py arg1 arg2
.
How can I envoke this in pyscript? <py-script src="/myscript.py arg1 arg2"></py-script>
doesn’t work.
I don’t believe there is a direct way to do this in PyScript currently - the src
attribute of a PyScript tag fetches the content from the specified URL as the Python code to be run. It doesn’t know how to accept additional arguments.
You could potentially open an issue/proposal about it over on GitHub, I don’t think I’ve seen this suggested before. Might stir some interesting discussion.
If refactoring your code just a bit is acceptable, a workaround is to wrap your code in a function:
def main(arg1, arg2):
#Original here
if __name__ == "__main__":
main(sys.argv[1], sys.argv[2])
And then in PyScript:
#py-env fetches myscript.py and loads it into the virtual filesystem
<py-env>
- paths:
- ./myscript.py
<py-env>
<py-script>
from myscript import main
main(arg1, arg2)
</py-script>
This should preserve the ability to use the script from the command line, while also allowing you to feed arguments to the main
function directly.
3 Likes
Great, I can refactor the myscript.py to fit both. Thanks!
1 Like