Mixing Javascript with python

563 views
Skip to first unread message

Carlo Oliveira

unread,
Jan 10, 2013, 5:00:08 AM1/10/13
to bry...@googlegroups.com
Is there a way to mix js with python? This would be good to help in wrapping js libraries for use in Brython. This technique is heavily use in pyjs, where there is the JS function that takes a string that is interpreted as javascript. Names in the scope of the Python calling function are shared with the javascrip code.

Francois Dion

unread,
Jan 10, 2013, 10:33:59 AM1/10/13
to bry...@googlegroups.com
Yes, you can mix js with Python. If it is a function, no doubt you've
noticed you can use it directly, without conversion.

For objects, the JSObject() name is currently under review and might
change, so consider this quite preliminary, but poyol posted the
following on the french list after Pierre implemented the
functionality:

<body onLoad="brython()">
<script src="brython.js"></script>
<script src="http://d3js.org/d3.v3.min.js"></script>
<div id="viz"></div>

<script type="text/javascript">
var sampleSVG = d3.select("#viz")
.append("svg")
.attr("width", 200)
.attr("height", 200);
</script>

<script type="text/python">
ssvg=JSObject(sampleSVG)

for i,color in zip(range(3),['blue','red','yellow']):
ssvg.append("circle").style("stroke", "gray").style("fill",
color).attr("r", (3-i)*10).attr("cx", 50).attr("cy", 50)
</script>
</body>

I think however that last night's build broke this...

Francois
> --
> You received this message because you are subscribed to the Google Groups
> "brython" group.
> To post to this group, send email to bry...@googlegroups.com.
> To unsubscribe from this group, send email to
> brython+u...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/brython/-/DRiFqDGIeCoJ.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Pierre Quentel

unread,
Jan 10, 2013, 6:16:04 PM1/10/13
to bry...@googlegroups.com

I think however that last night's build broke this...

It's fixed in this night's version ; both SVG demos are on line : http://brython.info/gallery/test_svg2.html  and tests_svg3.html

kiko (on pybonacci)

unread,
Jun 12, 2013, 5:05:44 PM6/12/13
to bry...@googlegroups.com


El viernes, 11 de enero de 2013 00:16:04 UTC+1, Pierre Quentel escribió:

I think however that last night's build broke this...

It's fixed in this night's version ; both SVG demos are on line : http://brython.info/gallery/test_svg2.html  and tests_svg3.html

I couldn't find the examples in the gallery.

I tried  this:


<body onLoad="brython()">
<script src="brython.js"></script>
<script src="http://d3js.org/d3.v3.min.js"></script>
 <div id="viz"></div>

<script type="text/javascript">
    var sampleSVG = d3.select("#viz")
        .append("svg")
        .attr("width", 200)
        .attr("height", 200);
</script>

<script type="text/python">
ssvg=JSObject(sampleSVG)

for i,color in zip(range(3),['blue','red','
yellow']):
    ssvg.append("circle").style("stroke", "gray").style("fill",
color).attr("r", (3-i)*10).attr("cx", 50).attr("cy", 50)
</script>
</body>

but I get:
[22:52:49.398] Error: 'NoneType' object has no attribute 'style' @ file:///home/kiko/pyprojs/brython_repo/examples/brython/brython.js:1938

If I do:

_d3 = JSObject(d3)
print(d3.min([0,1,2,3])

It works!!

But if I do, for example:

_d3time = JSObject(d3.time)

I obtain a 'NoneType' (I'm a JS loser).

Has someone more complex and/or sophisticated examples showing how to access jQuery, d3 or others JS libraries full capabilities? I wonder if this is possible right now.

Thanks in advance.
Reply all
Reply to author
Forward
0 new messages