Using "<script type="text/python></script>" syntax ?

2,021 views
Skip to first unread message

Hypee

unread,
Jun 20, 2011, 5:30:09 AM6/20/11
to Skulpt
Hi,

Was anybody being able to make work the "billearney" cloned version of
Skulpt which is supposed to allow use of the <script type="text/
python></script> syntax ?

The example provided in this cloned version didn't work for me out of
the box, seems that's "deps.js" is calling for some Closures libs/
files that are not in the repository.

It also seems that this functionality isn't included in the master
branch of Skulpt.

Thanks !

Billy Earney

unread,
Jun 21, 2011, 8:31:43 AM6/21/11
to Skulpt
Hypee,

I've been able to get basic scripts working, but once you add an import, etc
it would bomb. Not sure of the best (or correct way) to import python code
in Skulpt..

If someone could produce an example in which a library was imported, that
might help..

Brad Miller

unread,
Jun 22, 2011, 5:34:10 PM6/22/11
to sku...@googlegroups.com
Just to be sure... You do know that in order to import a library you need to add the builtin.js script to your source.  It must come after you include skulpt.js

for example:

    <script type="text/javascript" src="_static/skulpt.js"></script>
    <script type="text/javascript" src="_static/builtin.js"></script>

Once you have builtin.js then import should work as usual.

Brad

-- 
Brad Miller
Sent with Sparrow

Hypee

unread,
Jun 22, 2011, 6:23:30 PM6/22/11
to Skulpt
@Billy

Thanks for the quick answer !

In your Skult version, the "/example/Example1.html" seems to correctly
compile the code inside <script type="text/python> (which is already
quite amazing). But unfortunately, it doesn't seems to execute the
generated/compiled javascript code, or am I missing something ?

Might you give some info on how you achieved to make the <script
type="text/python> thing work ? (I mean what files did you modify).

@Brad

Do you mean that it's possible to make an "import myModule" that will
import the "myModule.py" file located in the same folder than
the .html file, without having to create an "/src/lib/myModule" folder
+ "_init_.py" file + recompiling with "m dist" ?

Regards,

Pascal

Hypee

unread,
Jun 22, 2011, 6:39:46 PM6/22/11
to Skulpt
@Billy

Ok, I found your SkulptInit function in init.js.
Will play with it !

On Jun 23, 12:23 am, Hypee <pascal.hy...@gmail.com> wrote:
> @Billy

Billy Earney

unread,
Jun 22, 2011, 7:44:36 PM6/22/11
to sku...@googlegroups.com
Brad,

Look at the init.js script.  It accesses the DOM and reads the contents
between the <script type="text/python"> ... </script> tags, and then
sends it to a skulpt function to parse it, and then execute it.

I've also created a myread function (that overrides read) which uses
XMLHTTPRequest to download the python file..

I'm having some trouble at this point.  ( get a Typeerror: a is
undefined)..   Not sure what is causing this.  I know the file is being
read in properly but after that something is wrong..

comment out the import myTest and it works fine.  uncomment the import
myTest, and I get the error above.  Paste the code from myTest.py inside
the script tags and it works (and parses, and executes) fine.

Does anyone have any suggestions?

Would be cool to get this working, and then to get python to access the
DOM, would be very awesome! :)
script.html
myTest.py
init.js

Billy Earney

unread,
Jun 22, 2011, 8:15:58 PM6/22/11
to sku...@googlegroups.com
I received an email stating that the js file was filtered, so here is a copy in which I've changed the file extension.
init.js.txt

Brad Miller

unread,
Jun 23, 2011, 12:41:59 PM6/23/11
to sku...@googlegroups.com


-- 
Brad Miller
Sent with Sparrow
No, all modules that you import have to be built into the builtin.js file that gets created when you run m dist  or m docbi .   

Brad

Regards,

Pascal

Billy Earney

unread,
Jun 23, 2011, 2:07:33 PM6/23/11
to sku...@googlegroups.com
Brad,

Thanks for the response.  That is sad news.  I was hoping to be able to import py files in the same (or from a different directory).  It seems to me that this could be a possibility.  I'm not sure how much Skulpt would need to be modified to get it to work, but I could see a lot of potential , if this feature was implemented.

Brad Miller

unread,
Jun 23, 2011, 3:41:24 PM6/23/11
to sku...@googlegroups.com
I agree.  In the long run that would be a cool approach.  I think I've learned enough about the guts of skulpt that I could probably do it, but unfortunately its pretty far down on my priority list right now.  I can accomplish everything I need for my current usage of skulpt using the builtin.js approach.

Brad

-- 
Brad Miller
Sent with Sparrow

Billy Earney

unread,
Jun 25, 2011, 9:16:29 AM6/25/11
to sku...@googlegroups.com
Brad,

Can you explain the builtin.js approach?   I've played with the code a little, and I'm failing to figure out how to import a module.

Thanks!

Billy Earney

unread,
Jun 25, 2011, 5:45:59 PM6/25/11
to sku...@googlegroups.com
Brad,

I saw your blog at http://blog.bonelakesoftware.com/2011/03/adding-module-to-skulpt.html which describes some basics of Skulpt.  I'm using your builtinRead function.  I believe skulpt is able to find my module (which is a python module), but I get an error "TypeError: a.tp$setattr is not a function".  Is it possible to create a module named myTest.py and place it in the src/lib directory and have it work?

Here's the test module I'm playing with:

class myTest:
   def __init__(self, c):
       self._c=c

   def run(self, b):
       return self._c+b


I've double checked and the builtin.js file contains the myTest class and I have added builtin.js after skulpt.js in my html file.

Any help or guidance is appreciated..

Thanks!

Billy

Hypee

unread,
Jun 25, 2011, 8:13:14 PM6/25/11
to Skulpt
First, thanks for tips on how to make the <script type="text/python">,
that's now working fine on my side.

For the module, create a folder "/src/lib/test" and create a
"__init.py__" in it with your code.
Compile with "m dist", then, in your .html page, do :

<script type="text/python">
import test
t = test.myTest( 1 )
print t.run( 2 )
</script>

You can also create Javascript modules in the same way, the structure
& communication is a little bit more tricky, but that's the way to go
to be able to communicate between Python/Javascript/DOM.

To allow import of "non compiled modules", we should be able to use
your XHR method. Either to do a dirty "str_replace" of "import
myModule.py" by the content of the module (lets says that adding .py
means it's a non compiled module) or modifying the existing import
function to handle that case.

Regards,

Pascal

On Jun 25, 11:45 pm, Billy Earney <billy.ear...@gmail.com> wrote:
> Brad,
>
> I saw your blog athttp://blog.bonelakesoftware.com/2011/03/adding-module-to-skulpt.htmlwhich
> describes some basics of Skulpt.  I'm using your builtinRead function.  I
> believe skulpt is able to find my module (which is a python module), but I
> get an error "TypeError: a.tp$setattr is not a function".  Is it possible to
> create a module named myTest.py and place it in the src/lib directory and
> have it work?
>
> Here's the test module I'm playing with:
>
> class myTest:
>    def __init__(self, c):
>        self._c=c
>
>    def run(self, b):
>        return self._c+b
>
> I've double checked and the builtin.js file contains the myTest class and I
> have added builtin.js after skulpt.js in my html file.
>
> Any help or guidance is appreciated..
>
> Thanks!
>
> Billy
>
> On Sat, Jun 25, 2011 at 8:16 AM, Billy Earney <billy.ear...@gmail.com>wrote:
>
>
>
>
>
>
>
> > Brad,
>
> > Can you explain the builtin.js approach?   I've played with the code a
> > little, and I'm failing to figure out how to import a module.
>
> > Thanks!
>
> > On Thu, Jun 23, 2011 at 2:41 PM, Brad Miller <bonel...@gmail.com> wrote:
>
> >>  I agree.  In the long run that would be a cool approach.  I think I've
> >> learned enough about the guts of skulpt that I could probably do it, but
> >> unfortunately its pretty far down on my priority list right now.  I can
> >> accomplish everything I need for my current usage of skulpt using the
> >> builtin.js approach.
>
> >> Brad
>
> >> --
> >> Brad Miller
> >> Sent with Sparrow <http://www.sparrowmailapp.com>
>
> >> On Thursday, June 23, 2011 at 1:07 PM, Billy Earney wrote:
>
> >> Brad,
>
> >> Thanks for the response.  That is sad news.  I was hoping to be able to
> >> import py files in the same (or from a different directory).  It seems to me
> >> that this could be a possibility.  I'm not sure how much Skulpt would need
> >> to be modified to get it to work, but I could see a lot of potential , if
> >> this feature was implemented.
>
> >> On Thu, Jun 23, 2011 at 11:41 AM, Brad Miller <bonel...@gmail.com> wrote:
>
> >> --
> >> Brad Miller
> >> Sent with Sparrow <http://www.sparrowmailapp.com>

Brad Miller

unread,
Jun 27, 2011, 5:50:48 PM6/27/11
to sku...@googlegroups.com
Billy,

The way I've tested it is to create a subdirectory under lib and then put my python module code in __init__.py

when you run ./m dist  that will compile the python to javascript and store it in builtin.js.

I'm not sure why you are getting the error below but it sounds like you might be seeing Python in builtin.js which is not good.

Brad

-- 
Brad Miller

Billy Earney

unread,
Jun 28, 2011, 6:36:25 PM6/28/11
to sku...@googlegroups.com
Brad,

Enclosed is my builtin.js file when I do a ./m dist

The python code is being inserted into the builtin.js file,and isn't being compiled into javascript.  Is there a step I should do before ./m dist?

Thanks!
builtin.js.txt

Brad Miller

unread,
Jun 29, 2011, 2:26:04 PM6/29/11
to sku...@googlegroups.com
Billy,

Two things:

1.  I don't know what I was talking about when I said no python in the builtin.js file.  I just double checked how my example worked and there it was.  So thats not the issue.

2.  I took your builtin.js that you sent me and dropped it into my test environment.  I tried the following program:

import myTest

x  = myTest.myTest(2)
print x.run(3)


And it worked just fine.   Now I did this using a textarea and the run script I posted on my blog, not in a <script> tag.  So that would seem to point to the script tag not necessarily anything in skulpt proper.  I haven't looked at the branch that implements <script type="text/python">  so I can't really comment.

Sorry for the mis-information before.


Brad


-- 
Brad Miller

Attachments:
- builtin.js.txt

Billy Earney

unread,
Jun 29, 2011, 2:58:24 PM6/29/11
to sku...@googlegroups.com

No problem.  I’ll look at this a little later today, and hopefully I can get it running.

Billy Earney

unread,
Jun 29, 2011, 8:24:36 PM6/29/11
to sku...@googlegroups.com
Brad,

Thanks for helping me with this.  I believe I have it working, thanks to the example you give below.  Also I believe I have the dynamic import thingee solved as well.  I've created a myExample.py script (and it isn't in the builtin.js file), and was able to import it by using
<script src="myExample.py" type="text/python"></script>

So we could use builtin.js for the regular py files that come with python (random, math, etc), and the above format could be another option of dynamically adding one or more .py files without using the builtin.js technique..

How to reproduce:

save the attachments.

mv dist.7z.txt dist.7z
mv example.7z.txt example
7za x dist.7z
7za x example.7z

cd example
open script.html in a browser and if the page says  Hello World, then 15, and then good bye!.  The dynamic import worked and compiled properly..

There might be a better way of doing this.  When I parse the script tag (which contains the src attribute), I look for and download the file (myExample.py) and then I place it in the builitin['files'] data structure.  After that, skulpt doesn't know if its really part of builtin.js or not.

Like I said, there could be a better way of doing this, but I guess this is a start. :)

Let me know if you have trouble replicating my results..

This could lead to some great things! :)

Billy
dist.7z.txt
example.7z.txt
Reply all
Reply to author
Forward
0 new messages