slash.basic

This module contains the Slash basic components.

class slash.basic.Axes(*, width=384, height=256)

Bases: SVG

Figure with x-axis and y-axis.

Example

>>> from slash.basic import Axes, Graph
>>>
>>> axes = Axes()
>>> axes.add_plot(Graph([0, 1, 2, 3], [42, 37, 96, 51]))
>>> axes.render()
Parameters:
  • width (int) – Width of the figure in pixels.

  • height (int) – Height of the figure in pixels.

add_plot(plot)

Add plot to the figure.

Parameters:

plot (Plot) – Plot instance to add.

Return type:

Self

clear_plots()

Remove all plots from the figure.

Return type:

Self

property grid: bool

Flag indicating whether to show grid.

property legend: bool

Flag indicating whether to show legend.

remove_plot(plot)
Parameters:

plot (Plot)

Return type:

Self

render()
Return type:

Self

set_grid(grid)
Parameters:

grid (bool)

Return type:

Self

set_legend(legend)
Parameters:

legend (bool)

Return type:

Self

set_title(title)
Parameters:

title (str | None)

Return type:

Self

set_xlabel(xlabel)
Parameters:

xlabel (str | None)

Return type:

Self

set_xlim(xmin=None, xmax=None)
Parameters:
Return type:

Self

set_xticks(xticks)
Parameters:

xticks (Sequence[float | tuple[float, str]] | None)

Return type:

Self

set_ylabel(ylabel)
Parameters:

ylabel (str | None)

Return type:

Self

set_ylim(ymin=None, ymax=None)
Parameters:
Return type:

Self

set_yticks(yticks)
Parameters:

yticks (Sequence[float | tuple[float, str]] | None)

Return type:

Self

property title: str | None

Title to appear at the top of the figure.

property xlabel: str | None

Description of the x-axis to appear below it.

property xlim: tuple[float | None, float | None]
xticks()
Return type:

Sequence[tuple[float, str]]

property ylabel: str | None

Description of the y-axis to appear next to it.

property ylim: tuple[float | None, float | None]
yticks()
Return type:

Sequence[tuple[float, str]]

class slash.basic.Bar(xs, ys, color=None, label=None, opacity=1.0, width=None)

Bases: Plot

Bar plot.

Parameters:
  • xs (Sequence[float]) – X-coordinates of data points.

  • ys (Sequence[float]) – Y-coordinates of data points.

  • color (str | None) – Color in HTML notation. If None, one of the default colors is used.

  • opacity (float) – Opacity value.

  • label (str | None) – Label to be used in the legend.

  • width (float | None) – Width of the bars.

plot(frame, xy_to_uv)

Construct plot elements inside frame.

Parameters:
width: float | None = None
class slash.basic.Checkbox(label='', *, checked=False, disabled=False)

Bases: Elem, SupportsOnClick

Checkbox element.

Parameters:
  • label (str | Elem) – Text label after checkbox.

  • checked (bool) – Flag indicating if checkbox is checked.

  • disabled (bool) – Flag indicating if checkbox is disabled.

property checked: bool
property disabled: bool
property label: str | Elem
set_checked(checked)
Parameters:

checked (bool)

Return type:

Self

set_disabled(disabled)
Parameters:

disabled (bool)

Return type:

Self

set_label(label)
Parameters:

label (str | Elem)

Return type:

Self

class slash.basic.DataTable(keys, *, labels=None, max_rows=10)

Bases: Elem

Table for displaying rows of data.

Parameters:
  • keys (Sequence[str]) – Sequence of keys for the columns.

  • labels (Mapping[str, str | Elem] | None) – Mapping of labels to be used as column header indexed by column keys. If None, or if a key is missing, the key itself is used as the column header.

  • max_rows (int) – Maximum number of rows displayed at once.

property labels: Mapping[str, str | Elem]
property max_rows: int
set_data(data)

Set data for table contents.

Parameters:

data (Sequence[Mapping[str, int | float | str | Elem]]) – Sequence of rows of data, each row being a mapping whose keys correspond to the keys of the table and whose values are the data.

Return type:

Self

set_keys(keys)

Set keys for table columns.

Parameters:

keys (Sequence[str]) – Sequence of column headings.

Return type:

Self

set_labels(labels)
Parameters:

labels (Mapping[str, str | Elem] | None)

Return type:

Self

set_max_rows(max_rows)
Parameters:

max_rows (int)

Return type:

Self

class slash.basic.Download(path, *, text='Download')

Bases: Elem

Download button element.

Parameters:
  • path (Path) – Path to file to download.

  • text (str) – Text shown on the button.

class slash.basic.FillBetween(xs, ys, zs=None, color=None, opacity=1.0, label=None)

Bases: Plot

Fill plot.

Parameters:
  • xs (Sequence[float]) – X-coordinates of data points.

  • ys (Sequence[float]) – First set of y-coordinates of data points.

  • zs (Sequence[float] | None) – Second set of y-coordinates of data points.

  • color (str | None) – Color in HTML notation. If None, one of the default colors is used.

  • opacity (float) – Opacity value.

  • label (str | None) – Label to be used in the legend.

plot(frame, xy_to_uv)

Construct plot elements inside frame.

Parameters:
class slash.basic.Graph(xs, ys, color=None, label=None, opacity=1.0)

Bases: Plot

Graph plot.

Parameters:
  • xs (Sequence[float]) – X-coordinates of data points.

  • ys (Sequence[float]) – Y-coordinates of data points.

  • color (str | None) – Color in HTML notation. If None, one of the default colors is used.

  • opacity (float) – Opacity value.

  • label (str | None) – Label to be used in the legend.

plot(frame, xy_to_uv)

Construct plot elements inside frame.

Parameters:
class slash.basic.Icon(icon)

Bases: Elem

Icon element.

Parameters:

icon (str) – Icon type, such as info, warning, error, debug, loading, help, refresh, moon, sun, trash, cancel, download.

property icon: str
set_icon(icon)
Parameters:

icon (str)

Return type:

Self

class slash.basic.LaTeX(latex, *, display=False)

Bases: HTML

LaTeX element.

Parameters:
  • latex (str) – String of LaTeX to be formatted as HTML.

  • display (bool) – If true, math is rendered display style, otherwise inline math.

set_latex(latex)
Return type:

Self

class slash.basic.Loading(description)

Bases: object

Loading screen as asynchronous context manager.

This class can be used for instance in a handler to show a loading screen to give the user feedback about what the handler is doing.

Example

>>> from slash.basic import Loading
>>>
>>> async with Loading("Doing first task..") as loading:
>>>     # Do first task ..
>>>     await loading.set_description("Doing second task..")
>>>     # Do second task ..
Parameters:

description (str | Elem) – Description to display above loading spinner.

async set_description(description)

Set description to display above loading spinner.

Parameters:

description (str | Elem)

Return type:

Self

class slash.basic.Markdown(markdown)

Bases: HTML

Markdown element.

Parameters:

markdown (str) – String of markdown to be formatted as HTML.

set_markdown(markdown)
Return type:

Self

class slash.basic.Pie(*, width=384, height=256)

Bases: SVG

Pie chart element.

Parameters:
  • width (int) – Width of figure in pixels.

  • height (int) – Height of figure in pixels.

property gap: float

Radius of gap in the center of the pie in pixels.

property legend: bool

Flag indicating whether to show legend.

property radius: float

Radius of the pie in pixels.

render(labels, values)

Render the pie chart.

Parameters:
  • labels (Sequence[str]) – Sequence of labels for each of the categories.

  • values (Sequence[float]) – Sequence of values for each of the categories.

Return type:

Self

set_gap(gap)
Parameters:

gap (float)

Return type:

Self

set_legend(legend)
Parameters:

legend (bool)

Return type:

Self

set_radius(radius)
Parameters:

radius (float)

Return type:

Self

set_title(title)
Parameters:

title (str | None)

Return type:

Self

property title: str | None

Title to appear at the top of the figure.

class slash.basic.Plot(xs, ys, color=None, label=None, opacity=1.0)

Bases: ABC

Abstract class containing information for a plot in a Axes figure.

Parameters:
  • xs (Sequence[float]) – X-coordinates of data points.

  • ys (Sequence[float]) – Y-coordinates of data points.

  • color (str | None) – Color in HTML notation. If None, one of the default colors is used.

  • opacity (float) – Opacity value.

  • label (str | None) – Label to be used in the legend.

color: str | None = None
label: str | None = None
opacity: float = 1.0
abstractmethod plot(frame, xy_to_uv)

Construct plot elements inside frame.

Parameters:
Return type:

None

xs: Sequence[float]
ys: Sequence[float]
class slash.basic.Progress(value=0.0, text=None)

Bases: Elem

Progress bar element.

Parameters:
  • value (float) – Number between 0 and 1 indicating the progress.

  • text (str | None) – Text in the center of the progress bar. Defaults to the percentage of progress.

set_value(value, text=None)
Parameters:
Return type:

None

property value: float
class slash.basic.Radio(label='', *, checked=False, disabled=False)

Bases: Elem, SupportsOnClick

Radio button element.

Parameters:
  • label (str | Elem) – Text label after radio button.

  • checked (bool) – Flag indicating if radio button is selected.

  • disabled (bool) – Flag indicating if checkbox is disabled.

property checked: bool
connect(other)

Connect to other radio button.

In a group of connected radio buttons, at most one can be selected at a time.

Parameters:

other (Radio) – Radio button to connect to.

Return type:

Self

property disabled: bool
property label: str | Elem
set_checked(checked)
Parameters:

checked (bool)

Return type:

Self

set_disabled(disabled)
Parameters:

disabled (bool)

Return type:

Self

set_label(label)
Parameters:

label (str | Elem)

Return type:

Self

class slash.basic.SVG(*children, **attrs)

Bases: SVGElem

HTML <svg> element.

Parameters:
class slash.basic.SVGElem(tag, *children, **attrs)

Bases: Elem

Analogous to Elem for SVG elements.

SVG elements are created with the namespace http://www.w3.org/2000/svg.

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 (Any) – Additional attribute values.

class slash.basic.Scatter(xs, ys, color=None, label=None, opacity=1.0)

Bases: Plot

Scatter plot.

Parameters:
  • xs (Sequence[float]) – X-coordinates of data points.

  • ys (Sequence[float]) – Y-coordinates of data points.

  • color (str | None) – Color in HTML notation. If None, one of the default colors is used.

  • opacity (float) – Opacity value.

  • label (str | None) – Label to be used in the legend.

plot(frame, xy_to_uv)

Construct plot elements inside frame.

Parameters:
class slash.basic.Tabs(labels, *, value=None)

Bases: Elem, SupportsOnChange

Tabs element.

Parameters:
  • labels (list[str]) – List of labels for the tabs.

  • value (str | None) – Label of currently selected tab.

property value: str
class slash.basic.Tooltip(*children, target)

Bases: Elem

Tooltip element.

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

  • target (Elem) – Target element. The tooltip will be shown when the target element is hovered.

class slash.basic.Upload(*, text='Drop files or click to upload', multiple=False)

Bases: Elem

Upload field element.

Parameters:
  • text (str) – Text to display inside the upload field.

  • multiple (bool) – Flag indicating if uploading multiple files at once is allowed.

onupload(handler)

Add event handler for upload event.

Parameters:

handler (Callable[[UploadEvent], Any] | Callable[[], Any]) – Function to be called when one or more files are uploaded.

Return type:

Self

upload(event)

Trigger upload event.

Parameters:

event (UploadEvent) – Event instance containing upload information.

Return type:

None

async slash.basic.confirm(message, *, ok_text='OK', cancel_text='Cancel')

Display confirmation dialog.

Parameters:
  • message (str | Elem) – Message to display in the confirmation dialog.

  • ok_text (str) – Text to display on the OK button.

  • cancel_text (str) – Text to display on the Cancel button.

Returns:

Boolean indicating whether OK (True) or Cancel (False) was selected.

Return type:

bool