To view this discussion on the web visit https://groups.google.com/d/msgid/brython/CAB-sx62-JRR_LKOSfNiSEYi3zFOVVsfFgeDfyCGtvjh1f%2B2sLg%40mail.gmail.com.--
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.
Thanks,I am going to concentrate on this after publishing the next versionbut it's a minor drawback compared to the present situationKiko,You are right, of course. I have been thinking of this issue for some time, this is why I introduced the object __BRYTHON__, but its use is very limited so far. The only drawback is that functions defined in the Brython code will no longer be available directly from Javascript, for instance the first example on the home page with the tag
<button onclick="echo()">
will have to be written something like
<button onclick="__BRYTHON__.echo()">
Pierre
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/CA%2BgUBLRuKy35GF1h4XRkWwmNC%3DqZJitFH68i5nSZkP6tT3YKYQ%40mail.gmail.com.
Pierre,Could we take approach jquery and other javascript libraries use?
Instead of typing jQuery("blah"), they do $("blah").
So for the above, couldn't we write a wrapper $("echo()") that would would be translated to something like __BRYTHON__.echo()? This shortcut seems to have gained acceptance in the javascript community for many popular javascript libraries.
On Mon, Dec 23, 2013 at 9:09 PM, Olemis Lang <ole...@gmail.com> wrote:Hi,
>
> that will be a problem when trying to integrate brython with other libs
> already defining $ . So (IMO) should be combined with adoption and support
> of AMD
Knowing that Brython code is contained in text/python3 and compiled before
exposing things, would this really be a problem?
On Mon, Dec 23, 2013 at 9:09 PM, Olemis Lang <ole...@gmail.com> wrote:
>
> that will be a problem when trying to integrate brython with other libs
> already defining $ . So (IMO) should be combined with adoption and support
> of AMD
Hi,
Knowing that Brython code is contained in text/python3 and compiled before
exposing things, would this really be a problem?
- put all Python built-in names as attributes of a single Javascript object __builtins__ (hopefully no Javascript library uses this name) : names such as object, str, list etc. are not defined in the global Javascript namespace, so don't conflict with other libraries
- similarly, put Brython-specific objects (such as DOMNode, JSObject etc.) as attributes of the object __BRYTHON__
- each Brython script is run inside an anonymous function ; at the beginning of the function, all the attributes of __builtins__ are introduced in the function namespace by something like
for(var $pyattr in __builtins__){eval("var "+$pyattr+"=__buiiltins__[$pyattr]")
so that the Javascript code generated by Brython can use str, list, object etc., but these names still remain out of the global Javascript namespace
For the names defined inside the Brython script, things could simply remain as they are today : set them as attributes of window, so that they are available from Javascript. A function defined by
def echo():
print('hello')
could still be called natively from the HTML tag
<button onclick="echo()">
With this option, it is the responsability of the developer not to define names that conflict with the Javascript libraries or Python module he uses ; for instance, avoid defining THREE if he uses the three.js library - this is what he would also have to do if he developed in Javascript
What do you think of that ?
- Pierre
- each Brython script is run inside an anonymous function ; at the beginning of the function, all the attributes of __builtins__ are introduced in the function namespace by something like
for(var $pyattr in __builtins__){eval("var "+$pyattr+"=__buiiltins__[$pyattr]")
so that the Javascript code generated by Brython can use str, list, object etc., but these names still remain out of the global Javascript namespace
For the names defined inside the Brython script, things could simply remain as they are today : set them as attributes of window, so that they are available from Javascript. A function defined by
def echo():
print('hello')
could still be called natively from the HTML tag
<button onclick="echo()">
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/CAGMZAuPPGWQNOJWA2RXZj0akpD40Gzvmz_azWJ%2B0a8S4yUhjjA%40mail.gmail.com.
On 12/23/2013 04:59 PM, Olemis Lang wrote:
>
> it'd be nice to be able to do something like <button onclick="brython:echo()" />
Yes, I think Olemis has a good idea. Perhaps a little more like this-
<button onclick="__BRYTHONENV__:echo()" />
And to add Consistency, replacing __builtins__ with __BUILTINS__ (or
__BBUILTINS__ for brython builtins)
Sepero
If I may ... the first thing I consider that may be (improved |
changed) regarding that syntax is that
1. it is still javascript, and thereby it's not possible to insert
python syntax e.g. onclick="print(x for x in some_list)" <= nor even
anything close to that . It's not actually something *wrong* , it's
nice to have .
2. brython names might clash with other js names which have to be
global in order to be referenced inline; therefore it'd be nice to
separate both js & brython vars namespaces
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/96829406-d13a-4270-9a1d-bda1bb2049ee%40googlegroups.com.
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/6b56edda-13d3-4702-9100-3db1d23c2c5c%40googlegroups.com.
Le 27 déc. 2013 16:07, "Billy Earney" <billy....@gmail.com> wrote:
> ...
> @expose
> ...
> What do others think?
That this thread contains a whole bunch of brilliant ideas (including this one), and that this is great team work.
chri
On Dec 27, 2013 11:45 AM, "Christophe Gragnic" <christop...@gmail.com> wrote:
>
> Le 27 déc. 2013 16:07, "Billy Earney" <billy....@gmail.com> wrote:
> > ...
> > @expose
> > ...
> > What do others think?
Cool, that's part of the solution, where (module) would it be defined ?
>
> That this thread contains a whole bunch of brilliant ideas (including this one), and that this is great team work.
>
<joke> It's the side effects of the wine we drink on Christmas </joke>
Sent from Android
--
Regards
Olemis - @olemislc
Blog-ES : http://simelo-es.blogspot.com
Blog-EN : http://simelo-en.blogspot.com
Projects : http://blood-hound.net
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/CAGMZAuNsUWrtHERjvkx%3DKV2kLZ2JeUMQ73ps2gSc7pMWFBfy_A%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/CAB1ii-e%2B1FGMMNm6XTUQ_PG7ACNWtmdsmbt2vJGEz8pTbjsNNQ%40mail.gmail.com.
So when reusing __all__ for this purpose, when a brython script imports a module, will __all__ of that module's public names be exposed to javascript?
But that potential interaction, points out an insufficiency in the >> decorator idea too... you wouldn't want to edit module sources to add the >> decorator to those module methods that you want to expose to javascript,I do not get it . The very same nature of decorators implies that they are functions so may be called anywhere with any number of arguments . What stops users from executing in __main__ (i.e. inline <script /> code in target web page ;) `expose(mypackage.mymodule.myfunc)` ?
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/52BE94B7.6030705%40g.nevcal.com.
Thanks for all the comments/suggestions. IMO, decorators is still the best way to implement this. This is similar to how cherrypy exposes elements: http://cherrypy.readthedocs.org/en/latest/tutorial/exposing.html This seems to work well.
Billy
--
On Sat, Dec 28, 2013 at 3:07 AM, Glenn Linderman <v+py...@g.nevcal.com> wrote:
Apologies. I forgot decorators could be invoked that way.On 12/28/2013 12:51 AM, Olemis Lang wrote:
But that potential interaction, points out an insufficiency in the >> decorator idea too... you wouldn't want to edit module sources to add the >> decorator to those module methods that you want to expose to javascript,I do not get it . The very same nature of decorators implies that they are functions so may be called anywhere with any number of arguments . What stops users from executing in __main__ (i.e. inline <script /> code in target web page ;) `expose(mypackage.mymodule.myfunc)` ?
--To view this discussion on the web visit https://groups.google.com/d/msgid/brython/52BE94B7.6030705%40g.nevcal.com.
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.
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/CAB1ii-epMTUrV10WVAhG%2B-Ft4crygQXm-Wed_gNTyKMCc_OqwA%40mail.gmail.com.
Like I said previously, I think the most important thing is naming consistency. An example of consistent naming-
__builtins__
__brython__
__brythonenv__
Also, on the topic of export decorator, I think it may be a good idea, but it seems not very relevant at this time. What I mean is, export doesn't seem to be a solution to the problem, but rather, a possible compliment to the solution.
The solution is to figure out the best way to clean global namespace, and after that problem is solved we can possibly use "export" to bring stuff back in.
--- Original Message ---
--
Regards,
Olemis - @olemislc
Featured article:
with this Brython script
-- 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.To view this discussion on the web visit https://groups.google.com/d/msgid/brython/8278b79f-2f47-4a22-ae61-0b0e8c7c2b4f%40googlegroups.com.For more options, visit https://groups.google.com/groups/opt_out.
If we use __all__ or the underscore, it will only apply to the main module (the one that is inside a <script type="text/python3"> tag), not to the imported modules. If there are several main modules in a page, then all the names in __all__, or those that don't start with an underscore, will be exposed, I don't see a problem here
I'm currently translating a js library to Brython.�
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/8278b79f-2f47-4a22-ae61-0b0e8c7c2b4f%40googlegroups.com.
With the standard Python process, import foo executes the module foo. The decorator will expose the name "bar" to the global Javascript namespace, with the side effect that it will also be available in the Python script, overriding the value of bar in this script. Very confusing, isn't it ? and it's even worse if foo1 imports foo2 that imports foo3 where a name "bar" is exposed...
To be clear about the problem-
We need to clean the global namespace of JS. To do this, we need to put the currently global variables into some global objects.
Does anyone disagree with this??? Reread again. We need to be clear on exactly what the problem is right now.
An "export" decorator may be a great idea, but it can be implemented after JS namespace is cleaned. Talk about "export" decorator at this time seems secondary and possibly distracting from the problem at hand.
I think the thing to discuss is:
1. How many new JS global variables are going to be needed
2. What will each of them contain
3. And what will they be named
If anyone thinks I'm mistaken here, please correct me. Hopefully, this can help the discussion move forward.
Talk about "export" decorator at this time seems secondary and possibly distracting from the problem at hand.
I can see Billy's point here, but from my perspective, this dialog appears to be going in multiple directions at once.
To be clear about the problem-
We need to clean the global namespace of JS. To do this, we need to put the currently global variables into some global objects.
Does anyone disagree with this??? Reread again. We need to be clear on exactly what the problem is right now.
An "export" decorator may be a great idea, but it can be implemented after JS namespace is cleaned. Talk about "export" decorator at this time seems secondary and possibly distracting from the problem at hand.
I think the thing to discuss is:
1. How many new JS global variables are going to be needed
2. What will each of them contain
3. And what will they be named
If anyone thinks I'm mistaken here, please correct me. Hopefully, this can help the discussion move forward.
For example- Without "export", we have to fix things by editing the JS code. With "export", we can fix things by editing the PY code.
So either way, code will be broken. Adding "export" decorator doesn't fix the breakage that will occur. So I still contend that "export" is something to think about after we solve the global namespace problem.
It may be possible to mitigate breakage for a limited time by keeping everything in global namespace, but I would avoid keeping things that way for a prolonged period of time. It could lead to difficult to trace errors.
--- Original Message ---
From: "Glenn Linderman" <v+py...@g.nevcal.com>
Sent: December 29, 2013 5:03 PM
To: bry...@googlegroups.com
Subject: Re: Global variables and namespace
On 12/29/2013 1:29 PM, sepe...@gmail.com wrote:
Talk about "export" decorator at this time seems secondary and possibly distracting from the problem at hand.
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/CAB-sx62xfu9LrBov20dCSP01pJEztcW0ooUqQjK0vFQn5z19aA%40mail.gmail.com.
Ok, the solution to this problem will break existing code. I agree that's an excellent point. The thing is, an "export" decorator doesn't solve the problem of broken code, it simply moves the breakage from the JS code to the PY code. For example- Without "export", we have to fix things by editing the JS code. With "export", we can fix things by editing the PY code. So either way, code will be broken.
Adding "export" decorator doesn't fix the breakage that will occur. So I still contend that "export" is something to think about after we solve the global namespace problem. It may be possible to mitigate breakage for a limited time by keeping everything in global namespace, but I would avoid keeping things that way for a prolonged period of time. It could lead to difficult to trace errors.
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/98f64e61-3a36-4777-bf91-c1514b7948cc%40googlegroups.com.
Some of the factors that concern me:
1. What are the merits of using an expose decorator vs global variable reference? I do not know if switching to use "expose" is the best solution, but I have no compelling argument either way. How does everyone else feel- Anyone prefer using global js variable reference? If so, why?
2. Perhaps we could imagine a better decorator name than "expose"? Anyone have suggestions?
3. What will we call the new global js variable that encapsulates Brython code ($py, __builtins__)? Any other suggestions?
Obviously, I think a very important factor is naming consistency. What are the names of other global variables that Brython already creates in js?
I'm in ill health right now, so hHopefully that all makes sense,
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/2123813665.26072.1388456210207.JavaMail.seven%40ap03.prod.7sys.net.
Also, you mention allowing more than one way to access python attributes from Javascript. It seems impossible (impractical) to prevent accessing the global js object. If the final conclusion is to go with a decorator, then I agree, it would be preferable that the global js object should be- easily accessible (if needed for some reason).
But at the same time, if choosing to use decorator, I will be 100% on board with promoting that preferred way of coding. It is Pythonic for there to be 1 right way to do a thing.
If deciding on using "export" decorator, I would say that a $py variable is not necessary. A syntax like __BRYTHON__.scope[X].__dict__[Y] appears sufficient, without needing to create any other variables. It is not brief, but it is easy to understand. It is not complex, and it is also very explicit, both are virtues of Python.
Lastly, I want to try to elaborate on naming, and why I believe it matters a lot. When a 3rd party developer works with software they have to know their naming "safe zone". Different js libraries have different conventions. A developer cannot be bothered to remember every variable name every library uses. If I find out Brython uses both "__builtins__" and "__BRYTHON__", what I see is that they are possibly using everything "__xxxx__" and "__XXXX__", and I am limiting the variable names I create based on that information. Also as a 3rd party dev, if I ever by chance need to use/access these Brython variable names, I'm more inclined to make typo errors. Making then differently up/low-cased causes unnecessary confusion for the 3rd party dev. It does not help. So basically, this is a plea to not cause the 3rd party dev to have to think more than necessary. Choose one single naming convention (whether __xxxx__ or __XXXX__ or $xxxx), and try to stick to using only that convention for everything. It is for similar reasons that Python 3 has changed all stdlibs to lower case. Make things brain dead simple for those who use your code.
You say "__BRYTHON__ is only used internally and in the standard library, it's not expected to be used in Brython programs". I have to say that 3rd party devs will not care about this. If on rare chance they ever need to dig into a Brython global variable, the naming difference of "__builtins__" and "__brython__" will be enough, and less likely to lead to typos.
My apologies for such a long message. :)
eval(f.__name__ + "= f")function expose(f) {where expose is defined in javascript as something likeFor example:Pierre,Could we expose certain functions to a javascript global by creating a special decorator?
I think you are making a good call here.
@exposedef echo(event):
## do neat stuff here..
}
// the expose function above probably isn't correct, but what it should do is take a function defined in brython and make it a global in javascript.
What do others think?
Billy
On Fri, Dec 27, 2013 at 12:50 AM, Pierre Quentel <pierre....@gmail.com> wrote:
The more I think of it, the more I am convinced that the solution is not to expose Brython namespace to Javascript programs at all, except for Brython functions defined at module level in the __main__ program, so that they can react to calls in event attributes (onclick="foo()")
The reason is that in most cases, it is not possible to create Javascript objects that can be used to interact with the matching Brython variable (at least with a simple syntax). For instance, suppose you have defined a dictionary in Brython with
br_dict = {'a':1}
and you want to add another item from a Javascript program. For this, in the Javascript program, you would like something like
js_dict = $py(br_dict) // or $py.br_dict, or $py("br_dict")...
js_dict['b']=2
Well, there is no such object, because in order to set an item to br_dict, the Javascript code is (once the builtin name __builtins__ will be introduced)
__builtins__.getattr(br_dict,'__setitem__')('b',2)
Providing a conversion function would work in some limited cases, but fail in most cases without any useful error message, or require a very complicated syntax. It's better to give a simple message : Brython variables must be dealt with only in Brython programs
If the only remaining use case is function calls inside event attributes, then since Brython functions are Javascript functions, they can be exposed in the global Javascript namespace. The risk of conflict with Javascript names exists and must be documented, but is not high enough to introduce a specific syntax
- Pierre
As for naming consistency :
- __builtins__ for the dictionary holding Python builtins is mandatory because it's how it's called in Python
- Brython also exposes the function brython(), used on page load
- __BRYTHON__ is only used internally and in the standard library, it's not expected to be used in Brython programs ; having a different look (all uppercase) helps to indicates that it has a different use
The syntax is ugly (it was not intended to be used in Javascript programs) but it wouldn't be difficult to set a global variable ($py or any other name) to __BRYTHON__.scope['__main__'].__dict__ so that the tag can be written
<button onclick="$py.echo()">
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/52C33631.1060707%40g.nevcal.com.
@expose
def echo():
Gives us this function call $py.echo()
I would give that a +1
If it wasn't to troublesome for Pierre and it doesn't risk internal conflict, then I would also be for putting __builtins__ inside __BRYTHON__. +1
I would also give support to @browser if the Brython import module wasn't already named "browser". For that reason, I have to give @browser a -1
In the end, I still give the name @expose +1
--- Original Message ---
From: "Billy Earney" <billy....@gmail.com>
Sent: December 31, 2013 5:45 PM
To: bry...@googlegroups.com
Subject: Re: Global variables and namespace
Glenn,
Billy
-- 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.To view this discussion on the web visit https://groups.google.com/d/msgid/brython/CAB1ii-cLc8PzbsnZP5SCudUH6nnAbb83Ns3jPvtrwK9kcxrfQw%40mail.gmail.com.For more options, visit https://groups.google.com/groups/opt_out.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/607813780.27381.1388537089956.JavaMail.seven%40ap03.prod.7sys.net.
Glenn,<button onclick="echo" /> and <button onclick="$py.echo" /> refer to two different functions. The first a javascript function, and the second a python function. When using a decorator alone, what does <button onclick="echo" /> really mean?
+1 I agree. I think using $py to access a python function from javascript is a good idea. That way, there won't be any confusion when there is a javascript function "echo" and a python function called "echo".
So Billy are you saying that this would be how it could work?@expose
def echo():Gives us this function call $py.echo()
I would give that a +1
If it wasn't to troublesome for Pierre and it doesn't risk internal conflict, then I would also be for putting __builtins__ inside __BRYTHON__. +1
--- Original Message ---
From: "Pierre Quentel" <pierre....@gmail.com>
Sent: January 1, 2014 5:19 AM
To: bry...@googlegroups.com
Subject: Re: Global variables and namespace
Le mardi 31 décembre 2013 23:45:34 UTC+1, Billy Earney a écrit :
Glenn,
+1 I agree. I think using $py to access a python function from javascript is a good idea. That way, there won't be any confusion when there is a javascript function "echo" and a python function called "echo".
<button onclick="echo" /> and <button onclick="$py.echo" /> refer to two different functions. The first a javascript function, and the second a python function. When using a decorator alone, what does <button onclick="echo" /> really mean?
I am not in favour of forcing the use of a prefix for Python functions. Brython is here to show that Python can be an alternative to Javascript for web browser development, and the DOM is language-independant. Having to write "$py.echo" for Python functions (even when no Javascript library is loaded in the page), and just "echo" for Javascript functions, gives the impression that Javascript is the default language
Of course the name could be something different from $py, but I believe it should be short and easy to type :)
Billy
-- 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.To view this discussion on the web visit https://groups.google.com/d/msgid/brython/4d755bc1-401f-463e-8566-cf7f56c2a14a%40googlegroups.com.For more options, visit https://groups.google.com/groups/opt_out.
<button onclick="echo" /> and <button onclick="$py.echo" /> refer to two different functions. The first a javascript function, and the second a python function. When using a decorator alone, what does <button onclick="echo" /> really mean?
I am not in favour of forcing the use of a prefix for Python functions. Brython is here to show that Python can be an alternative to Javascript for web browser development, and the DOM is language-independant. Having to write "$py.echo" for Python functions (even when no Javascript library is loaded in the page), and just "echo" for Javascript functions, gives the impression that Javascript is the default language
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/CAGMZAuPg%2BUgqi1RonRceMNvWjD6Rfvd02ACArg62x%3DsRxPPx-g%40mail.gmail.com.
For that reason it makes sense to me to avoid packing everything into a $py variable. To me that would give the appearance of making Brython a second class citizen to js.
With an export decorator, the dev can choose to place 1 or 100 functions into the js namespace. So if js namespace becomes flooded, it is their own fault.
Though, there is still a conflict for me.
I think the export decorator is a good solution for placing functions into js global namespace.
BUT, an export collision can happen if exporting the function "echo()" from 2 separate python namespaces.
# in __main__
from brython import export
import mymodule
@export
def echo():
print("This is __main__")
# in mymodule
from brython import export
@export
def echo():
print("This is mymodule")
A possible solution might be to have the export decorator restricted to only working with functions in __main__ namespace? Or when names collide, report a warning and give precedence to __main__ functions?
Js is a single namespace language, while python is multi-namespace language, and that makes the conversion a bit tricky. For now, I do not know what is the best path. I tend to lean in favor of restricting export decorator to the __main__ namespace. I wonder what everyone else thinks?
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/230017670.31215.1388737685265.JavaMail.seven%40ap03.prod.7sys.net.
I think I agree with Pierre, that the vision of Brython is to be a 100% alternative to js.
For that reason it makes sense to me to avoid packing everything into a $py variable. To me that would give the appearance of making Brython a second class citizen to js.
With an export decorator, the dev can choose to place 1 or 100 functions into the js namespace. So if js namespace becomes flooded, it is their own fault.
Though, there is still a conflict for me. I think the export decorator is a good solution for placing functions into js global namespace. BUT, an export collision can happen if exporting the function "echo()" from 2 separate python namespaces.
A possible solution might be to have the export decorator restricted to only working with functions in __main__ namespace? Or when names collide, report a warning and give precedence to __main__ functions?
Js is a single namespace language, while python is multi-namespace language, and that makes the conversion a bit tricky. For now, I do not know what is the best path. I tend to lean in favor of restricting export decorator to the __main__ namespace. I wonder what everyone else thinks?
Billyhow about functions exposed from __main__ are put in the "global" namespace directly, and the echo from mymodule would be accessed from javascript via mymodule.echo?<button click="mymodule.echo"/>
I think the possibility of using a $py variable is worth discussing further.
Why not use a decorator with an argument giving the js name to expose to ?
class B() :
@expose("objB_myfunc")
def myfunc(self):
... or maybe we do not needed at all should we find a way (e.g. proposed vars() function ) to retrieve in js code any variable bound to the namespace of brython's __main__ module
This means that from a Javascript program, you can access any object Y in any module X as
__BRYTHON__.scope[X].__dict__[Y]
On 1/3/14, Olemis Lang <ole...@gmail.com> wrote:I've stepped back for a while and see how discussion is running in circles over and over .So for the sake of moving forward with this subject and considering that we have been throwing many ideas on the table by now , my suggestion is to call upon a vote
Though we have to admit, the benefit of vars() is that it would result in no breakage in the future IF Brython became natively supported in the browser. On the other hand, something like "_py" absolutely would cause breakage in such a future event. If someone had this in their code onclick="_py._py()" there is no way to deprecate such usage, because there is no way syntactically separate these two statememts:
_py._py() # Old code that is a _py function in the _py js variable.
_py._py() # New code that is a _py function in a _py object.
Therefore removing the _py variable would need to be done with a hard break, and it would make ALL existing code backward INcompatible. That is not what people will want, and it will prevent Brython from becoming natively supported in the browser.
On 01/05/2014 06:46 PM, Glenn Linderman wrote:
On 1/5/2014 8:16 AM, sepe...@gmail.com wrote:
Though we have to admit, the benefit of vars() is that it would result in no
breakage in the future IF Brython became natively supported in the browser. On
the other hand, something like "_py" absolutely would cause breakage in such a
future event. If someone had this in their code onclick="_py._py()" there
is no way to deprecate such usage, because there is no way syntactically
separate these two statememts:
_py._py() # Old code that is a _py function in the _py js variable.
_py._py() # New code that is a _py function in a _py object.
Therefore removing the _py variable would need to be done with a hard break,
and it would make ALL existing code backward INcompatible. That is not what
people will want, and it will prevent Brython from becoming natively supported
in the browser.
Your example:
onclick="_py._py()"
That looks like valid javascript syntax, invoking a function in the javascript
_py namespace called _py().
That looks like valid python syntax, invoking a function in the python _py
module called _py().
That's correct.
We must realize that If Brython became natively supported in the browser, then use of a _py prefix would become obsolete. We would no longer need the _py prefix. Something that used to be "_py.echo()" now becomes just "echo()". This destroys backward compatibility. Can you see why?
I'll try to explain. If we try to keep the prefix _py backward compatible into a future native Brython, we would need to introduce a _py variable into the Brython __main__ environment. (So we could still use that old html code "_py.echo()" )
If the developer is already using a variable named _py ,then we have broken their code by bring in a new variable of the same name. To use _py prefix in a backward compatible way, we would need to prohibit use of the var name _py in Brython code.
To use _py and prohibit its use in Brython code might be the cleanest and simplest way to proceed, while maintaining backward compatibility. Just perhaps it might be a worthwhile way to go?
Now that this issue is solved, and that the only name exposed by Brython by default is going to be __BRYTHON__ (very unlikely to be used in a JS program), all that is left is, how do we call Brython functions as callbacks from inline Javascript events ?
I think it's a minor problem, since we could simply ignore it by binding functions to events with
elt.bind('click',echo)
We can't use vars() either, because it is not defined in the Javascript namespace (it would be __BRYTHON__.__builtins__.vars())
I like the solution with "expose" because it gives control to the developer
which is symmetric to elt.bind('click',echo). By the way, I don't think we need to pass an additional argument if we want an alias, nor need to prepend the module name as suggested by Billy : since "expose" is a function, we can use it as such in the main program
from javascript import expose
import X # X has a function "func" that we want to expose
echo = expose(X.func)
To avoid errors difficult to track, the solution with "expose" can be improved by allowing only functions defined in the module __main__ to be exposed
With this option, yes, there is a risk that another Javascript or Python program in the same page defines another function echo(). But it's also the case if all the code is written in Javascript, or if it's all in Python : using a library requires the effort to learn its API
I see the links, but perhaps you could elaborate on this more? I assume most of us are not very familiar with Kendo UI.
On 01/06/2014 03:30 PM, Olemis Lang wrote:
Regarding intrinsic events , afaict for the moment there's no much hope with
defining intrinsic events using python syntax in on* element's attributes . At
least I can not find any APIs .
So there's still a chance to :
1. intercept data:text/python[3] [6]_ URLs in href definitions
* as an equivalent to javascript: URI schema [1]_
2. support data-click attribute
* e.g. similar to Kendo UI [2]_
3. support data-bind attribute
* e.g. similar to Kendo UI [3]_ , knockout [4]_ et al.
That was his purpose in the example. He was saying 2 things.
from javascript import expose
import X # X has a function "func" that we want to expose
echo = expose(X.func)
To avoid errors difficult to track, the solution with "expose" can be
improved by allowing only functions defined in the module __main__ to be exposed
I do not get it . In your sample code you have actually exposed a function
defined in a module .
1. @expose is only for __main__
2. If you want to @expose something from a module, it can be done in the __main__ namespace.
On Mon, Jan 6, 2014 at 9:45 AM, Pierre Quentel <pierre....@gmail.com> wrote:[...]Now that this issue is solved, and that the only name exposed by Brython by default is going to be __BRYTHON__ (very unlikely to be used in a JS program), all that is left is, how do we call Brython functions as callbacks from inline Javascript events ?
Regarding intrinsic events , afaict for the moment there's no much hope with defining intrinsic events using python syntax in on* element's attributes . At least I can not find any APIs .So there's still a chance to :1. intercept data:text/python[3] [6]_ URLs in href definitions* as an equivalent to javascript: URI schema [1]_2. support data-click attribute* e.g. similar to Kendo UI [2]_3. support data-bind attribute* e.g. similar to Kendo UI [3]_ , knockout [4]_ et al.I think it's a minor problem, since we could simply ignore it by binding functions to events with
elt.bind('click',echo)4. and also add 'on' method for direct vs delegated event handler definition [5]_I could spend some time working on these enhancements;)[...]
We can't use vars() either, because it is not defined in the Javascript namespace (it would be __BRYTHON__.__builtins__.vars())
fwiw the original proposal implied to bind 'vars' name in global js namespace to the value of __BRYTHON__.__builtins__.vars
I like the solution with "expose" because it gives control to the developer... and hides any implementation detail .[...]which is symmetric to elt.bind('click',echo). By the way, I don't think we need to pass an additional argument if we want an alias, nor need to prepend the module name as suggested by Billy : since "expose" is a function, we can use it as such in the main program
from javascript import expose
import X # X has a function "func" that we want to expose
echo = expose(X.func)
To avoid errors difficult to track, the solution with "expose" can be improved by allowing only functions defined in the module __main__ to be exposed
I do not get it . In your sample code you have actually exposed a function defined in a module .
With this option, yes, there is a risk that another Javascript or Python program in the same page defines another function echo(). But it's also the case if all the code is written in Javascript, or if it's all in Python : using a library requires the effort to learn its API
js frameworks have developed solutions to avoid these issues :- jquery noConflict
It's exactly the same with what is proposed for Brython : very few names are exposed, and it's up to the developer to choose the right ones