widgets

package
v0.10.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2024 License: MIT, MIT Imports: 9 Imported by: 1

Documentation

Overview

Package widgets implement several simple widgets that can be used to make your Go programs interact with front-end widgets in a Jupyter Notebook, using GoNB kernel.

Because most widgets will have many optional parameters, it uses the convention of calling the widget to create a "builder" object, have optional parameters as method calls, and then call `Done()` to actually display and start it.

If you want to implement a new widget, checkout `gonb/gonbui/comms` package for the communication functionality, along with tools for building widgets.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ButtonBuilder

type ButtonBuilder struct {
	// contains filtered or unexported fields
}

ButtonBuilder is used to create a button on the front-end.

func Button

func Button(label string) *ButtonBuilder

Button returns a builder object that configures and builds a new button with the given `label`.

One can use `Listen` to get updates (button clicks) -- the value returned is an int that is incremented at every click.

Call `Done` method when you finish configuring the ButtonBuilder.

func (*ButtonBuilder) Address

func (b *ButtonBuilder) Address() string

Address returns the address used to communicate to the widgets HTML element.

func (*ButtonBuilder) AppendTo

func (b *ButtonBuilder) AppendTo(parentHtmlId string) *ButtonBuilder

AppendTo defines an id of the parent element in the DOM (in the front-end) where to insert the button.

If not defined, it will simply display it as default in the output of the cell.

func (*ButtonBuilder) Done

func (b *ButtonBuilder) Done() *ButtonBuilder

func (*ButtonBuilder) HtmlId

func (b *ButtonBuilder) HtmlId() string

HtmlId returns the `id` used in the widget HTML element created.

func (*ButtonBuilder) Listen

func (b *ButtonBuilder) Listen() *comms.AddressChan[int]

Listen returns an `AddressChannel[int]` (a wrapper for a `chan int`) that receives a counter each time the button is clicked. The counter is incremented at every click.

Close the returned channel (`Close()` method) to unsubscribe from these messages and release the resources.

It can only be called after the Button is created with Done, otherwise it panics.

If for any reason you need to listen to clicks before the button is created, create a channel with the function `Listen[int](address)` directly, but you will need to ignore the first counter value sent when the button is created (with value 0).

func (*ButtonBuilder) WithAddress added in v0.9.3

func (b *ButtonBuilder) WithAddress(address string) *ButtonBuilder

WithAddress configures the widget to use the given address to communicate its state with the front-end.

The default is to use a randomly created unique address.

It panics if called after the widget is built.

func (*ButtonBuilder) WithHtmlId added in v0.9.3

func (b *ButtonBuilder) WithHtmlId(htmlId string) *ButtonBuilder

WithHtmlId sets the id to use when creating the HTML element in the DOM. If not set, a unique one will be generated, and can be read with HtmlId.

This can only be set before call to Done. If called afterward, it panics.

type SelectBuilder added in v0.9.3

type SelectBuilder struct {
	// contains filtered or unexported fields
}

SelectBuilder is used to create a select element on the front-end.

func Select added in v0.9.3

func Select(options []string) *SelectBuilder

Select returns a builder object that builds a new `<select>` element with the list of values given.

Values (used for `Listen`, `Value` and `SetValue`) are integers representing the index of options selected.

Call `Done` method when you finish configuring the SelectBuilder.

func (*SelectBuilder) Address added in v0.9.3

func (b *SelectBuilder) Address() string

Address returns the address used to communicate to the widgets HTML element.

func (*SelectBuilder) AppendTo added in v0.9.3

func (b *SelectBuilder) AppendTo(parentHtmlId string) *SelectBuilder

AppendTo defines an id of the parent element in the DOM (in the front-end) where to insert the widget.

If not defined, it will simply display it as default in the output of the cell.

It panics if called after the widget is built.

func (*SelectBuilder) Done added in v0.9.3

func (b *SelectBuilder) Done() *SelectBuilder

Done builds the HTML element in the frontend and starts listening to updates.

After this is called options can no longer be set.

The value associated with the widget can now be read or modified with `Value`, `GetValue` and `Listen` are available.

func (*SelectBuilder) HtmlId added in v0.9.3

func (b *SelectBuilder) HtmlId() string

HtmlId returns the `id` used in the widget HTML element created.

func (*SelectBuilder) Listen added in v0.9.3

func (b *SelectBuilder) Listen() *comms.AddressChan[int]

Listen returns an `AddressChannel[int]` (a wrapper for a `chan int`) that receives the index to the a counter each time the select is changed.

Close the returned channel (`Close()` method) to unsubscribe from these messages and release the resources.

It can only be called after the Slider is created with Done, otherwise it panics.

func (*SelectBuilder) SetDefault added in v0.9.3

func (b *SelectBuilder) SetDefault(idx int) *SelectBuilder

SetDefault option of the selection. If not set, it is 0. Can only be set before being built.

It panics if called after the widget is built.

func (*SelectBuilder) SetValue added in v0.9.3

func (b *SelectBuilder) SetValue(value int)

SetValue sets the value of the widget, communicating that with the UI.

func (*SelectBuilder) Value added in v0.9.3

func (b *SelectBuilder) Value() int

Value returns the current value set by the widget.

func (*SelectBuilder) WithAddress added in v0.9.3

func (b *SelectBuilder) WithAddress(address string) *SelectBuilder

WithAddress configures the widget to use the given address to communicate its state with the front-end.

The default is to use a randomly created unique address.

It panics if called after the widget is built.

func (*SelectBuilder) WithHtmlId added in v0.9.3

func (b *SelectBuilder) WithHtmlId(htmlId string) *SelectBuilder

WithHtmlId sets the id to use when creating the HTML element in the DOM. If not set, a unique one will be generated, and can be read with HtmlId.

This can only be set before call to Done. If called afterward, it panics.

type SliderBuilder

type SliderBuilder struct {
	// contains filtered or unexported fields
}

SliderBuilder is used to create a slider on the front-end.

func Slider

func Slider(min, max, value int) *SliderBuilder

Slider returns a builder object that builds a new slider with the range and value given by `min`, `max` and `value`.

Values (used for `Listen`, `Value` and `SetValue`) are integers representing the slider position.

Call `Done` method when you finish configuring the SliderBuilder.

func (*SliderBuilder) Address

func (b *SliderBuilder) Address() string

Address returns the address used to communicate to the widgets HTML element.

func (*SliderBuilder) AppendTo

func (b *SliderBuilder) AppendTo(parentHtmlId string) *SliderBuilder

AppendTo defines an id of the parent element in the DOM (in the front-end) where to insert the widget.

If not defined, it will simply display it as default in the output of the cell.

It panics if called after the widget is built.

func (*SliderBuilder) Done

func (b *SliderBuilder) Done() *SliderBuilder

Done builds the HTML element in the frontend and starts listening to updates.

After this is called options can no longer be set.

The value associated with the widget can now be read or modified with `Value`, `GetValue` and `Listen` are available.

func (*SliderBuilder) HtmlId

func (b *SliderBuilder) HtmlId() string

HtmlId returns the `id` used in the widget HTML element created.

func (*SliderBuilder) Listen

func (b *SliderBuilder) Listen() *comms.AddressChan[int]

Listen returns an `AddressChannel[int]` (a wrapper for a `chan int`) that receives a counter each time the slider is changed.

Close the returned channel (`Close()` method) to unsubscribe from these messages and release the resources.

It can only be called after the Slider is created with Done, otherwise it panics.

func (*SliderBuilder) SetValue

func (b *SliderBuilder) SetValue(value int)

SetValue sets the value of the widget, communicating that with the UI.

func (*SliderBuilder) Value added in v0.9.3

func (b *SliderBuilder) Value() int

Value returns the current value set by the widget.

func (*SliderBuilder) WithAddress added in v0.9.3

func (b *SliderBuilder) WithAddress(address string) *SliderBuilder

WithAddress configures the widget to use the given address to communicate its state with the front-end.

The default is to use a randomly created unique address.

It panics if called after the widget is built.

func (*SliderBuilder) WithHtmlId added in v0.9.3

func (b *SliderBuilder) WithHtmlId(htmlId string) *SliderBuilder

WithHtmlId sets the id to use when creating the HTML element in the DOM. If not set, a unique one will be generated, and can be read with HtmlId.

This can only be set before call to Done. If called afterward, it panics.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL