How to inject multi line JS code in ___pragma___()?
Hi again,
I am trying to use __pragma__() to inject multi line JS code. The introductory webpages in transcrypt.org shows that it can be imported from org.transcrypt.stubs:
from org.transcrypt.stubs.browser import __pragma__
def run (autoTester):
...
__pragma__ ('js', '{}', 'self ["__dict__"] = {}')
link: https://www.transcrypt.org/docs/html/supported_constructs.html?highlight=pragma%20js#attribute-proxies-by-name-getattr-setattr
But, when I tried this way, 'org' not found error was raised. How can I import __pragma__?
I was using __pragma__ by copying the org.transcrypt.stubs directory to my local, and it worked. But I encountered an error when I upgraded the Transcrypt newest version 3.9.4. I think that copying the directory is not the right way.
Using #__pragma__ allows only single line code. If there is a way to inject multi line JS code, please tell me.
Thanks advance.
When I call ___pragma___ directly without importing, ___pragma___ not found error is raised. How can I use ___pragma___?
According to the documentation webpage, __pragma__ is acceptable only to Transcrypt without importing.
__pragma__ (<parameters>)
is acceptable only to Transcrypt, CPython requires a stub with parameter *args. Such a stub can either be defined locally or imported:
from org.transcrypt.stubs.browser import __pragma__
from https://www.transcrypt.org/docs/html/special_facilities.html#the-function-like-variety
But, Not Found Error is raised when I use ___pragma___() in Transcrypt
The stubs are only needed when you need to run your code with CPython. If you are only going to be using the Transcypt'd JS code then the stubs are not necessary because the objects in there are either in the Transcrypt runtime or they are built into JavaScript. But without the imported stub, or one that you create yourself (which is what I have been doing in many cases), the code will not run in CPython. Note that the stubs don't typically do anything, they mostly just allow the Python code to compile (though in some cases you might create a Python stub that mimics a similar JS behavior).
So you can create your own stubs that look something like this:
#__pragma__('skip')
def __pragma__(*args):
pass
#__pragma__('noskip')
In this case, when you run the code with CPython, it will use the self-defined do-nothing __pragma__ function, but when you transpile it, the Transcrypt compiler will ignore this stub because of the skip/noskip directive (I know this example feels self-referencing, but CPython already ignores the comment version __pragma__)
Are you needing to run your code with Python?
There does seem to be an issue with the Python path when trying to use the org import. So I am able to replicate that behavior, but I need to dig into it further. to resolve it If I have time, I'll try and tackle it this weekend. As far as I can tell, this has been an issue for some time.
Are you needing to run your code with Python?
No, I only run my code with Transcrypt.
I think that the best solution is to allow (or bugfix) __pragma__() without importing anything, and it is how the documentation webpage explains to use it in Transcrypt.
I need __pragma__() not #__pragma__() because I need to inject multi line JS code. Using org.transcript.stubs was a way to go around the problem that __pragma__() raised Not Found Error in Transcrypt. I think that using the stubs is not the right way because of collision to a new Transcrypt version. I want to remove the stubs I copied locally.
If I can use __pragma__()in Transcrypt, everything will work nicely without the stubs. I wonder why Not Found Error is raised in Transcrypt. Is it a bug?