slash.core

This module contains the Slash core classes.

class slash.core.Attr(name)

Bases: property

Property class representing an attribute of an element.

Parameters:

name (str) – Name of the attribute.

property name: str
class slash.core.Elem(tag, *children, **attrs)

Bases: object

Base class for all Slash elements.

Parameters:
  • tag (str) – HTML tag of the element.

  • children (Elem | str | Sequence[Elem | str]) – Child or children of element. Either an element, string or list of elements and strings.

  • attrs (str | int) – Additional attribute values.

add_class(name)

Add one or more classes to element.

Parameters:

name (str) – Name of class to add. Multiple names may be provided separated by spaces.

Return type:

Self

append(*children)

Append to the children of this element.

Parameters:

children (Elem | str | Sequence[Elem | str]) – Child or children to append. Either an element, string or list of elements and strings.

Return type:

Self

attrs()

Get element attributes.

Returns:

Dictionary containing the element attributes.

Return type:

dict[str, Any]

property children: list[Elem | str]
clear()

Unmount all children.

Return type:

Self

contains(elem)

Check if another element is contained by this element.

Parameters:

elem (Elem) – The other element.

Returns:

Boolean indicating if this element contains the other element.

Return type:

bool

property id: str
insert(position, *children)

Insert into the children of this element at given position.

Parameters:
  • position (int) – Index before which to insert children.

  • children (Elem | str | Sequence[Elem | str]) – Child or children to append. Either an element, string or list of elements and strings.

Return type:

Self

is_mounted()

Check if element is mounted.

Returns:

Boolean indicating if element is mounted.

Return type:

bool

mount()

Mount element.

Return type:

Self

onmount(handler)

Add event handler for mount event.

Parameters:

handler (Callable[[MountEvent], Any] | Callable[[], Any]) – Handler to be called when element is mounted.

Return type:

Self

onunmount(handler)

Add event handler for unmount event.

Parameters:

handler (Callable[[UnmountEvent], Any] | Callable[[], Any]) – Handler to be called when element is unmounted.

Return type:

Self

property parent: Elem | None

Parent of element, or None if the element is the root element.

remove_attr(name)

Remove attribute from element.

Parameters:

name (str) – Attribute name.

Return type:

Self

remove_class(name)

Remove one or more classes from element.

Parameters:

name (str) – Name of class to add. Multiple names may be provided separated by spaces.

Return type:

Self

set_attr(name, value='')

Set attribute of element.

Parameters:
  • name (str) – Attribute name.

  • value (str | int) – Attribute value.

Return type:

Self

set_text(text)

Set the text content of the element.

Parameters:

text (str)

Return type:

Self

style(style)

Update CSS style of element.

Parameters:

style (Mapping[str, str | None]) – Mapping with CSS attributes as keys. If a value is a string, the CSS attribute is set to that value. If a value is None, the CSS attribute is reset.

Return type:

Self

property tag: str
property text: str

Element text contents.

unmount(*, reset_parent=True)

Unmount element.

Parameters:

reset_parent (bool) – Flag indicating whether parent should be reset.

Return type:

Self

class slash.core.History

Bases: object

Class representing the JavaScript window.history object.

back()

Call the JavaScript window.history.back method.

Return type:

None

forward()

Call the JavaScript window.history.forward method.

Return type:

None

go(delta)

Call the JavaScript window.history.go method.

Parameters:

delta (int)

Return type:

None

onpopstate(handler)

Add event handler for popstate event.

Parameters:

handler (Callable[[PopStateEvent], Any] | Callable[[], Any]) – Handler to call on popstate event.

Return type:

None

popstate(event)

Trigger popstate event.

Parameters:

event (PopStateEvent) – Popstate event to be passed to handlers.

Return type:

None

push(state, url=None)

Call the JavaScript window.history.pushState method.

Parameters:
  • state (Any) – State to set. Must be JSON serializable.

  • url (str | None) – URL to set.

Return type:

None

replace(state, url=None)

Call the JavaScript window.history.replaceState method.

Parameters:
  • state (Any) – State to set. Must be JSON serializable.

  • url (str | None) – URL to set.

Return type:

None

class slash.core.Location(url)

Bases: object

Class containing detailed information on client location.

Parameters:

url (str)

property fragment: str

Fragment of location, that is the part after the # character.

property host: str

Host of location, such as 127.0.0.1:8080.

property hostname: str

Hostname of location, such as 127.0.0.1.

property path: str

Path of location, such as /path.

property port: int

Port of location, such as 8080.

property query: Mapping[str, str]

Query of location as key value mapping.

property scheme: str

Scheme of location, such as http or https.

property url: str

URL of location.

class slash.core.MountEvent(target)

Bases: object

Event that fires when an element is mounted.

Parameters:

target (Elem) – Element that was mounted.

property target: Elem

Element that was mounted.

class slash.core.PopStateEvent(state: 'Any')

Bases: object

Parameters:

state (Any)

state: Any
class slash.core.Session(server, client)

Bases: object

The Session class is used to communicate with the currently-connected user.

Parameters:
  • server (Server)

  • client (Client)

accept_file(handler)

Create an endpoint for file uploading.

Parameters:

handler (Callable[[UploadEvent], Any] | Callable[[], Any]) – Handler to be called when files are upload.

Returns:

URL to which files can be uploaded.

Return type:

str

add_script(path)

Add script.

Parameters:

path (Path) – Path to script.

Return type:

None

add_stylesheet(path)

Add stylesheet.

Parameters:

path (Path) – Path to stylesheet.

Return type:

None

call_handler(handler, event)

Call event handler in the context of the session.

Parameters:
  • handler (Callable[[E], Any] | Callable[[], Any]) – Event handler to execute.

  • event (E) – Event to be passed to handler.

Return type:

None

cancel_tasks(msg=None)

Cancel all tasks associated with this session.

Parameters:

msg (str | None)

Return type:

None

create_future()

Create future instance.

Return type:

Future

create_task(coroutine)

Create task in the context of the session.

Parameters:

coroutine (Awaitable[None]) – Awaitable function to run.

Return type:

None

static current()

Get the current session.

Returns:

Current session instance or None.

Return type:

Session | None

execute(jsfunction, args, name=None)

Execute a JavaScript function.

Parameters:
  • jsfunction (JSFunction) – JavaScript function instance to execute.

  • args (list[Any]) – List of arguments to provide to the function.

  • name (str | None) – If set, the output of the function will be stored on the client under this name. The output can later be accessed in JavaScript using Slash.value(name).

Return type:

None

async flush()

Send all queued messages to the client.

Return type:

None

get_data(key)

Get value in local storage.

Parameters:

key (str) – Data entry key.

Returns:

Data entry value.

Return type:

str | None

get_elem(id)

Get element by id.

Parameters:

id (str) – Element id.

Returns:

Element with given id or None if no such element exists.

Return type:

Elem | None

get_theme()

Get current theme.

Return type:

Literal[‘light’, ‘dark’]

property history: History

Session history instance.

property id: str

Session id.

property location: Location

Session location instance.

log(message, *, level='info', details=None)

Send logging message to the client.

Parameters:
  • message (str) – Message to display.

  • level (Literal['info', 'debug', 'warning', 'error']) – Type of logging message. Either ‘info’, ‘debug’, ‘warning’ or ‘error’.

  • details (str | Elem | None) – Details of message to display.

Return type:

None

static require()

Get the current session, or raise an error if there is none.

Returns:

Current session instance.

Raises:

RuntimeError – If there is no current session.

Return type:

Session

send(message)

Send a message to the client.

Parameters:

message (Message) – Message to be sent.

Return type:

None

set_data(key, value)

Set value in local storage.

Parameters:
  • key (str) – Data entry key.

  • value (str | None) – Data entry value. If None, then the key is removed.

Return type:

None

set_favicon(path)

Set favicon.

Parameters:

path (Path) – Path to favicon file.

Return type:

None

set_location(url)

Navigate to location.

Args;

url: URL to navigate to.

Parameters:

url (str)

Return type:

None

set_root(root)

Set root element.

Parameters:

root (Elem) – Element to set as root element.

Return type:

None

set_theme(theme)

Set theme.

Parameters:

theme (Literal['light', 'dark']) – Either ‘light’ or ‘dark’.

Return type:

None

set_title(title)

Set document title.

Parameters:

title (str) – Document title.

Return type:

None

share_file(path)

Create a download endpoint for a local file.

The file at the given path will be served to anyone who accesses the returned URL.

Parameters:

path (Path) – Path to the local file to be made accessable.

Returns:

URL from which the file can be accessed.

Return type:

str

class slash.core.UnmountEvent(target)

Bases: object

Event that fires when an element is unmounted.

Parameters:

target (Elem) – Element that was unmounted.

property target: Elem

Element that was unmounted.