Element#

class domsync.core._Element(document, id, tag)#

domsync.core._Element is analogous to the Javascript Element which represents an individual HTML element. The name of the class starts with an underscore, expressing the fact that this class should not be instantiated by the user, instead all instances of this class are created by domsync.Document.createElement().

Parameters
  • document (domsync.Document) – document to create the element within

  • id (str) – unique id of the element

  • tagName (str) – tag name of the element

Class attributes

  • innerText - sets and gets the element’s innerText, analogous to Javascript element.innerText

  • value - sets and gets the element’s value, analogous to Javascript element.value

  • tagName - gets the element’s tagName, analogous to Javascript element.tagName

  • children - gets the element’s list of child elements, analogous to Javascript element.children

  • firstElementChild - gets the element’s first child element, analogous to Javascript element.firstElementChild

  • lastElementChild - gets the element’s last child element, analogous to Javascript element.lastElementChild

  • attributes - gets the element’s dictionary of attributes

  • id - gets the element’s id

addEventListener(event, callback, js_value_getter=None)#

adds an event listener to the element

analogous to Javascript addEventListener

Parameters
  • event (str) – name of the event to listen to, see https://www.w3schools.com/jsref/dom_obj_event.asp for a list of valid events

  • callback (Callable(dict)) –

    the callback function to be called when the event happens. the function must take one argument which is a dict containing the details of the message:
    ’event’: name of the event, one of https://www.w3schools.com/jsref/dom_obj_event.asp
    ’id’: id of the element that the event happened on
    ’doc’: domsync.Document instance
    ’value’: value returned as a result of evaluating js_value_getter (see below)

  • js_value_getter (str) –

    a javascript expression that is executed in the context of the event and the return value of which is retrned in the ‘value’ field of the event message.
    A good example is 'this.value' in case of the ‘input’ event of an <input type=’text’> element, this will retun the current changed value of the input.
    Another example would be 'this.options[this.selectedIndex].value' in case of the ‘input’ event of a <select> element, this will retun the current changed value of the selection.
    In case of a ‘click’ event of a <button> there is no need to specify a js_value_getter because the click event doesn’t carry any relevant value (apart form the fact that the event happened).

Returns

None

appendChild(el_child)#

analogous to Javascript Element.appendChild

Parameters

el_child (domsync.core._Element) – child element to append

Returns

None

getAttribute(attrib, default=None)#

gets an attribute of an element

analogous to Javascript Element.getAttribute

Parameters
  • attrib (str) – name of the attribute to return

  • default – Optional, value to return in case the attribute is not available

Returns

str, the value of the attribute if the attribute exists, otherwise the value of the default argument

insertBefore(el_child_to_insert, el_child_before)#

inserts an element as a child before an existing child element

analogous to Javascript Element.insertBefore

Returns

None

remove()#

removes the element

analogous to Javascript Element.remove

Returns

None

removeAttribute(attrib)#

removes the given attribute

analogous to Javascript Element.removeAttribute

Parameters

attrib (str) – name of the attribute to remove

Returns

None

setAttribute(attrib, value)#

sets the attribute of an element

analogous to Javascript Element.setAttribute

Parameters
  • attrib (str) – name of the attribute to set

  • value (str) – value of the attribute to set

Returns

None

Document#

class domsync.Document#

domsync.Document is analogous to the Javascriot DOM document which contains a tree of domsync.core._Element objects. Every manipulation to the document generates Javascript code that when sent to the Browser client and evaluated results in the same DOM changes on the client side that happened in the domsync.Document.

Parameters

root_id (str) – id of the element in the client-side HTML where domsync should render

createElement(tagName, id=None, innerText=None, value=None, attributes=None)#

Creates a new element in the domsync.Document but doesn’t add it as a child to any existing elements, that needs to be done separately using appendChild.

analogous to Javascript document.createElement

This is the only way to create a new element because each element needs to be registered with the domsync.Document. This is the reason why the name of domsync.core._Element starts with an underscore character signalling that it’s a private class that is not meant to be instantiated by the user.

Parameters
  • tagName (str) – tag name of the element to be created

  • id (str) – Optional, unique id of the new element. if not provided, an automatically generated unique id will be used. Note that this is a difference between domsync and Javascript: in domsync every element has a unique id while in Javascript not necessarily.

  • innerText (str) – Optional, if provided this will be set as the innerText of the newly created element. equivalent to later setting the .innerText attribute of the element.

  • value (str) – Optional, if provided this will be set as the value attribute of the newly created element. Typically only used in case of <input type=’text’> elements. equivalent to later setting the .value attribute of the element.

  • attributes (dict) – Optional, if provided these attributes will be set on the element. equivalent to later using the setAttribute method on the element.

Returns

the newly created element

Return type

domsync.core._Element

getElementById(id, strict=True)#

Returns the element of the given ID

analogous to Javascript document.getElementById

Parameters
  • id (str) – id of the element

  • strict (bool) – optional, if True (default), then an exception is thrown if the provided id doesn’t exist. if False, then None is returned if the id doesn’t exist

Returns

the element of the provided id or None if the element doesn’t exist and strict is False

Return type

domsync.core._Element

getElementsByClassName(className)#

Returns a list of elements with the specified class name

analogous to Javascript document.getElementsByClassName

Parameters

className (str) – class name of the element

Returns

the elements of the given class name

Return type

list of domsync.core._Element

getElementsByTagName(tagName)#

Returns a list of elements with the specified tag name

analogous to Javascript document.getElementsByTagName

Parameters

tagName (str) – tag name of the element

Returns

the elements of the given tag

Return type

list of domsync.core._Element

getRootId()#

Returns the id of the root element that was passed on initialisation.

Returns

the id of the root element

Return type

str

handle_event(msg)#

The way event listeners work with domsync is that we set the event listener of an element using domsync.core._Element.addEventListener() which on the client side causes the ws_send Javascript function to be executed which sends back a message to the server containing the details of the event. When that message arrives to the server, this method needs to be called with the message as an argument which eventually triggers the callback function to be executed that was added using the domsync.core._Element.addEventListener() method.

You only need to deal with this if you are using your own server. If you use domsync.domsync_server.DomsyncServer, this is called automatically behind the scenes.

Parameters

msg (dict) –

is an event message generated on the client side containing:
’id’ of the element that generated the event
’event’ name of the HTML event that happened (see https://www.w3schools.com/jsref/dom_obj_event.asp for the list of events)
’value’ associated with the event as defined when the event was added using the js_value_getter argument of domsync.core._Element.addEventListener()

Returns

whatever the callback function returns that was added using domsync.core._Element.addEventListener()

render_js_full()#

Returns a full snapshot of Javascript code that represents the current state of the domsync.Document from scratch, not just the updates since the last time. It is useful when you have one domsync.Document instance in your memory and want to show the same instance to every users, like for example a read-only dashboard. In that case you can update your domsync.Document and send updates to already connected clients using the render_js_updates method, but whenever a new client connects you can use this method to send an initial full snapshot of the current state of the doc. Again, this only needs to be used if you decide to keep one domsync.Document instance for all users, as opposed to one domsync.Document for each user. If you use domsync.domsync_server.DomsyncServer, you don’t need to deal with this method because the server maintains one domsync.Document for each client and send updates behind the scenes automatically anyways. You only need to deal with this method if you decide to use your own server.

Returns

Javascript code containnig the full current state of the document.

Return type

str

render_js_updates()#

Returns the Javascript code changes that accummulated in the internal buffer of the domsync.Document due to any manipulations since the last call to this function. This is the method for generating the Javascript code updates that can be sent to the client. domsync.domsync_server.DomsyncServer uses this behind the scenes, so you only need to deal with this function if you want to use your own server.

Returns

the Javascript code generated since the last call to this function.

Return type

str