Porting htag to Brython

52 views
Skip to first unread message

man atlan

unread,
Mar 16, 2024, 6:59:36 AMMar 16
to brython
htag is my personnal project, and it won't exist if I never met brython before. (quickly : it try to do the same, but on serverside, with pure python3)
it works perfectly in a pyscript/html page too (on client side). But I'd like to make it work in a brython html page too ! (new challenge!)

For that, I need to implement my "Tag constructor", which is a mix of brython `html.<tagName>()` and domonic one. (which is able to set a lot of things at construction time)

Examples:
Tag.button("hello", _onclick = mymethod )
will produce html : <button onclick="...">hello</button>

"button" here, is an html tag name, and can be whatever imaginable

if you got web components (like shoelace, for example)
Tag.sl_button("hello", _onclick = mymethod )
will produce html : <sl-button onclick="...">hello</sl-button>

to be short:
  • the first parameter is the content of the node (everything that is str'able, or list)
  • the kargs are specials :
    • if it starts with "_" (domonic idea) : it's an html attributs (ex: _class,)
    • if not : it sets a python property on the instance
    • and if it starts with "_on" : it's a bindable method for the event
And it returns an brython HTMLElement, which can be attached in the dom

Here is a real example (use show source to see the short implem to do that)
it works great ^^

But I'd like to inherit HTMLElement, to be able to do more things ... and to be able to compose Tag (like in htag, to be able to create more complex Tag (the main goal of htag)) .... (i dream to be able to create components which will work "as is" in htag or in brython)

Can someone give me the direction/pointer to surcharge "HTMLElement" ... or at least, is it possible ?

thanks in advance
marc

Denis Migdal

unread,
Mar 16, 2024, 7:51:11 AMMar 16
to bry...@googlegroups.com
I also have a lib that can help using webcomponents : https://github.com/denis-migdal/BLISS

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/brython/714ca4d4-0af8-4a67-86cb-f481dfd03e22n%40googlegroups.com.

mana...@gmail.com

unread,
Mar 16, 2024, 8:12:55 AMMar 16
to bry...@googlegroups.com
thanks for quick response ...

No, I don't want to make "web component" (I've created wyc for that, python -> pure js )
(but thanks for bliss, i will have a look)

I want to create brython HtmlElement ...
but for now, I reach to create "brython component", in a brython style (with my Tag creator), like that :

class MyComponent(html.DIV):
    def __init__(self):
        self <= Tag.sl_button("add",_onclick=self.addline)

    def addline(self,ev):
        self <= Tag.li("one more")


I would like to be able to do something like :

class MyComponent(Tag.div):
    def __init__(self):
        self <= Tag.sl_button("add",_onclick=self.addline)

    def addline(self,ev):
        self <= Tag.li("one more")


(like in htag)

I will dive in the code (py/js), my metaclass should return a surcharge of HtmlElement ... or DomNode, or something like that ...

eeis...@auburnschl.edu

unread,
Mar 16, 2024, 8:23:18 AMMar 16
to brython
This is an interesting approach for perhaps enabling a pure python interop with HTMX.

Denis Migdal

unread,
Mar 16, 2024, 8:23:43 AMMar 16
to bry...@googlegroups.com
I believe inheriting HTMLElement (JS) should only be used with customElements.define().
Indeed, you shouldn't call directly the HTMLElement constructor and use document.createElement or customElements.??? to build an instance.

However, you can use a composition DP ?
class X:
     def __init__(self, HTML_TAG):
          self.__internal = document.createElement(HTML_TAG)
     @property
     def parent():
          return self.__internal.parent

And then :
class Y(X):
     pass

mana...@gmail.com

unread,
Mar 16, 2024, 8:46:49 AMMar 16
to bry...@googlegroups.com
Ok reached to do something ...
Now I just need to merge Tag & Tag2 together ... (to have just one !)
and it starts to mimic htag ...

mana...@gmail.com

unread,
Mar 16, 2024, 10:56:15 AMMar 16
to bry...@googlegroups.com
Now I've reached to do what I wanted (a first/dirty/usable version)
See : https://www.mlan.fr/pub/bryt_4.html (show the source)

So it's possible to create tag, with `Tag.<name>()` (like you'll do with `html.<TAG>()` ... but in the htag style)
And It's easy to compose htag tag like !

BTW, it's not 100% compatible with htag ones ... but could be in the future.
(I will perhaps adapt "htag.Tag" (my lib) before 1.0 release ... to be able to make them compatibles)



mana...@gmail.com

unread,
Mar 16, 2024, 2:03:33 PMMar 16
to bry...@googlegroups.com
And best of all : https://www.mlan.fr/pub/bryt_5.html
which import my "htag brython implem" as a module (see the module : https://www.mlan.fr/pub/htagbryt.txt (as a ".txt" to avoid proxy/webwasher trouble)

it's nearly perfect (but will need a lot of tweaks to be compatible 100% with real htag.Tag)

Now .... i will stop bothering you with my brython implem (will come back when it will be more complete (when a htag component will work in brython, pyscript, and other real python runners)



mana...@gmail.com

unread,
Mar 21, 2024, 2:44:38 PMMar 21
to bry...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages