Skip to main content

Create WebCompoent (Custom Element) from a python file

Project description

wyc

Create a Web Component (a Custom Element) from a python file (transpile python code to javascript (es2015)).

features

  • Use python to define your custom element (the important one ;-))
  • Use @react decorator to auto declare js methods (avoid observedAttributes and attributeChangedCallback)
  • can generate multiple custom elements from a single python file
  • auto register component's names in the page, based on classnames (customElements.define("my-component", MyComponent))
  • include javascript code (in module docstring)
  • generate es2015 javascript, for a maximum of compatibilities
  • 100% unittest coverage
  • should work with py2 too

install

pip install wyc

usecase

On server side ... just declare a http endpoint (/generate/<py_file>), get the content of the <py_file> and just return wyc.build(content).

So, your python file is automatically transpiled to JS, and directly usable in your html page, by adding a <script src='/generate/file.py' ></script>.

If your component class is named "MyComponent", it will be usable as <my-component ...> ... </my-component>

documentation

A minimal python custom element could be:

class HelloWorld(HTMLElement):
    """<div>Hello World</div>"""
    pass

When it's linked in your html page, you can start to use <hello-world/>.

Your class must inherit from HTMLElement, so you will have access to shadow dom thru self.shadowRoot. And your class will work exactly like HTMLElement in js side.

Initialize things at constructor phase

Your component can use an init(self) instance method, to initialize things in the constructor phase.

class HelloWorld(HTMLElement):
    """<div>Hello World</div>"""
    def init(self):
        self.root = self.shadowRoot.querySelector("div")

Declare react's attributes

By using the @react(*attributes), you can declare method which will be called when an attribute change (no need to use the observedAttributes and attributeChangedCallback)

class HelloWorld(HTMLElement):
    """<div>Hello World</div>"""

    @react("nb")
    def method(self):
        ...

When attribute "nb" change, the method is called ... simple!

Declare js code in py component

Sometimes you'll need to use external js, you can declare them in module docstrings.

"""
var myExternalJs=function() { ... }
"""

class HelloWorld(HTMLElement):
    """<div>Hello World</div>"""

    def a_method(self):
        myExternalJs()

see examples, for real examples and more tips ...

history

At the beginning, I wanted to build the same kind of things for brython ... but it was not a good idea, because brython would have been mandatory to use them.

Based on my experience with vbuild, I had made a POC ... And the POC comes to a real life module, which is pretty usable, in production too.

Like that, wyc components are usable in html/js, brython, angular, react, svelte ...etc... it's the power of standards.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wyc-1.0.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

wyc-1.0.0-py2.py3-none-any.whl (5.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file wyc-1.0.0.tar.gz.

File metadata

  • Download URL: wyc-1.0.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.10 Linux/5.11.0-37-generic

File hashes

Hashes for wyc-1.0.0.tar.gz
Algorithm Hash digest
SHA256 58a3421e22ac8c05ac52d7bcff766a868f9828677ba2977459d779eb40198299
MD5 428da5b74f997d67d9a67b25a949a62f
BLAKE2b-256 4e39376c35bce4943851af011576609fe627ebb197b15bd4a21882683e68df78

See more details on using hashes here.

File details

Details for the file wyc-1.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: wyc-1.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.10 Linux/5.11.0-37-generic

File hashes

Hashes for wyc-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b2184da6c470dd96642011e691c1055cf828b104069f548ca8b4cdd8dde190f1
MD5 affffe4a92c382ac45ae4a31ac78351d
BLAKE2b-256 a3ef1fd3b13c64490f7a6a42c08fb96f82dae66566bea7c914fa7587d77edfa5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page