Re: Brython Issue

55 views
Skip to first unread message

Jonathan Verner

unread,
Dec 11, 2017, 5:45:56 PM12/11/17
to bry...@googlegroups.com, booko...@gmail.com
Hi,

in the future, I suggest you post your messages to the Brython googlegroup as you will have a higher chance of getting a reply.

> If you can push me towards a direction, I would appreciate it tremendously.

See

http://www.brython.info/static_doc/en/javascript.html
http://www.brython.info/static_doc/en/jsobjects.html

If you'd like to integrate the js library in a more pythonic way (e.g. make it importable from python code without having to access it
through browser.window), you are currently on your own. Try looking at the files in

https://github.com/brython-dev/brython/tree/master/www/src/libs

which are examples of importable modules written in javascript. You would have to include the js library in your html and then implement
a wrapper around it along the lines of the above files, e.g. js_lib.js. The library would then be accessible via import js_lib.

Cheers,

Jonathan

--
Jonathan Verner, PhD
Department of Logic, Charles University
Palachovo nám. 2, 116 38 Praha 1
Czech Republic

On pondělí 11. prosince 2017 22:08:09 CET you wrote:
> Hello,
>
> I am writing to kindly ask you something.
> I have high aims for Brython but I cannot find anywhere on its site stated
> how someone could import a JavaScript library and use it with Brythonic
> code.
> If you can push me towards a direction, I would appreciate it tremendously.
>
> Thank you so much.
>

Jonathan Verner

unread,
Dec 13, 2017, 3:45:25 AM12/13/17
to bry...@googlegroups.com, booko...@gmail.com
> Say I want to pack jQuery so that I can do import jQuery - there is no
> straightforward way to do that ?

Well, there is much less need for jQuery in Brython. JQuery was built to overcome incompatibilities between browsers (happily not as relevant today) and to provide a better interface to the dom (Brython provides a nice pythonic interface, so this point is also not as relevant).

> Wouldn't it be an essential thing for Brython to provide guidelines for if
> we really want to bring Python to the client side ?

I think it would be nice to have this, but so far, nobody has found the time to write one. Perhaps you can look at the code I pointed you to, experiment with it a little, and start writing a guideline. We would be very happy to help you on the way...

Cheers,

Jonathan

--
Jonathan Verner, PhD
Department of Logic, Charles University
Palachovo nám. 2, 116 38 Praha 1
Czech Republic

On úterý 12. prosince 2017 19:09:34 CET booko...@gmail.com wrote:
> Thanks for the response,
>
> Say I want to pack jQuery so that I can do import jQuery - there is no
> straightforward way to do that ?
>
> Wouldn't it be an essential thing for Brython to provide guidelines for if
> we really want to bring Python to the client side ?

Pierre Quentel

unread,
Dec 13, 2017, 4:23:08 AM12/13/17
to brython
The goal of Brython is to develop client-side code without Javascript, so as Jonathan
says there should be less needs for Javascript libraries, especially jQuery.

If you want to use it without wrapping each library inside a specific Brython
module, the documentation shows how to do it, you probably saw the example with jQuery.

There is however a way to create Python modules that wrap Javascript libraries. This
technique is not documented, but used in the built-in Brython package jqueryui. Applied
to jQuery, you can write a jquery module like this :

from browser import window, load

load("/path/to/jquery.min.js")
jq = window.jQuery


The function browser.load(url) makes a blocking Ajax call to load the library into
the HTML document. When the library has been loaded, the global Javascript namespace,
referenced in Brython by browser.window, has an attribute jQuery.

You can then use the module like in this example :

<script type="text/python">
from jquery import jq

jq("#test").text("I can use jQuery here !")
</script>

<div id="test"></div>
</body>




Andreas

unread,
Dec 13, 2017, 8:13:12 AM12/13/17
to brython
Thank you so much for the response Pierre - I appreciate it tremendously.

How would I do something like this doable though? :

<script type="text/python">
import jquery

$("#test").text("I can use jQuery here !")

</script>

<div id="test"></div>
</body>

Pierre Quentel

unread,
Dec 13, 2017, 8:48:23 AM12/13/17
to brython


Le mercredi 13 décembre 2017 14:13:12 UTC+1, Andreas a écrit :
Thank you so much for the response Pierre - I appreciate it tremendously.

How would I do something like this doable though? :

<script type="text/python">
import jquery

$("#test").text("I can use jQuery here !")
</script>

<div id="test"></div>
</body>


No, Brython is an implementation of Python and in Python, $ is not a valid identifier.

Andreas

unread,
Dec 13, 2017, 9:24:26 AM12/13/17
to brython
Thank you again Pierre. Ok I understand, what about the part of doing just "import jquery" ? (no from)

Pierre Quentel

unread,
Dec 13, 2017, 9:54:03 AM12/13/17
to brython


Le mercredi 13 décembre 2017 15:24:26 UTC+1, Andreas a écrit :
Thank you again Pierre. Ok I understand, what about the part of doing just "import jquery" ? (no from)

Of course, you get the same result with

import jquery

jquery
.jq("#test").text("I can use jQuery here !")



Andreas

unread,
Dec 13, 2017, 1:21:18 PM12/13/17
to brython
Awesome ! Thanks again. 

Pierre Quentel

unread,
Dec 13, 2017, 3:10:03 PM12/13/17
to brython


Le mercredi 13 décembre 2017 19:21:18 UTC+1, Andreas a écrit :
Awesome ! Thanks again. 

Thank you Andreas, you raised an interesting point. I will add it in the documentation.

Andreas

unread,
Dec 14, 2017, 5:44:45 AM12/14/17
to brython
My pleasure. Would it make sense for Brython to automatically do what you described ? For instance, if you type import jquery, brython creates a module like the following without you having to do it manually:
from browser import window, load

load("/path/to/jquery.min.js")
jq = window.jQuery

Kiko

unread,
Dec 14, 2017, 9:29:22 AM12/14/17
to bry...@googlegroups.com
2017-12-14 11:44 GMT+01:00 Andreas <booko...@gmail.com>:
My pleasure. Would it make sense for Brython to automatically do what you described ? For instance, if you type import jquery, brython creates a module like the following without you having to do it manually:
from browser import window, load

load("/path/to/jquery.min.js")
jq = window.jQuery

It is not so easy, Some cases could work out of the box but it is far to be the general pattern.

Where is supposed the js library is located?, on a cdn?, locally?
Which version of the library?, the latest? one specific version that works with your code?
Which global names should be loaded? Do we know these global names? Should be named similarly in Brython?
...



On Wednesday, December 13, 2017 at 10:10:03 PM UTC+2, Pierre Quentel wrote:


Le mercredi 13 décembre 2017 19:21:18 UTC+1, Andreas a écrit :
Awesome ! Thanks again. 

Thank you Andreas, you raised an interesting point. I will add it in the documentation.

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+unsubscribe@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/e065830d-6da6-4e71-a995-44debb5f61b4%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Andreas

unread,
Dec 14, 2017, 10:04:52 AM12/14/17
to brython
What about creating something like pypi, a directory for JS library wrappers ? I fully respect what Pierre said that the goal is to work without JS but if the programmer is not exposed to any JS syntax, it should be ok as those JS shortcuts will be very useful.


On Thursday, December 14, 2017 at 4:29:22 PM UTC+2, kiko (on pybonacci) wrote:
2017-12-14 11:44 GMT+01:00 Andreas <booko...@gmail.com>:
My pleasure. Would it make sense for Brython to automatically do what you described ? For instance, if you type import jquery, brython creates a module like the following without you having to do it manually:
from browser import window, load

load("/path/to/jquery.min.js")
jq = window.jQuery

It is not so easy, Some cases could work out of the box but it is far to be the general pattern.

Where is supposed the js library is located?, on a cdn?, locally?
Which version of the library?, the latest? one specific version that works with your code?
Which global names should be loaded? Do we know these global names? Should be named similarly in Brython?
...



On Wednesday, December 13, 2017 at 10:10:03 PM UTC+2, Pierre Quentel wrote:


Le mercredi 13 décembre 2017 19:21:18 UTC+1, Andreas a écrit :
Awesome ! Thanks again. 

Thank you Andreas, you raised an interesting point. I will add it in the documentation.

--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.

To post to this group, send email to bry...@googlegroups.com.

kikocorreoso

unread,
Dec 14, 2017, 10:35:02 AM12/14/17
to bry...@googlegroups.com, Andreas

On 14/12/17 16:04, Andreas wrote:
What about creating something like pypi, a directory for JS library wrappers ? I fully respect what Pierre said that the goal is to work without JS but if the programmer is not exposed to any JS syntax, it should be ok as those JS shortcuts will be very useful.
You already could do so. Just share your snippets with others on the list, on the wiki (https://github.com/brython-dev/brython/wiki),...

Brython is great but is not so mainstream, there are a lot of work to be done and there is a lot of effort involved but with the actual number of people committing is not enough. You are more than welcome to try to alleviate this :-D

OTOH, I don't want 4000 packages with 4 lines of code when it is easier to obtain this functionality by yourself and with the behavior you need. There are several examples on the gallery: http://brython.info/gallery/gallery_en.html?lang=en

Andreas

unread,
Dec 14, 2017, 10:39:34 AM12/14/17
to brython
Oh, where is the section with the already existing snippets on the wiki ? Don't see it.

Andreas

unread,
Dec 14, 2017, 10:40:57 AM12/14/17
to brython
Oh I see a packages page by the way, that's nice.
Reply all
Reply to author
Forward
0 new messages