How to link html and Python object ?

44 views
Skip to first unread message

Nicolas Pinault

unread,
May 22, 2016, 4:15:22 PM5/22/16
to bry...@googlegroups.com
Hi,

Say, I have a class MyClass() which creates a DIV and adds this DIV to
document :

class MyClass() :
def __init__(self) :
d = html.DIV(id='myid')
document <= d

mc = Myclass()

In another place, I can get access to the DIV with document["myid"].
I there another way to get access to the DIV ? I mean, do I have to set
an id to get access to it ?
I want to get access to mc but I have no reference to it.
How can I get access to mc ? Can I set an attribute to the DIV which is
a reference to mc ?

Nicolas

Kiko

unread,
May 22, 2016, 4:55:58 PM5/22/16
to bry...@googlegroups.com
In the first script I use an id that can be used to import the script in the second script

Docs: http://brython.info/static_doc/en/import.html



Nicolas

--
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/aca2a2fb-e8d4-d53d-ad3c-09bbbe6947b2%40famillepinault.fr.
For more options, visit https://groups.google.com/d/optout.

Nicolas Pinault

unread,
May 23, 2016, 1:06:51 PM5/23/16
to bry...@googlegroups.com
Hi Kiko,

Le 22/05/2016 à 22:55, Kiko a écrit :
This is not what I want to achieve.
In my case, I have a single script with many modules.

Ideally, I would be able to attach a Brython object to a html element.
Something like :
module 1 :
import module2

class MyClass() :
    def __init__(self) :
        d = html.DIV(id='myid')
        d.brython_obj = self        # <--------- javascript object has an attribute referencing Brython object

        document <= d

mc = Myclass()
module2.dosomething()

module 2 :
def dosomething():
    mc = document["myid"].brython_obj

    mc.d.text += ' Added text.'

You will say I just have to pass mc as an argument to dosomething(). But for some reason, I don't want to (my actual application is much more complex).

Kiko

unread,
May 23, 2016, 1:11:23 PM5/23/16
to bry...@googlegroups.com
2016-05-23 19:06 GMT+02:00 Nicolas Pinault <d...@famillepinault.fr>:
Hi Kiko,

Le 22/05/2016 à 22:55, Kiko a écrit :
This is not what I want to achieve.
In my case, I have a single script with many modules.

Ideally, I would be able to attach a Brython object to a html element.
Something like :
module 1 :
import module2
class MyClass() :
    def __init__(self) :
        d = html.DIV(id='myid')
        d.brython_obj = self        # <--------- javascript object has an attribute referencing Brython object
        document <= d

mc = Myclass()
module2.dosomething()

module 2 :
def dosomething():
    mc = document["myid"].brython_obj

    mc.d.text += ' Added text.'

You will say I just have to pass mc as an argument to dosomething(). But for some reason, I don't want to (my actual application is much more complex).


I think you can't attach an object to a dom element (nor Brython neither javascript) . You can try to serialize your object (e.g., json) and then de-serialize and eval again the object but I think this way and what you want to do is not very natural. Maybe, if you explain a little bit better what you want to do with a more complete example we can try to help.
 

Nicolas Pinault

unread,
May 23, 2016, 3:15:51 PM5/23/16
to bry...@googlegroups.com
Never mind, I found another way to do what I want.
Thanks for your help.

Pierre Quentel

unread,
May 23, 2016, 3:22:43 PM5/23/16
to brython
You can actually set arbitrary attributes to DOM Nodes. This works perfectly :

<script type="text/python">
from browser import document, html

import module2

class MyClass:

    def __init__(self):
        self.d = d = html.DIV("essai", id="myid")
        d.attr = self
        document <= d

mc = MyClass()

module2.do_something()
</script>



where module2.py is

from browser import document

def do_something():
    mc = document['myid'].attr
    mc.d.text += " ok"


The only difference with your script is that you didn't set an attribute d to the instance of MyClass.

Nicolas Pinault

unread,
May 23, 2016, 4:58:13 PM5/23/16
to bry...@googlegroups.com
Great ! That's perfect.

Is it also applicable to generated events ?
evt = DOMEvent(evt_name)
evt.attr = my_obj
element.dispatchEvent(evt)

--
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.
Reply all
Reply to author
Forward
0 new messages